4

我的 node.js socket.io 聊天应用程序没有向其他用户发送聊天消息(当我打开另一个浏览器时)。来自浏览器 A 的消息只会显示在浏览器 A 中。

服务器.js

var app = require('http').createServer(handler),
     io = require('socket.io').listen(app),
     fs = require('fs');

app.listen(4000);

function handler (req, res) {
  fs.readFile(__dirname + '/index.html',
  function (err, data) {
    if (err) {
      res.writeHead(500);
      return res.end('Error loading index.html');
    }

    res.writeHead(200);
    res.end(data);
  });
}

io.sockets.on('connection', function (socket) {
  socket.on('sendChat', function (data) {
    // console.log('sendChat', data);
    socket.emit('displayChat', data);
  });
});

索引.html

<html>
<head>
  <title>Chat</title>

  <style type="text/css">
    #chats {
      overflow: auto;
    }

    #sender {
      position: absolute;
      bottom: 0;
    }
  </style>

  <script src="http://code.jquery.com/jquery-1.9.1.min.js"></script>
  <script src="/socket.io/socket.io.js"></script>

  <script>
    $(document).ready(function(){
      var socket = io.connect('http://localhost');
      var chats = $("#chats ul");

      socket.on('displayChat', function (data) {
        // console.log("displayChat", data);
        chats.append($("<li></li>").text(data));
      });

      var author = $("#author");
      var message = $("#message");

      message.keypress(function(event) {
        if ((event.keyCode || event.which) == 13 && author.val() != '') {
          // console.log("sendchat", author.val() + ": " + message.val());
          socket.emit('sendChat', author.val() + ": " + message.val());
          message.val('');
        }
      });
    });
  </script>
</head>
<body>

  <div id="chats">
    <ul></ul>
  </div>

  <div id="sender">
    <input id="author" type="text"><input id="message" type="text">
  </div>

</body>
</html>

这是一个日志

 info  - socket.io started
   debug - served static content /socket.io.js
   debug - client authorized
   info  - handshake authorized T0NOrPaqIkQMXOJjgz9E
   debug - setting request GET /socket.io/1/websocket/T0NOrPaqIkQMXOJjgz9E
   debug - set heartbeat interval for client T0NOrPaqIkQMXOJjgz9E
   debug - client authorized for 
   debug - websocket writing 1::
sendchat a: hi
   debug - websocket writing 5:::{"name":"displayChat","args":["a: hi"]}
   debug - served static content /socket.io.js
   debug - client authorized
   info  - handshake authorized PL2VRwE2V0UvBc6dgz9F
   debug - setting request GET /socket.io/1/websocket/PL2VRwE2V0UvBc6dgz9F
   debug - set heartbeat interval for client PL2VRwE2V0UvBc6dgz9F
   debug - client authorized for 
   debug - websocket writing 1::
sendchat b: hi
   debug - websocket writing 5:::{"name":"displayChat","args":["b: hi"]}
   info  - transport end (socket end)
   debug - set close timeout for client PL2VRwE2V0UvBc6dgz9F
   debug - cleared close timeout for client PL2VRwE2V0UvBc6dgz9F
   debug - cleared heartbeat interval for client PL2VRwE2V0UvBc6dgz9F
   debug - discarding transport
   debug - emitting heartbeat for client T0NOrPaqIkQMXOJjgz9E
   debug - websocket writing 2::
   debug - set heartbeat timeout for client T0NOrPaqIkQMXOJjgz9E
   debug - got heartbeat packet
   debug - cleared heartbeat timeout for client T0NOrPaqIkQMXOJjgz9E
   debug - set heartbeat interval for client T0NOrPaqIkQMXOJjgz9E
   debug - served static content /socket.io.js
   debug - client authorized
   info  - handshake authorized I3S_VVYyencv9VQOgz9G
   debug - setting request GET /socket.io/1/websocket/I3S_VVYyencv9VQOgz9G
   debug - set heartbeat interval for client I3S_VVYyencv9VQOgz9G
   debug - client authorized for 
   debug - websocket writing 1::
