joewalnes / reconnecting-websocket

A small decorator for the JavaScript WebSocket API that automatically reconnects
MIT License
4.22k stars 968 forks source link

How to manually reconnect? #20

Open Jakobud opened 10 years ago

Jakobud commented 10 years ago

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.

yovchev commented 9 years ago

just use .refresh()

vgoklani commented 9 years ago

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

Jakobud commented 9 years ago

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.

vgoklani commented 9 years ago

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

Jakobud commented 9 years ago

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.

nathanboktae commented 7 years ago

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.