【问题标题】:Issues with node.js socket keep reconnectingnode.js 套接字的问题不断重新连接
【发布时间】:2026-01-20 18:35:01
【问题描述】:

我在使用 socket.io 时遇到了一些意外行为,我正在经历无提示的重新连接

这是我的前端,由 index.html 引用

$(function(){
    var socket = io.connect('https://localhost:3005', {'flash policy port':3005} );
});

这是我的后台

exports.listen = function(server){

    io = socketio.listen(server);
    io.set('log level', 2);

    io.set('flash policy port', 3005); 
    io.set('transports', [                     
        'websocket'
      , 'flashsocket'
      , 'htmlfile'
      , 'xhr-polling'
      , 'jsonp-polling'
    ]);

    io.sockets.on('connection', function(socket){
        console.log("This is the socket id");
        console.log(socket.id);
    });
}

如果我在浏览器上什么都不做,这是输出

This is the socket id
 gxn4mxnG9xOoVCLAoMPN
   warn  - websocket parser error: reserved fields must be empty
   info  - transport end (undefined)
   info  - handshake authorized 260xpHzgrsHd8UnNoMPO
This is the socket id
260xpHzgrsHd8UnNoMPO
   warn  - websocket parser error: reserved fields must be empty
   info  - transport end (undefined)
   info  - handshake authorized cMCv6BS-iLCQbmv6oMPP

它将保持连接并断开连接并重新连接,直到我关闭服务器

我不确定它为什么会出现这种行为,因为我的印象是,如果您停留在同一页面上,socket.io 将不会重新连接。

任何建议将不胜感激

【问题讨论】:

    标签: node.js sockets websocket socket.io


    【解决方案1】:

    通过卸载 Node v0.10.9 并安装最新版本修复

    有关卸载节点的信息,请谨慎参考以下链接

    http://notepad2.blogspot.com/2012/06/how-to-uninstall-nodejs-on-mac-os-x.html

    用于重新安装节点

    请检查 brew 或 macport

    另外记得在安装之前更新你的 brew/macport

    【讨论】: