server.js
var app = require('express')() , server = require('http').createServer(app) , io = require('socket.io').listen(server,{log:false});express 3.0.6 socket.io 0.9.13server.listen(888);
app.get('/’, function (req, res) { res.sendfile(__dirname + ‘/index.html’); });
io.sockets.on('connection’, function (socket) { console.log('connection '+new Date()); socket.emit('news’, { hello: ‘world’ }); socket.on('my other event’, function (data) { console.log(data); }); socket.on('disconnect’, function () { console.log('user disconnected '+new Date()); }); });
client.html
<script src="http://localhost:888/socket.io/socket.io.js"></script> <script>if (/Firefox/\s/.test(navigator.userAgent)){ var socket = io.connect("http://localhost:888",{transports:[‘xhr-polling’]}); } else if(/MSIE (\d+.\d+);/.test(navigator.userAgent)){ var socket = io.connect("http://localhost:888",{transports:[‘jsonp-polling’]}); } else { var socket = io.connect(“http://localhost:888”); }
socket.on('news’, function (data) { //console.log(data); document.write(data.hello); socket.emit('my other event’, { my: ‘data’ }); });
</script>
客户端是apache+php的,在google浏览器一切正常,刷新和关闭都正常 但是在ie和ff能连接上并正常通信,但是在1分钟之内会自动触发disconnect事件,反而正常退出或刷新却不能触发disconnect事件,混乱了…求高手帮忙解答!!!