socket.io不向其他用户发送

时间:2013-02-10 00:37:32

标签: node.js socket.io

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

server.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);
  });
});

的index.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::

1 个答案:

答案 0 :(得分: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);
  });
});