Skip to content
New issue

Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.

By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.

Already on GitHub? Sign in to your account

WebSocketConnect in reconnect case #84

Open
VarnavaA opened this issue Jun 13, 2023 · 2 comments
Open

WebSocketConnect in reconnect case #84

VarnavaA opened this issue Jun 13, 2023 · 2 comments

Comments

@VarnavaA
Copy link

VarnavaA commented Jun 13, 2023

Hi @endel
Thank you for your great work
I have tryed to resolve issue with reconnect socket that was close before
In jslib there is method WebSocketConnect
and in this method we have condition
if (instance.ws !== null) {...}
but if we called close socket before, we deleted instance.ws

instance.ws.onclose = function (ev) {
     if (webSocketState.onClose)
         Module.dynCall_vii(webSocketState.onClose, instanceId, ev.code);
     delete instance.ws;
};

then I try to connect web socket again and instance.ws !== null will be always true
in this case "instance.ws" will be 'undefined', so the better condition will be '!=' not '!=='
Does it make sense?
Thank you

@LittleNyanCat
Copy link

LittleNyanCat commented Oct 18, 2023

You can easily automatically reconnected by checking the socket status and attempting to connect if it is equal to WebSocketState.Closed in a loop. (Eg. in the Update() method or with InvokeRepeating().

void TryReconnect() { if (ws.State == WebSocketState.Closed) { ws.Connect(); } }

Edit: This worked fine on my editor, but doesn't seem to work on my Quest 2. I have to restart the app every time the websocket connection closes, even if I try to create a new websocket instance.

@LinaEagle
Copy link

LinaEagle commented Jul 5, 2024

This approach
void TryReconnect() { if (ws.State == WebSocketState.Closed) { ws.Connect(); } }
doesn't seems to work neither in Editor nor in build in my case.

I found that full deleting of WebSocket object, "unreferencing" and recreating it and reconnecting works in my case.

I also noticed that connection can be interrupted by OnApplicationFocus(false), so in this case reconecting on OnApplicationFocus(true) helps.

Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment
Labels
None yet
Projects
None yet
Development

No branches or pull requests

3 participants