【发布时间】:2014-07-18 22:46:18
【问题描述】:
我在 Node.js 和 WebSockets 中使用 ws library 我正在从库示例中尝试这个示例:
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);
一切正常。它可以工作,但运行 3 个客户端,例如,并发送“你好!”从一个将使服务器只回复“谢谢!”发送给发送消息的客户端,而不是发送给所有客户端。
如何广播“谢谢!”当有人发送“Hello!”时向所有连接的客户端发送?
谢谢!
【问题讨论】:
标签: javascript node.js websocket