节点。js +套接字。从服务器而不是从特定客户端进行io广播?

时间:2021-11-24 20:27:29

I'm building a simple system like a realtime news feed, using node.js + socket.io.

我正在使用node构建一个简单的系统,比如实时新闻提要。js + socket . io。

Since this is a "read-only" system, clients connect and receive data, but clients never actually send any data of their own. The server generates the messages that needs to be sent to all clients, no client generates any messages; yet I do need to broadcast.

由于这是一个“只读”系统,客户端连接并接收数据,但是客户端从来不会发送任何自己的数据。服务器生成需要发送给所有客户端的消息,没有客户端生成任何消息;但我确实需要广播。

The documentation for socket.io's broadcast (end of page) says

套接字的文档。io的广播(页尾)说

To broadcast, simply add a broadcast flag to emit and send method calls. Broadcasting means sending a message to everyone else except for the socket that starts it.

要进行广播,只需添加一个广播标志来发出和发送方法调用。广播意味着向其他人发送消息,除了启动它的插座。

So I currently capture the most recent client to connect, into a variable, then emit() to that socket and broadcast.emit() to that socket, such that this new client gets the new data and all the other clients. But it feels like the client's role here is nothing more than a workaround for what I thought socket.io already supported.

因此,我当前捕获要连接的最近的客户端到一个变量中,然后向该套接字发出()并向该套接字释放(),以便新客户端获取新数据和所有其他客户端。但我觉得客户在这里扮演的角色不过是我所认为的套接字的修修补补。io已经支持。

Is there a way to send data to all clients based on an event initiated by the server?

是否有一种方法可以基于服务器发起的事件向所有客户端发送数据?

My current approach is roughly:

我目前的方法大致是:

var socket;

io.sockets.on("connection", function (s) {
  socket = s;
});

/* bunch of real logic, yadda yadda ... */

myServerSideNewsFeed.onNewEntry(function (msg) {
  socket.emit("msg", { "msg" : msg });
  socket.broadcast.emit("msg", { "msg" : msg });
});

Basically the events that cause data to require sending to the client are all server-side, not client-side.

基本上,导致数据需要发送到客户端的事件都是服务器端,而不是客户端。

2 个解决方案

#1


16  

Why not just do like below?

为什么不试试下面的方法呢?

io.sockets.emit('hello',{msg:'abc'});

#2


4  

Since you are emitting events only server side, you should create a custom EventEmitter for your server.

由于您只在服务器端发送事件,所以应该为您的服务器创建一个自定义事件发送器。

var io = require('socket.io').listen(80);
    events = require('events'),
    serverEmitter = new events.EventEmitter();

io.sockets.on('connection', function (socket) {
  // here you handle what happens on the 'newFeed' event
  // which will be triggered by the server later on
  serverEmitter.on('newFeed', function (data) {
    // this message will be sent to all connected users
    socket.emit(data);
  });
});

// sometime in the future the server will emit one or more newFeed events
serverEmitter.emit('newFeed', data);

Note: newFeed is just an event example, you can have as many events as you like.

注意:newFeed只是一个事件示例,您可以拥有任意多个事件。

Important

重要的

The solution above is better also because in the future you might need to emit certain messages only to some clients, not all (thus need conditions). For something simpler (just emit a message to all clients no matter what), io.sockets.broadcast.emit() is a better fit indeed.

上面的解决方案更好,因为将来您可能只需要将某些消息发送给某些客户机,而不是所有客户机(因此需要条件)。对于更简单的东西(无论如何只要向所有客户端发送消息),io.sockets.com .broadcast.emit()确实更适合。

#1


16  

Why not just do like below?

为什么不试试下面的方法呢?

io.sockets.emit('hello',{msg:'abc'});

#2


4  

Since you are emitting events only server side, you should create a custom EventEmitter for your server.

由于您只在服务器端发送事件,所以应该为您的服务器创建一个自定义事件发送器。

var io = require('socket.io').listen(80);
    events = require('events'),
    serverEmitter = new events.EventEmitter();

io.sockets.on('connection', function (socket) {
  // here you handle what happens on the 'newFeed' event
  // which will be triggered by the server later on
  serverEmitter.on('newFeed', function (data) {
    // this message will be sent to all connected users
    socket.emit(data);
  });
});

// sometime in the future the server will emit one or more newFeed events
serverEmitter.emit('newFeed', data);

Note: newFeed is just an event example, you can have as many events as you like.

注意:newFeed只是一个事件示例,您可以拥有任意多个事件。

Important

重要的

The solution above is better also because in the future you might need to emit certain messages only to some clients, not all (thus need conditions). For something simpler (just emit a message to all clients no matter what), io.sockets.broadcast.emit() is a better fit indeed.

上面的解决方案更好,因为将来您可能只需要将某些消息发送给某些客户机,而不是所有客户机(因此需要条件)。对于更简单的东西(无论如何只要向所有客户端发送消息),io.sockets.com .broadcast.emit()确实更适合。