<p>我在客户端是这样写的:</p>
<pre><code>var socket = io.connect(‘http://localhost:800’); var content = $(’#divcontent’);
socket.on('connect',function(){
content.append($('<p>').text('Contented'));
});
socket.on('Message',function(msg){
content.append($('<p>').text(msg).append($('<em>').text(' from server')));
content.scrollTop(content.height());
});
socket.on('disconnect',function(){
content.append($('<p>').text('Disconnected'));
})
socket.on('reconnect',function(){
content.append($('<p>').text('Reconnected'));
})
socket.on('reconnecting',function(){
content.append($('<p>').text('Reconnecting'));
})
</code></pre>
<p>服务端代码:</p>
<pre><code>io.sockets.on(‘connection’,function(socket){ socket.on(‘fromclient’, function(data) { socket.broadcast.emit(‘fromserver’, data); }); var vola = setInterval(function () { socket.volatile.emit(‘Message’, 'Message(current time) : ’ + new Date()); }, 1000); socket.on(‘disconnect’, function () { //clearInterval(vola); }); }); </code></pre>
<p>官方文档说,socket.io是自动reconnect的。</p>
<p>但是我在测试过程中,服务端一旦停掉,然后重新开启,客户端并不能自动连上去,页面上一直显示 Reconnecting 。</p>
<p>是我代码写得不对么?</p>