Can not send message from client to server use node.js and socket.io

I try node.js and socket.io to build application on web in several day. I don't understand why I can not send message from client to server use socket.io and server node.js. I can receive msgs and events from server to client. But can not receive from server of client's , events. I think I have sth wrong with my libs.

Modules node.js I installed

  • express
  • io
  • jade
  • net
  • socket.io
  • socket.io-client

Why there have socket.io-client and socket.io? I even try this example: code.google.com/p/nodejs-win/wiki/SimpleChatSystem But still not work. Server can not receive any massages and events from client.

code server.js

// Require HTTP module (to start server) and Socket.IO
var http = require('http'), 
io = require('socket.io');

// Start the server at port 8080
var server = http.createServer(function(req, res){ 

    // Send HTML headers and message
    res.writeHead(200,{ 'Content-Type': 'text/html' }); 
    res.end('<h1>Hello Socket Lover!</h1>');
});
server.listen(8080);
// Create a Socket.IO instance, passing it our server
var socket = io.listen(server);

// Add a connect listener
socket.on('connection', function(client){ 
    console.log("server is start on port 8080");

    // 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');
    });

});

code client index.html

<html>
<head>
    <style>
        * { margin:0; padding:0; font-size:11px; font-family:arial; color:#444; }
        body { padding:20px; }
        #message-list { list-style-type:none; width:300px; height:300px; overflow:auto; border:1px solid #999; padding:20px; }
        #message-list li { border-bottom:1px solid #ccc; padding-bottom:2px; margin-bottom:5px; }
        code { font-family:courier; background:#eee; padding:2px 4px; }
    </style>
    <script src="http://localhost:8080/socket.io/socket.io.js"></script>
    <script>

        // Create SocketIO instance
        var socket = new io.connect('localhost',{
            port: 8080
        });
        //socket.connect(); 

        // Add a connect listener
        socket.on('connect',function() {
            log('<span style="color:green;">Client has connected to the server!</span>');
        });

        // Add connecting listener
        socket.on('connecting',function() {
            log('<span style="color:green;">Client is connecting to the server!</span>');
        });
        // 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>');
        });

        // Sends a message to the server via sockets
        function sendMessageToServer(message) {
            socket.emit("message",message);
                    socket.send(message);            
            log('<span style="color:#888">Sending "' + message + '" to the server!</span>');
        }

        // Outputs to console and list
        function log(message) {
            var li = document.createElement('li');
            li.innerHTML = message;
            document.getElementById('message-list').appendChild(li);
        }

        setInterval(function(){ sendMessageToServer(new Date())},5000);

    </script>
</head>
<body>

    <p>Messages will appear below (and in the console).</p><br />
    <ul id="message-list"></ul>
    <ul style="margin:20px 0 0 20px;">
        <li>Type <code>socket.disconnect()</code> to disconnect</li>
        <li>Type <code>socket.connect()</code> to reconnect</li>
        <li>Type <code>sendMessageToServer('Your Message')</code> to send a message to the server</li>
    </ul>
</body>
</html>

My client log: https://dl.dropbox.com/u/56440676/client.png

My server log: https://dl.dropbox.com/u/56440676/server.png

Error here:

// Sends a message to the server via sockets
function sendMessageToServer(message) {
    socket.emit(message);
    log('<span style="color:#888">Sending "' + message + '" to the server!</span>');
}

Method emit takes two arguments. The first - event code. The second - event data.

Right solution:

// Sends a message to the server via sockets
function sendMessageToServer(message) {
    socket.emit('message', message);
    log('<span style="color:#888">Sending "' + message + '" to the server!</span>');
}