2013-01-01 236 views
1

我嘗試node.js和socket.io在幾天內在Web上構建應用程序。我不明白爲什麼我不能發送消息從客戶端到服務器使用socket.io和服務器node.js.我可以從服務器接收消息和事件到客戶端。但不能從客戶端的事件服務器接收。我想我的問題與我的庫有關。無法發送消息從客戶端到服務器使用node.js和socket.io

模塊的node.js我安裝

  • 表達
  • IO
  • socket.io
  • socket.io客戶端

爲什麼有插座。 io-client和socket.io?我甚至嘗試這個例子:code.google.com/p/nodejs-win/wiki/SimpleChatSystem 但仍然無法正常工作。服務器無法接收來自客戶端的任何按摩和事件。

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

}); 

代碼客戶端的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> 

我的客戶端日誌:https://dl.dropbox.com/u/56440676/client.png

我的服務器日誌:https://dl.dropbox.com/u/56440676/server.png

+0

你檢查出sockjs爲您的應用。它簡單而強大。 – GautamJeyaraman

+0

謝謝...我會試試看。但問題仍然存在,如果我不能讓它運行socket.io –

+0

我認爲'套接字'尚未初始化'sendMessageToServer'裏面。請在'sendMessageToServer'' console.log(typeof套接字)'裏面檢查一下。或嘗試移動此行''setInterval(function(){sendMessageToServer(new Date())},5000);''inside' socket.on('connect',function(){' – Broncha

回答

0

錯誤的位置:

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

方法emit需要兩個參數。 第一個事件代碼。 第二個事件數據。

正確的解決方案:

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

感謝Vadim Baryshev我只是糾正錯誤。在我使用emit()函數之前,我已經使用send()來發送msg,但是仍然存在,我改變爲像你說的那樣發射,但是它仍然存在,所以我使用它們,仍然不起作用。在socket.io模塊可以糾正這個問題:)有時候我可以收到serveral信息,但之後,每個消息從客戶端發送到服務器丟失...:|我仍然不知道那裏有什麼問題。 –

相關問題