# Unity websockets client disconnects after server emits with data

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.

client:

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);
#endif
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.

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.

• Signed up for this StackExchange to upvote this answer. You saved me so much time! IMO, the SocketIO library should handle errors gracefully and not just terminate, but beggars can't be choosers Apr 1 '20 at 6:00

I don't have enough reputation to comment yet but I wanted to thank Nick for the above answer. I was facing the same issue but my Unity client was disconnecting when the server sent events that it wasn't listening for that were not in JSON format.

• Thanks for thanking me. After some experience with websockets and unity, I'd also advise you to not use websockets with unity for making a real, working multiplayer game, unless you are just doing it for fun and for self learning. Main problem I encountered is no "Good" open source implementation of WS for unity, and another one is the Code reusability. After I was half way through my game, I understood that I wrote Duplicate code for client and server A lot of times. I wrote same code on C# and same code on JS, just to make it work. Unity offers pretty good multiplayer API on its own.
– Nick
Nov 27 '19 at 6:03
• Ah yes, I see the author's repo is in fact deprecated here: github.com/fpanettieri/unity-socket.io-DEPRECATED/tree/master/… which you don't see when installing from the Asset Store. At this point the library is working fine for me and I'm only sending small packets back and forth a few times per minute across no more than 3 devices so I'll continue using it. Maybe next project I'll try this one: github.com/Oxytocin/websocket-sharp-unity which I believe socket.io is leveraging anyway. Thanks again! Nov 27 '19 at 11:33
• Right, I wrote it in my question back then, that socket io was already deprecated, and this unity asset is actually just a wrapper for Websoscket-sharp itself :)
– Nick
Nov 27 '19 at 14:20