0

我正在尝试从中收集数据的服务器(任何感兴趣的人都可以使用mtgox websockets )。我遇到的麻烦是,有时我连接并开始从服务器接收数据,而其他时候我连接并且没有任何反应。在这两种情况下都会触发连接事件。我不知道为什么会这样。它不依赖于代码,它是否有效似乎完全随机。

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

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

0 回答 0