Persisting websocket connection with socket.io
There is a server (mtgox websockets for anyone interested) that I'm trying to collect data from. The trouble I'm having is that sometimes I connect and I begin receiving data from the server and other times I connect and nothing happens. The connection event is fired in both instances. I have no idea why this happens. It does not depend on the code and whether it works or not seems totally random.
To over come this I decided to persist my socket connection. If data does not begin to flow in a couple seconds then I would like to try connecting again. This should repeat until a true connection is established.
events = require 'events'
io = require 'socket.io-client'
colors = require 'colors'
module.exports = class Mtgox extends events.EventEmitter
constructor: () ->
@socket = io.connect('https://socketio.mtgox.com/mtgox')
@connected = false
# establish connection
@connect()
@socket.on('connect', () =>
console.log "connected"
@socket.on('message', (message) =>
console.log "in on message"
@connection = true
@emit(message.private, message[message.private])
)
)
connect: ->
@connected = false
id = setInterval(
() =>
if not @connected
console.log "persisting".blue
# console.log @socket
@socket = io.connect('https://socketio.mtgox.com/mtgox')
else
clearInterval(id)
, 5000)
This method does not work, as when I reset @socket
( this.socket
) to io.connect('https://socketio.mtgox.com/mtgox')
the connection event is not fired again. How can I re-establish a socket connection if the first one fails?
Note: There does not seem to be any difference between a working socket object and a broken one (one that does not send data) but here is a broken one logged.
{ socket:
{ options:
{ port: 443,
secure: true,
document: false,
resource: 'socket.io',
transports: [Object],
'connect timeout': 10000,
'try multiple transports': true,
reconnect: true,
'reconnection delay': 500,
'reconnection limit': Infinity,
'reopen delay': 3000,
'max reconnection attempts': 10,
'sync disconnect on unload': false,
'auto connect': true,
'flash policy port': 10843,
manualFlush: false,
host: 'socketio.mtgox.com',
query: '' },
connected: true,
open: true,
connecting: false,
reconnecting: false,
namespaces: { '/mtgox': [Circular], '': [Object] },
buffer: [],
doBuffer: false,
sessionid: 'ZwxMQD749XUTdgnb1yWu',
closeTimeout: 60000,
heartbeatTimeout: 60000,
origTransports: [ 'websocket' ],
transports: [ 'websocket' ],
heartbeatTimeoutTimer:
{ _idleTimeout: 60000,
_idlePrev: [Object],
_idleNext: [Object],
_onTimeout: [Function],
_idleStart: Sun Sep 01 2013 15:08:47 GMT-0400 (EDT) },
transport:
{ socket: [Circular],
sessid: 'ZwxMQD749XUTdgnb1yWu',
websocket: [Object],
isOpen: true,
closeTimeout: [Object] },
connectTimeoutTimer:
{ _idleTimeout: -1,
_idlePrev: null,
_idleNext: null,
_onTimeout: null,
_idleStart: Sun Sep 01 2013 15:08:46 GMT-0400 (EDT),
ontimeout: null },
'$events': {} },
name: '/mtgox',
flags: {},
json: { namespace: [Circular], name: 'json' },
ackPackets: 0,
acks: {},
'$events':
{ connect: [Function],
disconnect: [Function],
error: [Function],
message: [Function] } }
链接地址: http://www.djcxy.com/p/44738.html