簡體   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