我可以使用 vanilla js 在开发者工具中解码 websocket 消息吗??
这是示例代码:
(function(send) {
WebSocket.prototype.send = function(data) {
if (data instanceof Uint8Array) {
console.log("WebSocket sent Uint8Array:", data);
}
return send.apply(this, arguments);
};
})(WebSocket.prototype.send);
这是控制台日志:
WebSocket sent Uint8Array:
Uint8Array(18) [0, 65, 105, 0, 0, 0, 26, 1, 0, 0, 24, 1, 52, 1, 93, 165, 121, 62, buffer: ArrayBuffer(32), byteLength: 18, byteOffset: 0, length: 18, Symbol(Symbol.toStringTag): 'Uint8Array']
bootstrap-c245bc32c3.js:1
WebSocket sent Uint8Array:
Uint8Array(7) [0, 70, 58, 26, 0, 1, 0, buffer: ArrayBuffer(32), byteLength: 7, byteOffset: 0, length: 7, Symbol(Symbol.toStringTag): 'Uint8Array']
是否可以使用 vanilla javascript 解码消息 websocket?
假设消息是utf-8字符串,可以使用TextDecoder:
new TextDecoder().decode(data)