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

Auto re-connect when connection is closed #1546

Closed

Conversation

linmao-song
Copy link

This attempts to fix #1354 by automatically reconnecting every second.

Manually tested with the following steps:
Start up a proxy script that forwards port 9999 to 8546 (Originally by )

export LC_CTYPE=C
export LANG=C

TMP=mktemp -d
BACK=$TMP/pipe.back
SENT=$TMP/pipe.sent
RCVD=$TMP/pipe.rcvd
trap 'rm -rf "$TMP"' EXIT
mkfifo -m 0600 "$BACK" "$SENT" "$RCVD"
sed 's/^/ => /' <"$SENT" &
sed 's/^/<= /' <"$RCVD" &
nc -l 127.0.0.1 "$SRC_PORT" <"$BACK" | tee "$SENT" | nc "$DST_HOST" "$DST_PORT" | tee "$RCVD" >"$BACK"

Run the following
node
> const Web3 = require('web3');
> const web3 = new Web3('ws://localhost:9999')
> web3.eth.getBlock('latest').then(console.log)
This should print out the latest block. Now kill the above proxy script. Re-do:
> web3.eth.getBlock('latest').then(console.log)

This should print
> (node:48280) UnhandledPromiseRejectionWarning: Unhandled promise rejection (rejection id: 1): Error: connection not open

Now start up the same proxy again and repeat
> web3.eth.getBlock('latest').then(console.log)

This will print out the latest block again.

Unfortunately, it doesn't seem easy to add a unit test case for this.

@frozeman
Copy link
Contributor

frozeman commented Apr 13, 2018

In order to create tests you would need to create stubs for the ws.connect function and fire ws.onclose.
Does it still works when you try to call a function multiple times, but the connection is not active again?
Will .onclose be also called if the connection couldn't be successfully established?

@linmao-song linmao-song closed this May 4, 2018
@linmao-song linmao-song deleted the websocket_auto_reconnect_upon_close branch May 4, 2018 19:07
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

Successfully merging this pull request may close these issues.

Handle web socket disconnects
2 participants