如何使用Web插座服务器发送广播数据?

发布于 2025-01-27 16:35:05 字数 273 浏览 4 评论 0原文

当事件发生时,我需要从我的BE发送数据,因此我创建了一个事件发射器,在Fucntion中,我想向所有客户端发送广播消息(该消息是对象的数组),这是我的代码,但不幸的是无法正常工作。

emitter.on('readData', function(){
    wss.broadcast = function(data) {
        wss.clients.forEach(client => client.send(Object));
     };
    });

I need to send data from my BE to FE when an event happen, so I have created an Event Emitter and in the Fucntion i would like to send a Broadcast message to all the client (the message is an Array of Object), this is my code but unfortunately doesn't work properly.

emitter.on('readData', function(){
    wss.broadcast = function(data) {
        wss.clients.forEach(client => client.send(Object));
     };
    });

如果你对这篇内容有疑问,欢迎到本站社区发帖提问 参与讨论,获取更多帮助,或者扫码二维码加入 Web 技术交流群。

扫码二维码加入Web技术交流群

发布评论

需要 登录 才能够评论, 你可以免费 注册 一个本站的账号。

评论(1

唱一曲作罢 2025-02-03 16:35:05

好的,而不是广播,您正在定义广播的功能。

emitter.on('readData', function(){
    wss.clients.forEach(client => client.send(Object));
});

更多详细信息在这里

Ok instead of broadcasting, you are defining a function to broadcast.

emitter.on('readData', function(){
    wss.clients.forEach(client => client.send(Object));
});

More details here.

~没有更多了~
我们使用 Cookies 和其他技术来定制您的体验包括您的登录状态等。通过阅读我们的 隐私政策 了解更多相关信息。 单击 接受 或继续使用网站,即表示您同意使用 Cookies 和您的相关数据。
原文