websocket-sharp
websocket-sharp copied to clipboard
Delay?
I am using websocket-sharp with Unity3D for a multiplayer game, but every couple seconds I am experiencing a delay where the sockets aren't being received. It is just a bit of lag every couple of seconds, but very noticeable. I also made a pure web client for this game in Javascript where I do not have this problem. Has anyone experienced this issue?
I got this too perhaps we have to add event handler from another thread??
I am happy to hear that I am not the only one with this issue, we will solve it together! I am running the Websocket connection on a separate thread, I loosely modeled my code after the code in this asset (except I dont have the separate thread for the ping because I dont know what that's for), maybe the issue may be the socket thread blocking the main thread ever so slightly?
@deniskrop nvm, it was my problem. mine seemed fixed. thx anyways
@DefinitlyEvil What was the issue that caused it? Maybe I'm doing the same?
@deniskrop I did too much job in the main thread haha not directly caused by WebSocket.
still got that problem when Internet lags, Unity engine stucked (my friend told me)
@DefinitlyEvil If you don't mind, can you show me the code on how you are queueing incoming messages in order to not block the main thread? I have been trying to get this solved for hours :(
@deniskrop sry but I haven't still solve this problem yet. I used C# built-in ThreadPool but that still generate lag when waiting for data received.
Check if you have any thread with a infinity loop inside and its without and thread.sleep? i mean if u have something like this: while (true) { SomeAction(); }
if yes then try to add some delay like this: while (true) { SomeAction(); Thread.Sleep(2); //Adjust it, its best to have like 10 ms, but i dont know your app and what u do inside loops }
@Slyb00ts ik about this, and I don't have such code, I am using Unity3D btw.
problem found, I send byte[]
message almost every frame (position update) used this in my function to send message:
// .... encode message
ws.SendAsync(data, (v) => {});
it seems that SendAsync()
caused the lag.
@DefinitlyEvil I have a similar scenario, did you change it to just ws.Send and it worked? Or what
@deniskrop Nah, I switched to my custom protocol using original C# socket API but using NetworkStream for buffer handling.
If anyone interested, I had another problem with delays: I called ws.IsAlive
too often. I didn't know that IsAlive
internally sends ping (synchronously!!) to check if connection is alive, and using it inside message-sending loop was a bad idea. (To @sta : I think such unobvious thing should be documented in method comments)
Actyally, I just was looking for the way to check if connection is established and not closed. For this, I should have used ws.ReadyState == WebSocketState.Open
instead.
@NIA thx for the info!! ;D (tho I switched to custom protocol)
Big thanks to @NIA for the suggestion to not use IsAlive as a frequent monitor of the socket. I was experience disconnects until I read the comment above and converted to using ReadyState instead. I am really bothered by the way that the documentation for this library is out of date. I wonder how many people were attracted to it like me because of it's seemingly high mileage, but quickly turned off by out of date documentation
@GingerLoaf @NIA isn't threading not allowed? I am running the code while (true) { // listening....
}
However it gets stuck here.
I test it in Unity .For webgl i use javascript (JSLIB) plugin wich use websocket in browser, but an PC version websocket sharp work at 2 time slower (200ms more slower for me)
Did dome body find answerd why it can be so ?
it look like problem all c# websocket's plugin....look like some problem in c#
I found - _tcpClient.NoDelayneed to be true in Websocket.cs
https://learn.microsoft.com/en-us/dotnet/api/system.net.sockets.tcpclient.nodelay?view=netframework-2.0#system-net-sockets-tcpclient-nodelay