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?

4

2 回答 2

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
于 2017-01-29T23:24:43.020 回答
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.

于 2017-01-30T09:42:55.123 回答