繁体   English   中英

socket.io给我奇怪的输出

[英]socket.io giving me weird output

当我重新启动服务器时,我看到奇怪的输出..您能说明我为什么得到此输出吗。通过在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...!!!

我认为您的客户端代码存在一些问题。 请看下面的代码。 这是工作。 服务器端代码“ 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> 

暂无
暂无

声明:本站的技术帖子网页,遵循CC BY-SA 4.0协议,如果您需要转载,请注明本站网址或者原文地址。任何问题请咨询:yoyou2525@163.com.

 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM