8

I'm building a Unity3D client in C# for a multiplayer game that I made. The server is written in Node.js, and I wrote a web client in Javascript that works well. But I am running into lag issues with my Unity3D client implementation. I am using the websocket-sharp library, but have been unable to use it in the same way as I did on my Javascript client with the HTML5 Websocket class. I believe the issue lies in the way the websocket-sharp library is made, currently each Send() will cause a lock to _forSend, so the data will transmit like this:

data1:frg1

data1:frg2

data2:frg1

data2:frg2

...

But on my working javascript implementation, it looks like this as it is truly multithreaded:

data1:frg1

data2:frg1

data2:frg2

data1:frg2

Therefore I believe this is the cause of my lag with my C# Unity implementation, because the websockets-sharp library behaves like this. Can anyone confirm that this is an issue with the websockets-sharp library or suggest a library that would work?

daniel metlitski
  • 747
  • 3
  • 11
  • 23
  • 3
    Impossible to say anything without seeing any code nor what data or frg is or how they're sent or where – Sami Kuhmonen Mar 12 '17 at 21:56
  • 1
    @SamiKuhmonen I was hoping someone who had worked with websocket-sharp on a similar style of game could advise if they had ran into this issue – daniel metlitski Mar 12 '17 at 21:58
  • not sure what your snippets represent as I don't know the logic of the function that would call `Send` here, but it seems like your looking for `SendAsync` behavior – Brett Caswell Mar 12 '17 at 22:29
  • 1
    @danielmetlitski I actually made a tutorial about how to use websockets with Unity/C# here https://github.com/pudility/unityWS – zoecarver Aug 11 '17 at 16:11

3 Answers3

1

I had lag when using websocket-sharp. canvas UI was freakingly slow.Got solved by using native-websocket https://github.com/endel/NativeWebSocket

NWA saba
  • 21
  • 4
0

you can see my simple SignalR unity client and create simple one for your node.js server, then see if the lag still present or not. https://github.com/Mo-Rahemi/WebGlGameWithSignalRClient

from my experience the lag should happens because of :

  1. if your fps is low then java script calls affects too.as unity is single threaded and Webgl not support C# threads you have to handle incoming update in unity main thread. let say your server send update every 50 ms, so you have 20 java script call per second, so if your fps is under 20 frame per second your game go behind the server.

  2. if your server update rate is too high it would be the problem because of tcp protocol of websocket ... .

  3. interpolation, if your server sends 20 update per second depend on your player move speed you may have jumping issue in game client. so you must first queue them and interpolate between updates smoothly.

  4. as websocket-sharp is a known library i think it shouldn't be problem, perhaps you are using it in bad way but we need see the code sample to help.

Mamad RN
  • 690
  • 9
  • 33
-3

I used Socket.IO for Unity in my project and it work. I implement to create notification for my game example when someone aware a gilf ....

https://www.assetstore.unity3d.com/en/#!/content/21721

Cổ Chí Tâm
  • 330
  • 1
  • 7
  • 3
    Socket.IO and websocket are not the same thing. There is a reason someone chooses one vs the other. Socket.IO has bloat that sometimes is useful, and other times detrimental. – pspahn Apr 06 '17 at 04:34