Closed
Description
hi all!
I'm a newbie for socket.io. I have downloaded an example for understanding how socket.io works. And i meet an issue that I can't explain why!
Here is my client code:
<script src="socket.io.js"></script>
<script>
// Create SocketIO instance
var socket;
// Sends a message to the server via sockets
function sendMessageToServer(message) {
if(socket){
socket.send(message);
log('<span style="color:#888">Sending "' + message + '" to the server!</span>');
}//socket.flushBuffer;
}
function disconnect(){
}
function reconnect(){
socket = io.connect(
'http://localhost:8080'
);
//socket.connect();
// Add a connect listener
socket.on('connect',function() {
log('<span style="color:green;">Client has connected to the server!</span>');
//server.send('hello world');
});
// Add a connect listener
socket.on('message',function(data) {
log('Received a message from the server: ' + data);
});
// Add a disconnect listener
socket.on('disconnect',function() {
log('<span style="color:red;">The client has disconnected!</span>');
});
}
// Outputs to console and list
function log(message) {
var li = document.createElement('li');
li.innerHTML = message;
document.getElementById('message-list').appendChild(li);
}
</script>
and server code:
// Require and Socket.IO
var io = require('./lib/socket.io');
// Create a Socket.IO instance, passing it our server
var socket = io.listen(8080);
console.log('Server is started!');
// Add a connect listener
socket.on('connection', function(client){
// Create periodical which ends a message to the client every 5 seconds
var interval = setInterval(function() {
client.send('This is a message from the server! ' + new Date().getTime());
},5000);
// Success! Now listen to messages to be received
client.on('message',function(event){
console.log('Received message from client!',event);
});
client.on('disconnect',function(){
clearInterval(interval);
console.log('Server has disconnected');
});
});
My PC runs win7 32bit.
I run my client on Firefox and Chrome with follow steps:
- Run my server by command: node server.js
- Open my client on browser.
- Click 'Connect' button to call reconnect() function.
- Notice that 'connect' event is not triggered. By now, server can send a message 'This is a message from the server!' to client and client can receive this.
- I shutdown server and realize 'connect' event is triggered on client side.
When i run my client on IE9. Everything is ok. 'Connect' event is normally triggered before client can receive message from server.
And a weird thing is when i copy my source to my friend PC which runs win7 32bit, everything is ok on every browser like when i run my client on IE9.
So, where is wrong here? Do i make a mistake? Do i config something wrong?
Pls help me! Thanks.
Metadata
Metadata
Assignees
Labels
No labels