我在 chrome 打包的应用程序中构建了一个 websocket 服务器。我让一切正常(从客户端到服务器的握手和接收)但是,当我尝试从服务器向客户端发送消息时,它总是出现错误:“失败:服务器不能屏蔽它发送给客户端的任何帧。”
这是我的 prepareMessage 代码:
var sendText = _toUTF8Array(JSON.stringify(msg) + '\n');
var aa;
var buffer;
if (sendText.length > 125) {
if (sendText.length < 65536) {
aa = new ArrayBuffer(4 + sendText.length);
buffer = new Uint8Array(aa);
buffer[0] = 129;
buffer[1] = 126;
buffer[2] = sendText.length >> 8;
buffer[3] = sendText.length & 255;
_arrayCopy(sendText, 0, buffer, 4, sendText.length);
}
else {
aa = new ArrayBuffer(10 + sendText.length);
buffer = new Uint8Array(aa);
buffer[0] = 129;
buffer[1] = 127;
var len = new Uint8Array(8);
len = _Uint64ToUint8(sendText.length);
var tt = new Uint8Array(8);
for (var i = 0; i < 8; i++)
{
tt[7 - i] = len[i];
}
_arrayCopy(tt, 0, buffer, 2, 8);
_arrayCopy(sendText, 0, buffer, 10, sendText.length);
}
}
else {
aa = new ArrayBuffer(2 + sendText.length);
buffer = new Uint8Array(aa);
buffer[0] = 129;
buffer[1] = sendText.length;
_arrayCopy(sendText, 0, buffer, 2, sendText.length);
}
return aa;