socket.io给了我奇怪的输出

时间:2014-11-11 06:38:51

标签: angularjs node.js websocket socket.io

当我重新启动我的服务器时,我看到了奇怪的输出..你能澄清我为什么得到这个输出。通过chrome中的开发人员工具中看到网络我发现每次它都在发出请求。我的目标是尽快测试套接字断开,这就是我设置心跳间隔和心跳超时的原因。

我的服务器端代码为===>>>

var app = require('express')();
var http = require('http').Server(app);
var io = require('socket.io')(http);
io.set('heartbeat interval',10);
io.set('heartbeat timeout',25);
io.set('transports',  'xhr-polling');
app.get('/', function(req, res){
  res.sendfile(__dirname + '/index.html');
});
io.on('connection', function(socket){
  var currentdate = new Date(); 
  var datetime = "Last Sync: " + currentdate.getDate() + "/"
                  + (currentdate.getMonth()+1)  + "/" 
                  + currentdate.getFullYear() + " @ "  
                  + currentdate.getHours() + ":"  
                  + currentdate.getMinutes() + ":" 
                  + currentdate.getSeconds();
  console.log("connection established...!!!" + datetime);
  socket.on('chat message', function(msg){
    io.emit('chat message', msg);
  });
  socket.on('disconnect', function(){
    console.log("disconnection established...!!!");
  });
});
http.listen(3000, function(){
  console.log('listening on *:3000');
});

,输出是 1)。我开始服务器 2)。客户连接 3)。我清除了控制台 4)。我关闭了服务器并再次重启。

重启服务器后的输出是这个。任何人都可以告诉我它为什么会发生。只有一个客户端有多个连接和断开连接。

connection established...!!!Last Sync: 11/11/2014 @ 8:11:37
disconnection established...!!!
connection established...!!!Last Sync: 11/11/2014 @ 8:11:40
disconnection established...!!!
connection established...!!!Last Sync: 11/11/2014 @ 8:11:47
disconnection established...!!!
connection established...!!!Last Sync: 11/11/2014 @ 8:11:54
disconnection established...!!!
connection established...!!!Last Sync: 11/11/2014 @ 8:12:14
disconnection established...!!!
connection established...!!!Last Sync: 11/11/2014 @ 8:12:35
disconnection established...!!!

1 个答案:

答案 0 :(得分:0)

我认为您的客户端代码存在一些问题。请查看以下代码。这是工作。 服务器端代码 - 'chat.js'

var app = require('express')(),
    http = require('http').Server(app),
    io = require('socket.io')(http);

app.get('/', function(req, res){
  res.sendfile('index.html');
});

io.on('connection', function(socket){
 console.log('a user connected');
 socket.on('disconnect', function(){
    console.log('user disconnected');
 });
 socket.on('chat_message', function(msg){
    console.log('message: ' + msg);
 });
  socket.emit("progress",90);
});

http.listen(5000, function(){
  console.log('listening on *:5000');
});

客户端代码就在这里。'index.html'

<!doctype html>
<html>

<head>
    <title>Socket.IO chat</title>
    <script src="/socket.io/socket.io.js"></script>
    <script src="http://code.jquery.com/jquery-2.1.1.min.js"></script>

</head>

<body>
    <ul id="messages"></ul>
    <input id="m" autocomplete="off" />
    <button id="btn">Send</button>
    <script>
    var socket = io();
    $('#btn').click(function(e) {
        console.log('ok');
        //e.preventDefault();
        socket.emit('chat_message', $('#m').val());
        $('#m').val('');
        return false;
    });
    socket.on('chat_message', function(msg) {
        $('#messages').append($('<li>').text(msg));
    });
    socket.on("progress", function(val) {
        console.log(val);
    });
    </script>
</body>

</html>