2013-02-10 40 views
3

我的node.js socket.io聊天應用程序沒有向其他用戶發送聊天消息(當我打開另一個瀏覽器時)。從瀏覽器時,該消息將只在瀏覽器A.顯示socket.io沒有發送給其他用戶

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

回答

7

帶插座你只發射到當前連接。您需要使用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); 
    }); 
}); 
相關問題