XSockets / XSockets.Clients

Realtime clients for XSockets.NET 4.*
MIT License
11 stars 9 forks source link

Initialization of XSocketClient without third parameter (controllers) failed #8

Closed dannyyy closed 9 years ago

dannyyy commented 9 years ago

Trying to connect to a XSockets server without using the third parameter params string[] controllers will establish successful a connection but controller invocation has no effect.

Doesn't work: var client = new XSocketClient("ws://localhost:4502", "http://localhost");

Work: var client = new XSocketClient("ws://localhost:4502", "http://localhost", new[] {"Test});

Sample project can be found here: http://www.heiniger.ch/cloud/XSocketsControllerSample.zip

codeplanner commented 9 years ago

This is due to the fact that the controller never will have a connectionId unless it is specified in the call to the XSocketClient ctor. The change to be able to do what you want is easy though.. Just have to change the condition in the Invoke method on the client... Instead of checking the ConnectionId we have to check the IsHandshakeDone flag...

From:

public virtual void Invoke(IMessage payload)
{
    if (!this.XSocketClient.IsConnected)
        throw new Exception("You cant send messages when not connected to the server");

    payload.Controller = this.ClientInfo.Controller;
    var frame = GetDataFrame(payload).ToBytes();  
    //If controller not yet open... Queue message
    if (this.ClientInfo.ConnectionId == Guid.Empty)
    {
        this.queuedFrames.AddRange(frame);
        return;
    }

    this.XSocketClient.Socket.Send(frame, () => { }, err => FireClosed());            
}

To:

public virtual void Invoke(IMessage payload)
{
    if (!this.XSocketClient.IsConnected)
        throw new Exception("You cant send messages when not connected to the server");

    payload.Controller = this.ClientInfo.Controller;
    var frame = GetDataFrame(payload).ToBytes();  
    //If controller not yet open... Queue message
    if (!this.XSocketClient.IsHandshakeDone)
    {
        this.queuedFrames.AddRange(frame);
        return;
    }

    this.XSocketClient.Socket.Send(frame, () => { }, err => FireClosed());            
}
codeplanner commented 9 years ago

I will push the changes to the repo now so that you can build it if you want to, but the release to nuget will not be done today.

codeplanner commented 9 years ago

Included in 4.2.0 that will be released 2015-03-30