Socket.IO连接仅在来自节点repl的运行代码时有效

时间:2019-01-23 19:48:45

标签: javascript node.js socket.io

如果代码是从文件中运行的,则客户端不会连接到服务器;如果是从repl中运行的相同的代码,则连接。

服务器代码:

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");
});

客户端代码(也为node.js):

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

这是创建的Socket对象的跟踪。首先是尝试运行节点客户端时。 readyState变为打开状态,连接为false,断开连接为true。

Socket {
  io:
   Manager {
     nsps: { '/': [Circular] },
     subs: [ [Object], [Object], [Object] ],
     opts:
      { reconnect: true,
        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',
     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,
        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: '/',
  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] ] } }

我尝试了不同的SocketIO版本,不同的操作系统,各种可选参数,但无济于事。

1 个答案:

答案 0 :(得分:0)

我知道了...

这是由于我的无限循环和对js运行时的理解不足...

我以为脚本应该在执行到达文件末尾时完成,并编写了一个防止这种情况的循环,但是似乎我阻止了JS事件循环。

:X