【发布时间】:2013-06-14 09:24:32
【问题描述】:
我正在开发一个用 Node.js 编程的 Websocket 服务器,我计划向超过 20,000 个用户发送多播消息。该消息将每隔一秒发送一次,但是我担心 Node.js 服务器的性能。
我了解 Node.js 以异步方式工作并根据需要创建和销毁线程,但我不确定它的效率。理想情况下,我希望以 5 毫秒的平均延迟发送消息。
目前,我正在通过运行所有已连接客户端的 for 循环向所有用户发送消息,如下所示:
function StartBroadcastMessage()
{
console.log("broadcasting socket data to client...!");
for(var i=0;i < clientsWithEvents.length;i++){ //Runs through all the clients
client = clientsWithEvents[i];
if(client.eventid.toLowerCase() == serverEventName.toLowerCase()) //Checks to see if the Client Event names and server names are the same
client.connection.sendUTF(GetEventSocketFeed(client.eventid)); //Sends out event data to that particular client
}
timeoutId = setTimeout(StartBroadcastMessage,2*1000);
}
这是一种以低延迟发送多播消息的有效方式,还是有更好的方式?
另外,有没有一种有效的方法在服务器上执行负载测试,模拟连接到 Websocket 服务器的许多设备? (到目前为止,我已经找到了这个 Node 应用程序https://github.com/qarea/websockets-stress-test)
【问题讨论】: