Node.js 使用socket.io持久化websocket连接

Node.js 使用socket.io持久化websocket连接,node.js,coffeescript,websocket,socket.io,Node.js,Coffeescript,Websocket,Socket.io,有一个服务器(对于任何感兴趣的人)我正试图从中收集数据。我遇到的问题是,有时我连接并开始从服务器接收数据,而有时我连接却什么也没发生。在这两个实例中都会触发连接事件。我不知道为什么会这样。这并不取决于代码,它是否工作似乎完全是随机的 为了解决这个问题,我决定坚持我的套接字连接。如果数据在几秒钟内没有开始流动,那么我想再次尝试连接。应重复此操作,直到建立真正的连接 events = require 'events' io = require 'socket.io-client' colors =

有一个服务器(对于任何感兴趣的人)我正试图从中收集数据。我遇到的问题是,有时我连接并开始从服务器接收数据,而有时我连接却什么也没发生。在这两个实例中都会触发连接事件。我不知道为什么会这样。这并不取决于代码,它是否工作似乎完全是随机的

为了解决这个问题,我决定坚持我的套接字连接。如果数据在几秒钟内没有开始流动,那么我想再次尝试连接。应重复此操作,直到建立真正的连接

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)
此方法不起作用,因为我将
@socket
This.socket
)重置为
io.connect('https://socketio.mtgox.com/mtgox)
连接事件不会再次触发。如果第一个套接字连接失败,如何重新建立套接字连接


注意:工作套接字对象和损坏的套接字对象(不发送数据的套接字对象)之间似乎没有任何区别,但这里记录了一个损坏的套接字对象

{ 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] } }

你看过《断开连接》吗@jcollum我试过了,它成功了,但当我重新连接时,我的意思是调用
@socket=io.connect('https://socketio.mtgox.com/mtgox)
连接事件再也没有被触发。虽然我会再次断开,断开就会开火。