javascript Node.js WebSocket 广播
声明:本页面是StackOverFlow热门问题的中英对照翻译,遵循CC BY-SA 4.0协议,如果您需要使用它,必须同样遵循CC BY-SA许可,注明原文地址和作者信息,同时你必须将它归于原作者(不是我):StackOverFlow
原文地址: http://stackoverflow.com/questions/6098313/
Warning: these are provided under cc-by-sa 4.0 license. You are free to use/share it, But you must attribute it to the original authors (not me):
StackOverFlow
Node.js WebSocket Broadcast
提问by vhyea
I'm using the ws libraryfor WebSockets in Node.js and I'm trying this example from the library examples:
我在 Node.js 中为 WebSockets使用ws 库,我正在从库示例中尝试这个示例:
var sys = require("sys"),
ws = require("./ws");
ws.createServer(function (websocket) {
websocket.addListener("connect", function (resource) {
// emitted after handshake
sys.debug("connect: " + resource);
// server closes connection after 10s, will also get "close" event
setTimeout(websocket.end, 10 * 1000);
}).addListener("data", function (data) {
// handle incoming data
sys.debug(data);
// send data to client
websocket.write("Thanks!");
}).addListener("close", function () {
// emitted when server or client closes connection
sys.debug("close");
});
}).listen(8080);
All OK. It works, but running 3 clients, for instance, and sending "Hello!" from one will make the server only reply "Thanks!" to the client which sent the message, not to all.
一切都好。它可以工作,但是例如运行 3 个客户端并发送“Hello!” from one 将使服务器只回复“Thanks!” 发送给发送消息的客户端,而不是全部。
How can I broadcast "Thanks!" to all connected clients when someone sends "Hello!"?
我如何广播“谢谢!” 当有人发送“你好!”时到所有连接的客户端?
Thanks!
谢谢!
回答by onteria_
If you want to send out to all clients, you have to keep track of them. Here is a sample:
如果您想发送给所有客户,您必须跟踪他们。这是一个示例:
var sys = require("sys"),
ws = require("./ws");
// # Keep track of all our clients
var clients = [];
ws.createServer(function (websocket) {
websocket.addListener("connect", function (resource) {
// emitted after handshake
sys.debug("connect: " + resource);
// # Add to our list of clients
clients.push(websocket);
// server closes connection after 10s, will also get "close" event
// setTimeout(websocket.end, 10 * 1000);
}).addListener("data", function (data) {
// handle incoming data
sys.debug(data);
// send data to client
// # Write out to all our clients
for(var i = 0; i < clients.length; i++) {
clients[i].write("Thanks!");
}
}).addListener("close", function () {
// emitted when server or client closes connection
sys.debug("close");
for(var i = 0; i < clients.length; i++) {
// # Remove from our connections list so we don't send
// # to a dead socket
if(clients[i] == websocket) {
clients.splice(i);
break;
}
}
});
}).listen(8080);
I was able to get it to broadcast to all clients, but it's not heavily tested for all cases. The general concept should get you started though.
我能够让它向所有客户端广播,但它并没有针对所有情况进行大量测试。不过,一般概念应该可以帮助您入门。
EDIT: By the way I'm not sure what the 10 second close is for so I've commented it out. It's rather useless if you're trying to broadcast to all clients since they'll just keep getting disconnected.
编辑:顺便说一下,我不确定 10 秒关闭是为了什么,所以我已经将其注释掉了。如果您尝试向所有客户端广播,那将毫无用处,因为它们只会不断断开连接。
回答by bjornd
I would recommend you to use socket.io. It has example web-chat functionality out of the box and also provides abstraction layer from the socket technology on client (WebSockets are supported by Safari, Chrome, Opera and Firefox, but disabled in Firefox and Opera now due to security vulnerabilities in ws-protocol).
我建议你使用socket.io。它具有开箱即用的示例网络聊天功能,还提供客户端套接字技术的抽象层(Safari、Chrome、Opera 和 Firefox 支持 WebSockets,但由于 ws-protocol 中的安全漏洞,现在在 Firefox 和 Opera 中禁用)。