sendchat b: hi
   debug - websocket writing 5:::{"name":"displayChat","args":["b: hi"]}
   debug - emitting heartbeat for client T0NOrPaqIkQMXOJjgz9E
   debug - websocket writing 2::
   debug - set heartbeat timeout for client T0NOrPaqIkQMXOJjgz9E
   debug - got heartbeat packet
   debug - cleared heartbeat timeout for client T0NOrPaqIkQMXOJjgz9E
   debug - set heartbeat interval for client T0NOrPaqIkQMXOJjgz9E
   debug - emitting heartbeat for client I3S_VVYyencv9VQOgz9G
   debug - websocket writing 2::
   debug - set heartbeat timeout for client I3S_VVYyencv9VQOgz9G
   debug - got heartbeat packet
   debug - cleared heartbeat timeout for client I3S_VVYyencv9VQOgz9G
   debug - set heartbeat interval for client I3S_VVYyencv9VQOgz9G
   debug - emitting heartbeat for client T0NOrPaqIkQMXOJjgz9E
   debug - websocket writing 2::
   debug - set heartbeat timeout for client T0NOrPaqIkQMXOJjgz9E
   debug - got heartbeat packet
   debug - cleared heartbeat timeout for client T0NOrPaqIkQMXOJjgz9E
   debug - set heartbeat interval for client T0NOrPaqIkQMXOJjgz9E
   debug - emitting heartbeat for client I3S_VVYyencv9VQOgz9G
   debug - websocket writing 2::
   debug - set heartbeat timeout for client I3S_VVYyencv9VQOgz9G
   debug - got heartbeat packet
   debug - cleared heartbeat timeout for client I3S_VVYyencv9VQOgz9G
   debug - set heartbeat interval for client I3S_VVYyencv9VQOgz9G
sendchat a: digg
   debug - websocket writing 5:::{"name":"displayChat","args":["a: digg"]}
   debug - emitting heartbeat for client T0NOrPaqIkQMXOJjgz9E
   debug - websocket writing 2::
   debug - set heartbeat timeout for client T0NOrPaqIkQMXOJjgz9E
   debug - got heartbeat packet
   debug - cleared heartbeat timeout for client T0NOrPaqIkQMXOJjgz9E
   debug - set heartbeat interval for client T0NOrPaqIkQMXOJjgz9E
   debug - emitting heartbeat for client I3S_VVYyencv9VQOgz9G
   debug - websocket writing 2::
   debug - set heartbeat timeout for client I3S_VVYyencv9VQOgz9G
   debug - got heartbeat packet
   debug - cleared heartbeat timeout for client I3S_VVYyencv9VQOgz9G
   debug - set heartbeat interval for client I3S_VVYyencv9VQOgz9G
sendchat b: dd
   debug - websocket writing 5:::{"name":"displayChat","args":["b: dd"]}
   debug - emitting heartbeat for client T0NOrPaqIkQMXOJjgz9E
   debug - websocket writing 2::
   debug - set heartbeat timeout for client T0NOrPaqIkQMXOJjgz9E
   debug - got heartbeat packet
   debug - cleared heartbeat timeout for client T0NOrPaqIkQMXOJjgz9E
   debug - set heartbeat interval for client T0NOrPaqIkQMXOJjgz9E
   debug - emitting heartbeat for client I3S_VVYyencv9VQOgz9G
   debug - websocket writing 2::
4

1 回答 1

8

使用套接字,您只向当前连接发出。您需要使用 io.sockets 发射,它将发射到任何打开的套接字。

改变这个:

io.sockets.on('connection', function (socket) {
  socket.on('sendChat', function (data) {
    // console.log('sendChat', data);
    socket.emit('displayChat', data);
  });
});

对此:

io.sockets.on('connection', function (socket) {
  socket.on('sendChat', function (data) {
    // console.log('sendChat', data);
    io.sockets.emit('displayChat', data);
  });
});
于 2013-02-10T01:32:17.933 回答