客户端断开client.emit - Node.js

时间:2013-08-13 19:07:19

标签: javascript node.js cordova socket.io

我已经做了好几天了,并且让问题变得如此简单。

我只有一个按钮,它向我的node.js服务器发送一个命令,它只是console.logs短语'testing',并向客户端发出一个信号来运行一个函数。

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

客户端:

<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

我一直在研究这个问题并且一直无处可去。有谁知道为什么这个代码第一次尝试不起作用? - 警报仅在第一次尝试后出现。

1 个答案:

答案 0 :(得分:0)

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

// in client
io.connect('host:port', { 'force new connection': true });