James-Frowen / SimpleWebTransport

WebSocket Server and client for unity designed for Mirror Networking
MIT License
62 stars 18 forks source link

Connecting to a simplewebserver via alternative client solution? #9

Closed ZeoWorks closed 1 year ago

ZeoWorks commented 1 year ago

Hi, I'm currently connecting to the simplewebserver via this solution; https://github.com/endel/NativeWebSocket

It works great, however after around 30 seconds the server disconnects. To clarify, it seems like the simplewebserver is auto-kicking the client after 30 seconds.

Any reasons why you think this may be?

-Sean

James-Frowen commented 1 year ago

it disconnects connection if there are no messages within the receiveTimeout. you can either increase that timeout or send messages more often.

For example when SimpleWebTransport is used with Mirror, Mirror will send a Ping/Pong messages every few seconds (to keep time sync), and this also keeps the connections alive.

ZeoWorks commented 1 year ago

it disconnects connection if there are no messages within the receiveTimeout. you can either increase that timeout or send messages more often.

For example when SimpleWebTransport is used with Mirror, Mirror will send a Ping/Pong messages every few seconds (to keep time sync), and this also keeps the connections alive.

Hi, I am sending a message every second but it still disconnects after 30 seconds (receive timeout is set to 5000). When we don't send a message every second, it disconnects after 5 seconds as expected. A simple byte[1]{0}. Thanks!

James-Frowen commented 1 year ago

Are you sending that 1 bytes message to/from both server and client? it is possible that NativeWebSocket also has a timeout.

If that isn't it you might need to enable logging to try find out what is going wrong

ZeoWorks commented 1 year ago

Hi, Yes it is being sent to and from the server. NativeWebSocket doesn't actually have a timeout, I've tested other node-based websocket servers without issue.

Upon enabling the debugger, we found this error occurs prior to disconnect after 30 seconds; ERROR: Invalid data from [Conn:1, endPoint:[::1]:63478]: Unexpected opcode 9 image

James-Frowen commented 1 year ago

I think that is the ping opcode, SimpleWebTransport current doesn't have the ping/pong opcodes implemented, most websocket clients dont bother sending ping opcode so I havn't gotten around to adding it.

Is there a way to disable NativeWebSocket from sending the ping?

James-Frowen commented 1 year ago

Looking at NativeWebSocket it looks like it uses a similar jslib to SimpleWebTransport, so I assume it is the c# ClientWebSocket sending it. looking at the c# code, setting this should stop it sending ping clientWebSocket.Options.KeepAliveInterval = TimeSpan.Zero;

ZeoWorks commented 1 year ago

Oh I believe you are correct, thanks!

m_Socket.Options.KeepAliveInterval =TimeSpan.Zero;

This did the trick since NativeWebSocket is simply System.Net.Sockets; Resolved.