2013-07-25 45 views
0

有人能告訴我如何在node.js處與socket.ioxhr-polling斷開連接事件嗎?node.js - 使用xhr-polling獲取與socket.io的斷開連接事件

這個例子不是xhr-polling工作,但與websockets工程..

/* Basics */ 

var express = require('express'), 
app = express(), 
server = require('http').createServer(app), 
io = require('socket.io').listen(server); 

server.listen(1337, null); 

io.set('transports', ['xhr-polling']); 

// routing 
app.get('/', function (req, res) { 
res.sendfile("index.html"); 
app.use(express.static(__dirname)); 
}); 

var online_clients = 0; 

io.sockets.on('connection', function (socket) { 
    socket.on('disconnect', function() { 
     console.log("SOMEONE LEFT"); 
    }); 
}); 

因此,如何與xhr-pooling得到斷開事件?

預先感謝;)

回答

1

在socket.io當連接被關閉不發出斷開事件的XHR輪詢實現,請參閱github issue #431

你可以問socket.io的客戶端通過打開sync disconnect on unload標誌以通知斷開服務器:

// the browser (HTML) side 
var socket = io.connect('http://localhost', { 
    'sync disconnect on unload': true 
}); 

警告:此選項會惡化,當網絡和/或服務器很慢,用戶體驗,請參閱此pull request以獲取更多信息。

相關問題:

相關問題