如何在客户端解码websocket数据?
我花了很多时间尝试从服务器解码缓冲区,但我还没能弄清楚它的位置。
我尝试删除 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 技术交流群。
绑定邮箱获取回复消息
由于您还没有绑定你的真实邮箱,如果其他用户或者作者回复了您的评论,将不能在第一时间通知您!
发布评论
评论(1)
只需摆脱缓冲区即可。
而不是
Just get rid of the Buffer.
instead of