Search code examples
c#monosocket.ioxsockets.net

XSocket client with Socket.IO backend?


I was wondering if it's possible to use XSocket as a client and Socket.IO as the server side.

I was pretty sure that it wouldn't work but then I read this post, which is effectively the opposite of what I'd like to do. and that seems to work. The major difference I guess is that I'm not writing the server, I'm just trying to use one that already exists.

Here's what I have so far:

    public static void Main (string[] args)
    {
        try{
            const string ClientID = "--MyClientID--";
            const string AccessToken = "--MyAccessToken--";

            XSocketClient client = new XSocketClient ("https://streamtip.com", "https://streamtip.com", "StreamTipIO");

            client.QueryString.Add("client_id", Uri.EscapeDataString(ClientID));
            client.QueryString.Add("access_token",Uri.EscapeDataString(AccessToken));

            client.OnError += 
                (object sender, XSockets.Client40.Common.Event.Arguments.OnErrorArgs e) => Console.WriteLine (e);
            client.OnConnected += 
                (object sender, EventArgs e) => Console.WriteLine ("Connected");
            client.OnDisconnected += 
                (object sender, EventArgs e) =>  Console.WriteLine ("Disconnected");
            client.OnPing += 
                (object sender, XSockets.Client40.Model.Message e) => Console.WriteLine (e);
            client.OnPong += 
                (object sender, XSockets.Client40.Model.Message e) => Console.WriteLine (e);


            //client.Controller("test").On("authenticated", () => Console.WriteLine("test"));
            client.Controller("StreamTipIO").On<string>("authenticated", Console.WriteLine);

            client.Open();
        }
        catch(Exception e)
        {
            Console.WriteLine (e);
        }

        Console.ReadLine ();
    }

The weird bit is It seems to connect, but I'm not getting any data.... I assume this is something to do with Controllers and the fact I just made on up, as I'm not sure what you'd name the controller when trying to use a Socket.IO backend...

--Output

Disconnected

Connected

Oddly enough disconnected fires first.... not sure what that's about

On a side note:

I'd like to beable to connect to Socket.IO servers in C# but it seems really arbitrary.. I've already tried a few C# Socket.Io ports, SocketIoClientDotNet actually works perfectly, until you try to run it in Mono then it has this error


Solution

  • You can connect anything with TCP to a XSockets.NET server since XSockets allows cross-protocol communication. In clear text this means that XSockets does not care about the "transport" or "language" of each client. The server does not care about message format either since each protocol is responsible for converting messages going in/out

    You can not use a XSockets client with a socket.io server. They will probably connect, but the format of the messages will not fit directly into socket.io.

    Since your mission seems to be to use C#/Mono with socket.io just take the XSockets C# client (or any other websocket client in C#) and rebuild it for socket.io. The XSockets client runs on Mono and is open source on github

    My guess is that other clients will be easier to port since the XSockets client is very specific to XSockets and probably more complex than a simple websocket client in C#