Skip to content
New issue

Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.

By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.

Already on GitHub? Sign in to your account

'connect' event is not triggered on Firefox and Chrome #1009

Closed
learningKool opened this issue Aug 29, 2012 · 1 comment
Closed

'connect' event is not triggered on Firefox and Chrome #1009

learningKool opened this issue Aug 29, 2012 · 1 comment

Comments

@learningKool
Copy link

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:

  1. Run my server by command: node server.js
  2. Open my client on browser.
  3. Click 'Connect' button to call reconnect() function.
  4. 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.
  5. 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.

@learningKool
Copy link
Author

hi there!

I've found it. Because of my IDM, I remove it and everything is ok!

Thank you for your support.

Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment
Labels
None yet
Projects
None yet
Development

No branches or pull requests

1 participant