问题描述
以下代码产生一个神秘的第三帧:451-["clientMsg",{"_placeholder":true,"num":0}]
The following code results in a mysterious third frame:451-["clientMsg",{"_placeholder":true,"num":0}]
function sendMessage() {
var bufArr = new ArrayBuffer(4);
var bufView = new Uint8Array(bufArr);
bufView[0]=6;
bufView[1]=7;
bufView[2]=8;
bufView[3]=9;
// send binary message to server
socket.emit('serverMsg', bufArr);
}
sendMessage();
服务器
socket.on('serverMsg', function (bufArr) {
var ba = new ArrayBuffer(4);
var bv = new Uint8Array(ba);
bv[0]=10;
bv[1]=11;
bv[2]=12;
bv[3]=13;
var bufView = new Uint8Array(bufArr);
console.log("Data: ", bufView[0], bufView[1], bufView[2], bufView[3]);
// Send message back to client
socket.emit("clientMsg", ba);
});
客户
socket.on('clientMsg', function (bufArr) {
var bufView = new Uint8Array(bufArr);
console.log("Data: ", bufView[0], bufView[1], bufView[2], bufView[3])
});
上面的代码产生了三个帧,我希望它们只有两个帧,一个从客户端到服务器,一个从服务器到客户端.谁能解释这个第三帧是什么,以及如何摆脱它?查看打击截图:
The above code is resulting in THREE frames I would expect only TWO frames, one from the client to the server and one from the server to the client. Can anyone explain what this third frame is, and how to get rid of it? See the blow screenshot:
推荐答案
浏览socket.io的代码,似乎只要有一个带有二进制数据的对象,它将使用该占位符事件替换该对象的该部分.然后,它按照提供的"num"字段的顺序发送二进制数据,并在客户端重新构建.
Looking through socket.io's code, it looks like any time there is an object with binary data, it will replace that portion of the object with this placeholder event. It then sends the binary data in the order of the provided 'num' field, which it reconstructs on the client side.
由于只有一个对象要为事件"clientMsg"发送,因此它以占位符对象为整体发送事件,并跟踪二进制数据.
Since you only have a single object you are sending out for the event 'clientMsg', it sends out the event with the placeholder object as the entire body, with a followup of the binary data.
请参阅socket.io-parser的binary.js: https: //github.com/socketio/socket.io-parser/blob/master/binary.js -特别是_deconstructPacket
和_reconstructPacket
函数.
See socket.io-parser's binary.js: https://github.com/socketio/socket.io-parser/blob/master/binary.js - specifically the _deconstructPacket
and _reconstructPacket
functions.
不幸的是,我也意识到添加二进制文件的开销相当大,除了代码外,我找不到很多文档.
I unfortunately also realized the overhead of adding binary is pretty rough, and I can't find much documentation other than the code.
这篇关于正在发送Socket.io额外占位符框架的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持!