Real time C# websockets client to use with Unity3D?
Asked Answered
I

3

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?

Instructions answered 12/3, 2017 at 21:52 Comment(4)
Impossible to say anything without seeing any code nor what data or frg is or how they're sent or whereShifflett
@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 issueInstructions
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 behaviorDriving
@danielmetlitski I actually made a tutorial about how to use websockets with Unity/C# here github.com/pudility/unityWSBouldon
S
1

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

Studley answered 29/3, 2023 at 21:27 Comment(0)
S
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.

Spinous answered 9/10, 2022 at 9:35 Comment(0)
L
-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

Liberec answered 13/3, 2017 at 9:4 Comment(1)
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.Wards

© 2022 - 2024 — McMap. All rights reserved.