如何在客户端解码websocket数据?

发布于 2025-01-10 18:30:38 字数 1879 浏览 0 评论 0原文

我花了很多时间尝试从服务器解码缓冲区,但我还没能弄清楚它的位置。

我尝试删除 JSON.parse() 但仍然出现错误。

这是从后端发送的数据,我将一个普通对象转换为缓冲区并将其字符串化:

 wss.clients.forEach((client) => {
      if (client.userID === contactID) {
        console.log(client.userID);
        client.send(Buffer.from(JSON.stringify(messageObj)));
      }
    });

在前端我有一个侦听器:

 ws.socket.addEventListener("message", async (message) => {
          const blobToText = await new Response(JSON.parse(message.data)).text();
          alert(blobToText);
          console.log(blobToText)
        });

这是我收到的错误:


[Unhandled promise rejection: SyntaxError: JSON Parse error: Unexpected identifier "object"]
at components/Home/Home.js:38:12 in ws.socket.addEventListener$argument_1

当我从后端删除缓冲区时(仅使用 stringify) ,我在客户端得到这个:

{"fields":{"consumerTag":"amq.ctag-LijiOI-nknkknjknj","deliveryTag":16,"redelivered":false,"exchange":"","routingKey":"messages"},"properties":{"headers":{}},"content":{"type":"Buffer","data":[123,34,114,101,97,100,34,58,102,97,108,115,101,44,34,116,105,109,101,83,101,110,116,34,58,49,54,52,54,48,55,52,50,54,52,55,54,53,44,34,109,101,115,115,97,103,101,34,58,34,102,101,114,102,101,114,102,34,44,34,117,115,101,114,73,68,34,58,52,55,44,34,99,111,110,116,97,99,116,73,68,34,58,52,55,125]}}

我现在解析时得到这个;如何将其转换为实际的消息对象?:

[
  101,  44,  34, 116, 105, 109, 101,  83, 101, 110, 116,  34,
   58,  49,  54,  52,  54,  48,  55,  54,  56,  50,  48,  52,
   58,  34, 101, 101, 102, 101, 119, 102,  34,  44,  34, 117,
  115, 101, 114,  73,  68,  34,  58,  52,  55,  44,  34,  99,
]

I've spent numerous hours trying to decode a buffer from the server and I haven't been anle to figure it put.

I've tried to remove JSON.parse() and I still an error.

This is the sent data from the backend where I convert a plain object to a buffer and stringify it:

 wss.clients.forEach((client) => {
      if (client.userID === contactID) {
        console.log(client.userID);
        client.send(Buffer.from(JSON.stringify(messageObj)));
      }
    });

On the frontend I have a listener:

 ws.socket.addEventListener("message", async (message) => {
          const blobToText = await new Response(JSON.parse(message.data)).text();
          alert(blobToText);
          console.log(blobToText)
        });

This is the error i'm getting:


[Unhandled promise rejection: SyntaxError: JSON Parse error: Unexpected identifier "object"]
at components/Home/Home.js:38:12 in ws.socket.addEventListener$argument_1

When I remove buffer from the backend (just using stringify), I get this on client:

{"fields":{"consumerTag":"amq.ctag-LijiOI-nknkknjknj","deliveryTag":16,"redelivered":false,"exchange":"","routingKey":"messages"},"properties":{"headers":{}},"content":{"type":"Buffer","data":[123,34,114,101,97,100,34,58,102,97,108,115,101,44,34,116,105,109,101,83,101,110,116,34,58,49,54,52,54,48,55,52,50,54,52,55,54,53,44,34,109,101,115,115,97,103,101,34,58,34,102,101,114,102,101,114,102,34,44,34,117,115,101,114,73,68,34,58,52,55,44,34,99,111,110,116,97,99,116,73,68,34,58,52,55,125]}}

I am getting this when parsing now; how can I convert this to the actual message object?:

[
  101,  44,  34, 116, 105, 109, 101,  83, 101, 110, 116,  34,
   58,  49,  54,  52,  54,  48,  55,  54,  56,  50,  48,  52,
   58,  34, 101, 101, 102, 101, 119, 102,  34,  44,  34, 117,
  115, 101, 114,  73,  68,  34,  58,  52,  55,  44,  34,  99,
]

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

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

发布评论

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

评论(1

心凉 2025-01-17 18:30:38

只需摆脱缓冲区即可。

    client.send(JSON.stringify(messageObj));

而不是

    client.send(Buffer.from(JSON.stringify(messageObj)));

Just get rid of the Buffer.

    client.send(JSON.stringify(messageObj));

instead of

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