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

How to manually reconnect?

Open Jakobud opened this issue 10 years ago • 6 comments

Lets say I open a websocket to a site. Soon after, my computer goes to sleep. When waking my computer up, I'm no longer connected to the websocket. But the library obviously doesn't know I need to reconnect since an onclose event never got a chance to run. There doesn't seem to be a way for me to manually reconnect. Also there doesn't seem to be a way to tell if I am actually still connected (not sure if that is even possible).

So in order to reconnect I have to remake the ReconnectingWebsocket AND redefine all my onopen, onmessage, etc functions, which is a bummer. There should just be a .reconnect() or .connect() function that recreates the Websocket inside the library for you so you don't have to redefine everything.

Jakobud avatar Aug 02 '14 17:08 Jakobud

just use .refresh()

yovchev avatar Nov 12 '14 23:11 yovchev

@Jakobud - i have the same problem. How do I reconnect after my machine wakes up from sleep. Is there a specific websocket state? onsleep() ?

vgoklani avatar Apr 20 '15 20:04 vgoklani

I've contributed changes to the source for this project that allows you to manually reconnect. Take a look at the updated doc's and source.

Jakobud avatar Apr 20 '15 20:04 Jakobud

manually reconnecting is fine, but what state should i reference? i.e. is there an onWakeUp() ? How do I trigger the callback?

vgoklani avatar Apr 20 '15 20:04 vgoklani

Do you mean what callback are you looking for when a computer wakes back up? That is something you'd have to figure out on the browser level I think. This library doesn't contain anything that is called when a computer wakes up from sleep. I would personally suggest looking into the PageVisibility API.

Jakobud avatar Apr 20 '15 21:04 Jakobud

robust-websocket a library with a similar API and is aware of HTML5 offline/online events natively (which are triggered on wakeup as you were asking @vgoklani ) - as well as a way to manually open and close the socket again. Check it out.

nathanboktae avatar Oct 14 '16 05:10 nathanboktae