websocket-sharp icon indicating copy to clipboard operation
websocket-sharp copied to clipboard

Delay?

Open 253153 opened this issue 8 years ago • 20 comments

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?

253153 avatar Feb 14 '17 02:02 253153

I got this too perhaps we have to add event handler from another thread??

DefinitlyEvil avatar Feb 15 '17 16:02 DefinitlyEvil

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?

253153 avatar Feb 17 '17 02:02 253153

@deniskrop nvm, it was my problem. mine seemed fixed. thx anyways

DefinitlyEvil avatar Feb 17 '17 04:02 DefinitlyEvil

@DefinitlyEvil What was the issue that caused it? Maybe I'm doing the same?

253153 avatar Feb 17 '17 05:02 253153

@deniskrop I did too much job in the main thread haha not directly caused by WebSocket.

DefinitlyEvil avatar Feb 17 '17 05:02 DefinitlyEvil

still got that problem when Internet lags, Unity engine stucked (my friend told me)

DefinitlyEvil avatar Feb 17 '17 05:02 DefinitlyEvil

@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 :(

253153 avatar Feb 17 '17 07:02 253153

@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.

DefinitlyEvil avatar Feb 17 '17 12:02 DefinitlyEvil

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 avatar Feb 19 '17 08:02 Slyb00ts

@Slyb00ts ik about this, and I don't have such code, I am using Unity3D btw.

DefinitlyEvil avatar Feb 19 '17 13:02 DefinitlyEvil

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 avatar Feb 24 '17 05:02 DefinitlyEvil

@DefinitlyEvil I have a similar scenario, did you change it to just ws.Send and it worked? Or what

253153 avatar Mar 04 '17 03:03 253153

@deniskrop Nah, I switched to my custom protocol using original C# socket API but using NetworkStream for buffer handling.

DefinitlyEvil avatar Mar 04 '17 15:03 DefinitlyEvil

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 avatar Mar 22 '17 07:03 NIA

@NIA thx for the info!! ;D (tho I switched to custom protocol)

DefinitlyEvil avatar Mar 23 '17 11:03 DefinitlyEvil

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 avatar May 13 '19 21:05 GingerLoaf

@GingerLoaf @NIA isn't threading not allowed? I am running the code while (true) { // listening....

}

However it gets stuck here.

moizalidv avatar Oct 22 '19 13:10 moizalidv

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 ?

webrobot1 avatar Nov 03 '22 01:11 webrobot1

it look like problem all c# websocket's plugin....look like some problem in c#

webrobot1 avatar Nov 03 '22 15:11 webrobot1

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

webrobot1 avatar Nov 04 '22 16:11 webrobot1