1

i can connect to my socketio server, but how can i send a requst with params?

My socketio server listen event

socket.on('init', ( data ) => {
    console.log('init', data);
});

on client side i tryed to do this

 _socket.OnMessage += (sender, e) =>
{
    Console.WriteLine("message: {0} {1}", e.IsBinary, e.Data);
    _socket.Send(MakePacket("init", new Init
        {
            key = "1",
            room = "eq",
            type = "s"
        }.ToJson())
    );
};

private string MakePacket(string e, string data)
{
    return new[] {e, data}.ToJson();
}

so i send json to server ["init", {"type":"s","key":"1","room":"eq"}]

But server wont react at this packet. Server working fine, i have problem only with call this event at C#. What i do wrong?

SLI
  • 713
  • 11
  • 29

2 Answers2

4

The problem is that socket.io is not plain websocket, but a custom protocol on top of websocket (or also on top of HTTP long polling as a fallback and on initialization). That means to speak with a socket.io server you have to encode your data and messages just like socket.io would do it. Here seems to be some documentation about how it works.

Alternatives:

  • Use a socket.io library on client side - but I don't know if one exists
  • Work with raw websockets on server side
Matthias247
  • 9,836
  • 1
  • 20
  • 29
3

C# has socketio library but i have some issues that i can't find answers at and there are no support at all. So i switched to websocket-sharp.

Afer some reseach of debug info from socketio server i found the answer. If i send this string all works fine

42["init", {"type":"s","key":"1","room":"eq"}]

Just add 42 before json and all will work fine. Magic. I think this 42 number is like your current connect status or something like this. Because when you just connect to socketio server it's send string with 0 before json.

SLI
  • 713
  • 11
  • 29
  • 1
    In order to understand these codes you can refer to: https://github.com/socketio/engine.io-protocol. Also in order to connect to Socket.IO server people should know that they ill need the following suffix: "wss://{BaseUrl}/socket.io/?EIO=2&transport=websocket" – Guy P Oct 07 '18 at 11:10