socketio / engine.io-client

The engine used in the Socket.IO JavaScript client, which manages the low-level transports such as HTTP long-polling, WebSocket and WebTransport.
https://socket.io
744 stars 355 forks source link

Error in Request.cleanup #472

Closed moxious closed 8 years ago

moxious commented 8 years ago

Working with socket.io-client I'm seeing some errors from inside of engine.io-client as below, which seems to relate to problems in the polling-xhr transport, and Request.cleanup.

Node 5.11.0, socket.io.client version 1.4.6, which implicates engine.io.client version 1.6.9.

Stack trace:

Could not create socket.io client: TypeError: Cannot set property 'NaN' of undefined
/Users/mda/sw/toolbox/node_modules/engine.io-client/lib/transports/polling-xhr.js:324
    delete Request.requests[this.index];
                                ^

TypeError: Cannot convert undefined or null to object
    at Request.cleanup (/Users/mda/sw/toolbox/node_modules/engine.io-client/lib/transports/polling-xhr.js:324:33)
    at Request.onSuccess (/Users/mda/sw/toolbox/node_modules/engine.io-client/lib/transports/polling-xhr.js:275:8)
    at Request.onData (/Users/mda/sw/toolbox/node_modules/engine.io-client/lib/transports/polling-xhr.js:286:8)
    at Request.onLoad (/Users/mda/sw/toolbox/node_modules/engine.io-client/lib/transports/polling-xhr.js:366:10)
    at XMLHttpRequest.xhr.onreadystatechange (/Users/mda/sw/toolbox/node_modules/engine.io-client/lib/transports/polling-xhr.js:238:16)
    at XMLHttpRequest.dispatchEvent (/Users/mda/sw/toolbox/node_modules/xmlhttprequest-ssl/lib/XMLHttpRequest.js:617:25)
    at XMLHttpRequest.setState (/Users/mda/sw/toolbox/node_modules/xmlhttprequest-ssl/lib/XMLHttpRequest.js:636:14)
    at IncomingMessage.<anonymous> (/Users/mda/sw/toolbox/node_modules/xmlhttprequest-ssl/lib/XMLHttpRequest.js:474:13)
    at emitNone (events.js:85:20)
    at IncomingMessage.emit (events.js:179:7)
    at endReadableNT (_stream_readable.js:913:12)
    at _combinedTickCallback (internal/process/next_tick.js:74:11)
    at process._tickCallback (internal/process/next_tick.js:98:9)

The code which triggers this is very simple (occurs when the defaultSocketClient function below is called)

import io from 'socket.io-client';
import config from './index';
import client from './client';

const getSocketClient = () => {
    return client.getToken()
        .then(token => {
            console.log('Endpoint=' + config.endpoint + ' TOKEN=' + token);
            var socket = io.connect(config.endpoint, {
                query: 'token=' + token
            });

            return socket;
        })
        .catch(err => {
            console.error('Could not create socket.io client: ' + err);
        })
};

var defaultSocketClient = (channels) => {
    channels = (channels || ['dataquality']);

    return getSocketClient()
        .then(c => {

            c.on('connect', () => {
                console.log('Socket.io client connected.');
            });

            c.on('subscribe', (channel, count) => {
                console.log('Listening on ' + channel + '/' + count);
            });

            c.on('message', (channel, message) => {
                console.log('Heard on channel ' + channel + ': ' +
                    JSON.stringify(message));
            });

            for(var channel of channels) {
                c.subscribe(channel);
            }

            return c;
        });
}

var exports = {
    getSocketClient,
    defaultSocketClient
};

export default exports;
darrachequesne commented 8 years ago

Fixed by https://github.com/socketio/engine.io-client/pull/490