reconnecting-websocket
reconnecting-websocket copied to clipboard
How to manually reconnect?
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.
just use .refresh()
@Jakobud - i have the same problem. How do I reconnect after my machine wakes up from sleep. Is there a specific websocket state? onsleep() ?
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.
manually reconnecting is fine, but what state should i reference? i.e. is there an onWakeUp() ? How do I trigger the callback?
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.
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.