我正在编写TCP客户端。对于小型响应包,它运作良好。当响应在不同的包上时(chrome.sockets.tcp.onReceive.addListener多次调用),我遇到了问题。它们有时以正确的顺序到达,有时变量“索引”没有意义。你如何在javascript中处理它?</ p>
var index = 0,
effectiveSize = 0,
expectedLength = 0,
buf = [];
chrome.sockets.tcp.send(socketId, data, function(sendInfo) {});
chrome.sockets.tcp.onReceive.addListener(function(info) {
effectiveSize += info.data.byteLength;
if (index === 0) {
offset = 0;
readBuffer(info.data, 4); // header
expectedLength = readBuffer(info.data, 4);
}
console.log('index: ' + index + ' expectedLength: ' + expectedLength + ' effectiveSize: ' + effectiveSize);
index++;
}