I am using some unity "socket-io" plugin i found on the asset store. (its essentially just a scoket.io wrapper for websocket-sharp) everything works absolutely fine, i can catch clients connecting or disconnecting, i can also emit from both sides and call some functions, but the only problem is, when i try to send data from server using .emit("name", data); client receives this data properly(i can console.log it) and suddenly disconnects, but restores connection afterwards. if i just use .emit("name") everything works fine.

This is my server code:

//where _PLAYERS is just an array(object) of socket.id-s with corresponding Player objs
for (var key in _PLAYERS) {
    var data = {
        name: "test"
    io.sockets.connected[key].emit("update", data);

I have also tried: io.to(key).emit("update", data); and inside io.on("connection") i did socket.emit("update", data). disconnects in all cases.


socket.On("update", OnUpdate);
public void OnUpdate(SocketIOEvent e)
    Debug.Log(e.data); //this one doesn't work here

but it works inside wrapper code:

    private void OnMessage(object sender, MessageEventArgs e)
        #if SOCKET_IO_DEBUG
        //result: 42["update","{\"name\":\"test\"}"]
        debugMethod.Invoke("[SocketIO] Raw message: " + e.Data);
        Packet packet = decoder.Decode(e);

        switch (packet.enginePacketType) {
            case EnginePacketType.OPEN:     HandleOpen(packet);     break;
            case EnginePacketType.CLOSE:    EmitEvent("close");     break;

Hence, correct client receives a message with data successfully, but wrapper or websocket-sharp(not really sure which) causes it to error and doesn't fire my onUpdate() method.

OnMessage is just a function bind to websocket-sharp's onMessage. Like this:

ws = new WebSocket(url);
ws.OnMessage += OnMessage;

Keep in mind that io.sockets.connected[key].emit("update"); without data works fine. But i need to send data. Also client can .emit("name", data) to server, and it also works, but data from server crashes.

My main question is, what are all the possible reasons in this whole websocket thing that may cause client to disconnect after receiving a message with data? So If anybody isn't familiar with these particular libraries, at least i would be able to debug and find that error myself.

ps: websocket-sharp

and the wrapper i am using(which is deprecated a long time ago)


Finally, after many hours of debugging, found an error. This library expects data from server to be a JSON object, and not string (that's a bit weird because this library allows to send both string and object to the server)

so instead of io.to(key).emit("update", JSON.stringify(data));

I did:

io.to(key).emit("update", { data: "isMine" });

And now, everything is fine :) Hope that will help somebody in future.


Your Answer

By clicking “Post Your Answer”, you agree to our terms of service, privacy policy and cookie policy

Not the answer you're looking for? Browse other questions tagged or ask your own question.