Last active
December 15, 2015 18:49
-
-
Save zivester/5306901 to your computer and use it in GitHub Desktop.
socket.io-client not retrying indefinitely
This file contains hidden or bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
| var events = require('events'), | |
| util = require('util'), | |
| io = require('socket.io-client'), | |
| url = "ws://localhost:12345", // intentionally an unreachable URL | |
| socketOptions = { | |
| "transports" : [ "websocket" ], | |
| "try multiple transports" : false, | |
| "reconnect" : false, | |
| "connect timeout" : 5000 | |
| }; | |
| // The goal is to have this socket attempt to connect forever | |
| // I would like to do it without the built in reconnects, as these | |
| // are somewhat unreliable (reconnect* events not always firing) | |
| function Test(){ | |
| var self = this; | |
| events.EventEmitter.call(self); | |
| var socket; | |
| function reconnect(){ | |
| setTimeout(go, 1000); | |
| } | |
| function go(){ | |
| console.log("connecting to", url, socketOptions); | |
| socket = io.connect(url, socketOptions); | |
| socket.on('connect', function(){ | |
| console.log("connected! wat."); | |
| }); | |
| socket.on('error', function(err){ | |
| console.log("socket.io-client 'error'", err); | |
| reconnect(); | |
| }); | |
| socket.on('connect_failed', function(){ | |
| console.log("socket.io-client 'connect_failed'"); | |
| reconnect(); | |
| }); | |
| socket.on('disconnect', function(){ | |
| console.log("socket.io-client 'disconnect'"); | |
| reconnect(); | |
| }); | |
| } | |
| go(); | |
| } | |
| util.inherits(Test, events.EventEmitter); | |
| var test = new Test(); | |
| process.on('exit', function(){ | |
| console.log("this should never end"); | |
| }); |
Sign up for free
to join this conversation on GitHub.
Already have an account?
Sign in to comment