Skip to content
New issue

Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.

By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.

Already on GitHub? Sign in to your account

Connect working only if run code from Node repl #1266

Closed
1 of 2 tasks
pavel-karpovich opened this issue Jan 22, 2019 · 1 comment
Closed
1 of 2 tasks

Connect working only if run code from Node repl #1266

pavel-karpovich opened this issue Jan 22, 2019 · 1 comment

Comments

@pavel-karpovich
Copy link

pavel-karpovich commented Jan 22, 2019

You want to:

  • report a bug
  • request a feature

Current behaviour

The client does not connect to the server if the code is run from a file, and connects if you run the same code from the repl.

Steps to reproduce (if the current behaviour is a bug)

Code for Server:

const http = require("http");
const express = require("express");
const socketIO = require("socket.io");
const app = express();
const server = http.Server(app);
const io = socketIO(server);
io.on("connection", function(socket) {
    console.log("Wow! Client socket connected!");
});
server.listen(8081, "0.0.0.0", function() {
    console.log("Starting server on port 8081");
});

Code for Client:

const socketIOClient = require("socket.io-client");
const socket = socketIOClient("http://0.0.0.0:8081");
while (true);

Expected behaviour

Of course, I expect it to work correctly in both scenarios.

Setup

  • OS: Windows 10 1809 and Amazon Linux AMI
  • browser: without
  • socket.io version: 2.2.0,

Other information (e.g. stacktraces, related issues, suggestions how to fix)

I am confused with this behavior...

Here is the trace of the SocketIO object. The first is when try run node client. The readyState becomes opening, connected false and disconnected true.

Socket {
io:
Manager {
    nsps: { '/sandbox': [Circular] },
    subs: [ [Object], [Object], [Object] ],
    opts:
    { path: '/socket.io',
        hostname: '0.0.0.0',
        secure: false,
        port: '8081' },
    _reconnection: true,
    _reconnectionAttempts: Infinity,
    _reconnectionDelay: 1000,
    _reconnectionDelayMax: 5000,
    _randomizationFactor: 0.5,
    backoff:
    Backoff { ms: 1000, max: 5000, factor: 2, jitter: 0.5, attempts: 0 },
    _timeout: 20000,
    readyState: 'opening',
    uri: 'http://0.0.0.0:8081/sandbox',
    connecting: [ [Circular] ],
    lastPing: null,
    encoding: false,
    packetBuffer: [],
    encoder: Encoder {},
    decoder: Decoder { reconstructor: null },
    autoConnect: true,
    engine:
    Socket {
        secure: false,
        agent: false,
        hostname: '0.0.0.0',
        port: '8081',
        query: {},
        upgrade: true,
        path: '/socket.io/',
        forceJSONP: false,
        jsonp: true,
        forceBase64: false,
        enablesXDR: false,
        timestampParam: 't',
        timestampRequests: undefined,
        transports: [Array],
        transportOptions: {},
        readyState: 'opening',
        writeBuffer: [],
        prevBufferLen: 0,
        policyPort: 843,
        rememberUpgrade: false,
        binaryType: null,
        onlyBinaryUpgrades: undefined,
        perMessageDeflate: [Object],
        pfx: null,
        key: null,
        passphrase: null,
        cert: null,
        ca: null,
        ciphers: null,
        rejectUnauthorized: true,
        forceNode: false,
        isReactNative: false,
        id: null,
        upgrades: null,
        pingInterval: null,
        pingTimeout: null,
        pingIntervalTimer: null,
        pingTimeoutTimer: null,
        transport: [XHR],
        _callbacks: [Object] },
    skipReconnect: false,
    _callbacks: { '$open': [Array], '$packet': [Array], '$close': [Array] } },
nsp: '/sandbox',
json: [Circular],
ids: 0,
acks: {},
receiveBuffer: [],
sendBuffer: [],
connected: false,
disconnected: true,
flags: {},
subs:
[ { destroy: [Function: destroy] },
    { destroy: [Function: destroy] },
    { destroy: [Function: destroy] } ],
_callbacks:
{ '$connecting': [ [Function: onConnecting] ],
    '$connect': [ [Function] ] } }

And second output, when you first run a node repl with node command. And then paste the same code to execute and all works:

