节点 Websockets 广播不起作用 [英] Node Websockets Broadcast not working

查看:39
本文介绍了节点 Websockets 广播不起作用的处理方法,对大家解决问题具有一定的参考价值,需要的朋友们下面随着小编来一起学习吧!

问题描述

我在 Node.js 中启动的超级简单的 websocket 设置目前有一个问题,广播"似乎无法正常运行.这是我第一次使用 websockets,所以我可能会遗漏一些非常明显的东西,但是在网上查找了一段时间后我找不到解决方案.

Currently having an issue with "Broadcast" not seeming to function properly with a super simple websocket setup I started in Node. This is my first time working with websockets so I may be missing something pretty apparent, but after looking online for a while I wasn't able to find a resolution.

基本上我只是想有能力将一些 json 推送到所有当前连接的客户端.

Basically I am just trying to have the ability to push some json out to all currently connected Clients.

我可以确认该套接字正在工作,因为当我在 ws://localhost:3000 上连接时,我能够在连接"块中看到ws.send"上的静态连接字符串,以及看到多个如果我与多个客户端连接,客户端会从广播方法中注销.

I can confirm that that socket is working because I am able to see the static connection string on 'ws.send' in the 'connection' block when I connect at ws://localhost:3000, as well as seeing mulitple clients logged out from the broadcast method if I connect with multiple clients.

对于我可能遗漏的任何帮助,将不胜感激,

Any help as to what I may be missing would be greatly appreciated,

var WebSocketServer = require('uws').Server;
var wss = new WebSocketServer({ port: 3000 }); // ws://localhost:3000

// Static test var
var test_message = {
    'test': 'Response',
    'test2': 'Response2'
};

// Broadcast to all.
wss.broadcast = function broadcast(data) {
  wss.clients.forEach(function each(client) {
    console.log('IT IS GETTING INSIDE CLIENTS');
    console.log(client);

    // The data is coming in correctly
    console.log(data);
    client.send(data);
  });
};

wss.on('connection', function(ws) {
   ws.on('message', function(message) {
     wss.broadcast(test_message);
     console.log('Received: ' + message);
   });

   // TODO This is static just to check that the connection is properly working
   ws.send('You successfully connected to the websocket.');
});

推荐答案

我使用 Smart Websocket Client 测试了您的代码.你的代码没问题.如果你广播的数据只有 string,那么你可以在 UI 中看到回复,但是对于 javascript object,客户端不显示,尽管您可以在 Chrome Developer Tools 中看到响应为 Binary Frames (opcode = 2).

I tested your code with Smart Websocket Client. Your code is fine. If you broadcast data having string only, then you can see the reply in UI, but for javascript object, the client doesn't display although you can see response as Binary Frames (opcode = 2) in Chrome Developer Tools.

这种行为背后的原因是 ws.send() 方法支持普通字符串、类型化数组或 blob,但发送类型化数组和 blob 将导致客户端作为二进制帧(操作码 = 2).

The reason behind this behavior is that the ws.send() method support normal strings, typed arrays or blobs, but sending typed arrays and blobs will result in the frame(s) received by the client as binary frames (opcode = 2).

你可以试试JSON.stringifyobject

wss.on('connection', function(ws) {
   ws.on('message', function(message) {
     wss.broadcast(JSON.stringify(test_message));
     console.log('Received: ' + message);
   });

   ws.send('You successfully connected to the websocket.');
});

这篇关于节点 Websockets 广播不起作用的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持IT屋!

查看全文
登录 关闭
扫码关注1秒登录
发送“验证码”获取 | 15天全站免登陆