Search code examples
websocketxsockets.net

XSocket.net ClientPool not working properly


I have a strange problem using XSockets.net, maybe someone can help me to find what I'm doing wrong.

I have in my main following method:

public void SendMessageToTestController(Guid id, string mess)
{
    ITextArgs textargs = new TextArgs(mess, "SendEventMessageToClient", "TestController");
    poolClient.Send(new { clientId = id, message = textargs }, "SendEventMessageToClient");
}

and in my controller:

 public void SendEventMessageToClient(Guid clientId, ITextArgs message)
        {
            if (message != null)
            {
                this.SendTo(p => p.ClientGuid == clientId, message.data, "events");
            }
        }

Everything works fine, the event from the clientpool arrives at the controller, the value of the clientId is right, but the message is always null. am I doing something wrong?

Thanks!


Solution

  • You should not create a TextArgs and send it to the server. You can send whatever you want to the server and if you do not have custom parameters on your serverside method (action method) XSockets will create an ITextArgs for you since you have not specified anything. See: http://xsockets.net/docs/c-server-api#custom-controllers

    Your code should be...

    //Client    
    public void SendMessageToTestController(Guid id, string mess)
    {        
        poolClient.Send(new { clientId = id, message = mess }, "SendEventMessageToClient");
    }
    
    //Action method on Controller
    public void SendEventMessageToClient(Guid clientId, string message)
    {
        this.SendTo(p => p.ClientGuid == clientId, message, "event");
    }
    

    Since serialization is faster when having objects instead of multiple parameters I would go for a simple model containing clientid and message and have that as a single parameter on the aciton method instead.