Skip to content

Instantly share code, notes, and snippets.

@whistler
Last active December 20, 2015 02:49
Show Gist options
  • Star 0 You must be signed in to star a gist
  • Fork 0 You must be signed in to fork a gist
  • Save whistler/6059645 to your computer and use it in GitHub Desktop.
Save whistler/6059645 to your computer and use it in GitHub Desktop.
How to use socket.io-client to keep retrying for server or connect to a different server
client = require 'socket.io-client'
socket = null
host = "http://localhost:12345"
connect_client = () ->
console.log('connecting')
socket = client.connect(host, {'force new connection': true})
socket.on('connect', ()->
console.log('connected')
)
socket.on('error', (err)->
reconnect()
)
connect_client()
reconnect = () ->
# optionally change host here
socket.removeAllListeners() if socket
setTimeout(go, 1000)
require 'socket.io'
socketio = require('socket.io').listen(12345, {'log':false})
socketio.on('connection', (socket)->
console.log('hello')
)
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment