仅当从 Node repl 运行代码时,Socket.IO 连接才有效

Posted

技术标签:

【中文标题】仅当从 Node repl 运行代码时,Socket.IO 连接才有效【英文标题】:Socket.IO connect working only if run code from Node repl 【发布时间】:2019-01-23 19:48:45 【问题描述】:

如果代码从文件运行,客户端不会连接到服务器,如果您从 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 变为打开,连接为假,断开为真。

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 版本、不同的操作系统、各种可选参数,但没有任何帮助。

【问题讨论】:

您是否尝试连接到本地主机? (我假设您正在尝试在同一主机内连接: const socket = socketIOClient("127.0.0.1:8081"); 你在代理后面吗? 【参考方案1】:

我想通了……

这是因为我的无限循环和对js运行时的理解不够...

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

:X

【讨论】:

以上是关于仅当从 Node repl 运行代码时,Socket.IO 连接才有效的主要内容,如果未能解决你的问题,请参考以下文章

第二节:Node.js中的交互式运行环境——REPL

仅当从下拉列表中选择选项时才附加到 iframe src

Node.js权威指南 - Node.js中的交互式运行环境——REPL

Node.js知识点整理之----REPL

Node.js系列基础学习----安装,实现Hello World, REPL

仅当从警报调用时,如何向第二个向下钻取View Controller添加按钮?