
用 c#写了一个感觉挺简单,毕竟封装得很无脑了。
js 写的话完全没头绪...(好吧,其实就是懒)
因为接收到的数据 arrayBuffer
var ws = new WebSocket("ws://localhost:8899");
ws.binaryType = "arraybuffer";
……
ws.send("看到这行字的人,鸡鸡缩短 10cm");
……
ws.onmessage = (e) =>{
var blob = new Blob([new Uint8Array(e.data)]);
var reader = new FileReader();
reader.onload = (evt) => {
console.log(reader.result);
};
reader.readAsText(blob, "GB2312");
}
//这样转字符串不会乱码
现在纠结是 arrayBuffer to hex ?? blob to hex ?? string to hex ??
找到的 js string to hex function 基本都是英文没什么问题,但是中文我将收获一段乱码?
怎么转才不会出现乱码呢?
stackoverflow 都翻遍了,jsdelivr 也翻遍了,鱼也摸过了。