2017-03-29 14 views
0

聊天是使用來自socket.io庫的websockets在nodejs中編程的,我有一個也使用socket.io的websockets的客戶端,但我無法做出兩人一起工作。出於某種原因,客戶端不會將消息發送到服務器(我有一個console.log函數,它應該在接收到消息時寫入控制檯但不寫任何內容)。爲什麼我在使用websockets的nodejs中的簡單聊天不會發送消息

服務器的代碼是:

var app = require('express')(); 
var http = require('http').Server(app); 
var io = require('socket.io')(http); 
var port = process.env.PORT || 3000; 

app.get('/', function(req, res){ 
    res.sendFile(__dirname + '/index.html'); 
}); 

io.on('connection', function(socket){ 
    socket.on('chat message', function(msg){ 
     console.log('message: ' + msg); 
    io.emit('chat message', msg); 
    }); 
}); 

http.listen(port, function(){ 
    console.log('listening on *:' + port); 
}); 

以及客戶端是:

<!DOCTYPE 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://cdn.socket.io/stable/socket.io.js"></script> 
    <script> 



     // Sends a message to the server via sockets 
     function sendMessageToServer(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); 
     } 


     // Create a socket instance 
     socket = new WebSocket('ws://localhost:8080'); 

     // Open the socket 
     socket.onopen = function(event) { 
      console.log('Socket opened on client side',event); 

      // Listen for messages 
      socket.onmessage = function(event) { 
       console.log('Client received a message',event); 
      }; 

      // Listen for socket closes 
      socket.onclose = function(event) { 
       console.log('Client notified socket has closed',event); 
      }; 

     }; 


    </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> 

你有任何想法有什麼不對嗎? 在此先感謝!

+0

嘗試從'socket.on('chat message''改爲'socket.on('message'',please ..根據[docs](https://socket.io/docs/client-api/ #socket-send-args-ack):''socket.send' _發送一個'message' event._,所以你需要在服務器端聽一個正確的。 – FirstOne

+0

另外,那個cdn壞了......也許你是隻是遵循過時的教程,請參考:[https://socket.io/get-started/chat/](https://socket.io/get-started/chat/) – FirstOne

+0

我將「聊天消息」更改爲'消息',但仍然是相同的問題 –

回答

0

根據我的知識,您有幾個問題。如果它們都是固定的,它應該可以工作。

  1. 連接socket.io.js文件應該是你自己的。在你的情況,我猜這是<script src="http://localhost:3000/socket/socket.io.js"></script>我不是100%確定,因爲你的端口可能會有所不同。
  2. 您沒有在客戶端正確定義套接字。將其定義爲:var socket = io();。你這樣做的方式是socket = new WebSocket('ws://localhost:8080');其中WebSocket沒有定義。而且,代碼將創建一個新的websocket服務器,您已經在服務器代碼中完成了這一任務。你需要在客戶端做什麼只是連接到服務器。
  3. 您必須使用相同的通道讓客戶端和服務器進行通信。所以在你的客戶端代碼中,你應該發送像這樣的消息socket.emit('chat message', 'content you want to send'
  4. 我不知道你爲什麼在服務器端再次發出消息。我假設你正在發送消息從客戶端到服務器。所以你不應該在你的服務器代碼中發出命令。

如果所有四件事情都已經解決了,它就會起作用。

如果我的表達是不明確的,我寫了一個超級簡單的例子給你:

服務器端:

let io = socketio(server.listener); 
io.on("connection", function(socket) { 
    console.log("A user connected"); 

    // auto messages when a user is connected 
    setTimeout(function() { 
     socket.send("message at 4 seconds"); 
    }, 4000); 
    setTimeout(function() { 
     socket.send("message at 8 seconds"); 
    }, 8000); 

    // callback when a user disconnected 
    socket.on("disconnect", function(){ 
     console.log("A user disconnected"); 
    }); 

    // receive message from client 
    socket.on("client-server", function(msg) { 
     console.log(msg); 
    }); 
}); 

// you put this code anywhere when you need to send message from server to client 
io.emit("server-client", "server to client message"); 

客戶端:

<!DOCTYPE html> 
<html> 
    <head><title>Hello world</title></head> 
    <script src="http://localhost:3000/socket.io/socket.io.js"></script> 
    <script> 
    var socket = io(); 
    socket.on('server-client', function(data) {document.write(data)}); 
    socket.emit('client-server', 'test message'); 
    </script> 
    <body>Hello world</body> 
</html> 

我的服務器代碼編寫在打字稿中,所以不要介意讓關鍵詞,雙引號等等。

相關問題