0

我已经为此工作了好几天,并且使问题变得尽可能简单。

我只有一个按钮,它向我的 node.js 服务器发送一个命令,它只是 console.logs 短语“测试”,并且还向客户端发出一个信号以运行一个函数。

问题是客户端正在断开连接?!但是,它不会在第二次尝试时断开连接。

客户:

<button onclick="test()">Login</button>

var socket  = io.connect('http://54.213.92.113:8080');

function test(){
                    socket.emit('test');
                    }

socket.on('conf', function () {
                    alert("testing worked");
                    }

服务器:

var io = require('socket.io').listen(app);
io.sockets.on('connection', function(client) {

        client.on('test', function(){
        client.emit('conf);
        console.log("testing");
        });
});

这是我的日志。同样,代码似乎在第二次尝试时有效:

第一次尝试:

info  - socket.io started
   debug - served static content /socket.io.js
   debug - client authorized
   info  - handshake authorized Egh72p-hOLFSpLh2CS7m
   debug - setting request GET /socket.io/1/websocket/Egh72p-hOLFSpLh2CS7m
   debug - set heartbeat interval for client Egh72p-hOLFSpLh2CS7m
   debug - client authorized for 
   debug - websocket writing 1::
   debug - websocket writing 5:::{"name":"conf"}
testing
   info  - transport end (socket end)
   debug - set close timeout for client Egh72p-hOLFSpLh2CS7m
   debug - cleared close timeout for client Egh72p-hOLFSpLh2CS7m
   debug - cleared heartbeat interval for client Egh72p-hOLFSpLh2CS7m
   debug - discarding transport
   debug - served static content /socket.io.js
   debug - client authorized
   info  - handshake authorized PCzh1CS-tze49XHaCS7n
   debug - setting request GET /socket.io/1/websocket/PCzh1CS-tze49XHaCS7n
   debug - set heartbeat interval for client PCzh1CS-tze49XHaCS7n
   debug - client authorized for 
   debug - websocket writing 1::

第二次尝试后:

 info  - socket.io started
   debug - served static content /socket.io.js
   debug - client authorized
   info  - handshake authorized Egh72p-hOLFSpLh2CS7m
   debug - setting request GET /socket.io/1/websocket/Egh72p-hOLFSpLh2CS7m
   debug - set heartbeat interval for client Egh72p-hOLFSpLh2CS7m
   debug - client authorized for 
   debug - websocket writing 1::
   debug - websocket writing 5:::{"name":"conf"}
testing
   info  - transport end (socket end)
   debug - set close timeout for client Egh72p-hOLFSpLh2CS7m
   debug - cleared close timeout for client Egh72p-hOLFSpLh2CS7m
   debug - cleared heartbeat interval for client Egh72p-hOLFSpLh2CS7m
   debug - discarding transport
   debug - served static content /socket.io.js
   debug - client authorized
   info  - handshake authorized PCzh1CS-tze49XHaCS7n
   debug - setting request GET /socket.io/1/websocket/PCzh1CS-tze49XHaCS7n
   debug - set heartbeat interval for client PCzh1CS-tze49XHaCS7n
   debug - client authorized for 
   debug - websocket writing 1::
   debug - websocket writing 5:::{"name":"conf"}
testing

我已经研究了一段时间,但完全无处可去。有谁知道为什么这段代码在第一次尝试时不起作用?- 警报仅在第一次尝试后出现。

4

1 回答 1

0

我在这里找到了解决方案:socket.io client connection cannot be made on the 2nd time

// in client
io.connect('host:port', { 'force new connection': true });
于 2013-09-29T05:34:56.753 回答