Html 我可以向所有 WebSocket 客户端广播吗

声明:本页面是StackOverFlow热门问题的中英对照翻译,遵循CC BY-SA 4.0协议,如果您需要使用它,必须同样遵循CC BY-SA许可,注明原文地址和作者信息,同时你必须将它归于原作者(不是我):StackOverFlow 原文地址: http://stackoverflow.com/questions/3155528/
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

提示:将鼠标放在中文语句上可以显示对应的英文。显示中英文
时间:2020-08-29 03:40:35  来源:igfitidea点击:

Can I broadcast to all WebSocket clients

htmlwebsocket

提问by Bernhard Hofmann

I'm assuming this isn't possible, but wanted to ask in case it is. If I want to provide a status information web page, I want to use WebSockets to push the data from the server to the browser. But my concerns are the effect a large number of browsers will have on the server. Can I broadcast to all clients rather than send discrete messages to each client?

我假设这是不可能的,但想问问以防万一。如果我想提供一个状态信息网页,我想使用 WebSockets 将数据从服务器推送到浏览器。但我担心的是大量浏览器会对服务器产生影响。我可以向所有客户端广播而不是向每个客户端发送离散消息吗?

回答by nos

WebSockets uses TCP, which is point to point, and provides no broadcast support.

WebSockets 使用 TCP,它是点对点的,不提供广播支持。

回答by Nicholas Pufal

Not sure how is your client/server setup, but you can always just keep in the server a collection of all connected clients - and then iterate over each one and send the message.

不确定您的客户端/服务器设置如何,但您始终可以在服务器中保留所有连接客户端的集合 - 然后遍历每个客户端并发送消息。

A simple example using Node's Websocket library:

一个使用 Node 的 Websocket 库的简单示例:

Server code

服务器代码

var WebSocketServer = require('websocket').server;

var clients = [];
var socket = new WebSocketServer({
  httpServer: server,
  autoAcceptConnections: false
});

socket.on('request', function(request) {
  var connection = request.accept('any-protocol', request.origin);
  clients.push(connection);

  connection.on('message', function(message) {
    //broadcast the message to all the clients
    clients.forEach(function(client) {
      client.send(message.utf8Data);
    });
  });
});

回答by broofa

As noted in other answers, WebSockets don't support multicast, but it looks like the 'ws' module maintains a list of connected clients for you, so it's pretty easy to iterate through them. From the docs:

正如其他答案中所述,WebSockets 不支持多播,但“ws”模块似乎为您维护了一个已连接客户端的列表,因此遍历它们非常容易。从文档

var WebSocketServer = require('ws').Server
  , wss = new WebSocketServer({ port: 8080 });

wss.broadcast = function broadcast(data) {
  wss.clients.forEach(function each(client) {
    client.send(data);
  });
};

回答by Barnwal Vivek

Yes, it is possible to broadcast messages to multiple clients.

是的,可以向多个客户端广播消息。

In Java,

在爪哇,

  @OnMessage
  public void onMessage(String m, Session s) throws IOException {
  for (Session session : s.getOpenSessions()) {
    session.getBasicRemote().sendText(m);
   }
}

and here it is explained. https://blogs.oracle.com/PavelBucek/entry/optimized_websocket_broadcast.

并在这里解释。 https://blogs.oracle.com/PavelBucek/entry/optimized_websocket_broadcast

回答by Johnydep

Yes you can and there are many socket servers out there written in various scripting languages that are doing it.

是的,您可以,并且有许多用各种脚本语言编写的套接字服务器正在执行此操作。

回答by Joseph Victor Zammit

It depends on the server-side really. Here's an example of how it's done using Tomcat7:

这真的取决于服务器端。以下是如何使用Tomcat7完成的示例

Tomcat 7 Chat Websockets Servlet Example

Tomcat 7 聊天 Websockets Servlet 示例

and an explanation of the how it's constructed here.

并解释它是如何在这里构建的。

回答by tupefacto

The Microsoft.Web.WebSockets namespace has a WebSocketCollection with Broadcast capability. Look for the assembly in Nuget. The name is Microsoft.WebSockets.

Microsoft.Web.WebSockets 命名空间具有具有广播功能的 WebSocketCollection。在 Nuget 中查找程序集。名称是 Microsoft.WebSockets。