Socket {
io:
Manager {
    nsps: { '/sandbox': [Circular] },
    subs:
    [ [Object], [Object], [Object], [Object], [Object], [Object] ],
    opts:
    { path: '/socket.io',
        hostname: '0.0.0.0',
        secure: false,
        port: '8081' },
    _reconnection: true,
    _reconnectionAttempts: Infinity,
    _reconnectionDelay: 1000,
    _reconnectionDelayMax: 5000,
    _randomizationFactor: 0.5,
    backoff:
    Backoff { ms: 1000, max: 5000, factor: 2, jitter: 0.5, attempts: 0 },
    _timeout: 20000,
    readyState: 'open',
    uri: 'http://0.0.0.0:8081/sandbox',
    connecting: [ [Circular] ],
    lastPing: null,
    encoding: false,
    packetBuffer: [],
    encoder: Encoder {},
    decoder: Decoder { reconstructor: null, _callbacks: [Object] },
    autoConnect: true,
    engine:
    Socket {
        secure: false,
        agent: false,
        hostname: '0.0.0.0',
        port: '8081',
        query: {},
        upgrade: true,
        path: '/socket.io/',
        forceJSONP: false,
        jsonp: true,
        forceBase64: false,
        enablesXDR: false,
        timestampParam: 't',
        timestampRequests: undefined,
        transports: [Array],
        transportOptions: {},
        readyState: 'open',
        writeBuffer: [],
        prevBufferLen: 0,
        policyPort: 843,
        rememberUpgrade: false,
        binaryType: null,
        onlyBinaryUpgrades: undefined,
        perMessageDeflate: [Object],
        pfx: null,
        key: null,
        passphrase: null,
        cert: null,
        ca: null,
        ciphers: null,
        rejectUnauthorized: true,
        forceNode: false,
        isReactNative: false,
        id: 'yKzSX2994gDxQs49AAAE',
        upgrades: [Array],
        pingInterval: 25000,
        pingTimeout: 5000,
        pingIntervalTimer:
        Timeout {
        _called: false,
        _idleTimeout: 25000,
        _idlePrev: [TimersList],
        _idleNext: [TimersList],
        _idleStart: 59120,
        _onTimeout: [Function],
        _timerArgs: undefined,
        _repeat: null,
        _destroyed: false,
        domain: [Domain],
        [Symbol(unrefed)]: false,
        [Symbol(asyncId)]: 615,
        [Symbol(triggerId)]: 604 },
        pingTimeoutTimer:
        Timeout {
        _called: false,
        _idleTimeout: 30000,
        _idlePrev: [TimersList],
        _idleNext: [TimersList],
        _idleStart: 59299,
        _onTimeout: [Function],
        _timerArgs: undefined,
        _repeat: null,
        _destroyed: false,
        domain: [Domain],
        [Symbol(unrefed)]: false,
        [Symbol(asyncId)]: 684,
        [Symbol(triggerId)]: 679 },
        transport: [WS],
        _callbacks: [Object],
        upgrading: false },
    skipReconnect: false,
    _callbacks: { '$open': [Array], '$packet': [Array], '$close': [Array] } },
nsp: '/sandbox',
json: [Circular],
ids: 0,
acks: {},
receiveBuffer: [],
sendBuffer: [],
connected: true,
disconnected: false,
flags: {},
subs:
[ { destroy: [Function: destroy] },
    { destroy: [Function: destroy] },
    { destroy: [Function: destroy] } ],
_callbacks:
{ '$connecting': [ [Function: onConnecting] ],
    '$connect': [ [Function] ] },
id: '/sandbox#yKzSX2994gDxQs49AAAE' }
@pavel-karpovich pavel-karpovich changed the title Connect working only if run code from node interpreter Connect working only if run code from Node repl Jan 23, 2019
@pavel-karpovich
Copy link
Author

I figured it out...
This is because of my infinite loop and poor understanding of the js runtime ...
I thought the script should complete when the execution reached the end of the file, and wrote a loop to prevent this, but it seems instead I blocked the JS event loop.

Without loop all works fine. Sorry for trouble. :X

Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment
Labels
None yet
Projects
None yet
Development

No branches or pull requests

1 participant