Javascript zeromq/zwssock上的Protobuf到JSMQ/Protobuf.js消息未完全接收
我正在通过zwssock(czmq)将Protobuf编码的数据发送到JSMQ.js,然后对Protobuf数据进行解码。zwssock(czmq)是zcmq的websocket扩展Javascript zeromq/zwssock上的Protobuf到JSMQ/Protobuf.js消息未完全接收,javascript,c,protocol-buffers,zeromq,Javascript,C,Protocol Buffers,Zeromq,我正在通过zwssock(czmq)将Protobuf编码的数据发送到JSMQ.js,然后对Protobuf数据进行解码。zwssock(czmq)是zcmq的websocket扩展 操作系统:Windows 浏览器:Chrome 40.0.2 ØMQ:4.0.4 czmq:2.2.0 出现以下问题: 接收的数据不包含已发送的所有字节。 事实上,检查后发现,接收到的所有字节最多为前0个字节 实例: 数据发送: 10,4,78,77,69,65,-110,3,19,8101,-86,6,14,8,
function onmessage(ev) {
if (ev.data instanceof Blob) {
var arrayBuffer;
var fileReader = new FileReader();
fileReader.onload = function () {
processFrame(this.result);
};
fileReader.readAsArrayBuffer(ev.data);
} else if (ev.data instanceof ArrayBuffer) {
processFrame(ev.data);
}
// Other message type are not supported and will just be dropped
};
function processFrame(frame) {
var view = new Uint8Array(frame);
var more = view[0];
if (incomingMessage == null) {
incomingMessage = new JSMQ.Message();
}
incomingMessage.addBuffer(frame);
// last message
if (more == 0) {
if (that.onMessage != null) {
that.onMessage(that, incomingMessage);
}
incomingMessage = null;
}
}
在onmessage/processFrame中,接收的数据已经不包含完整的字节序列。如您所见,接收到的字节序列以0开头,与[more==0]保护相匹配
我无法让wireshark嗅探发送的包裹,正在检查
如果发送的字节不正确
一种解决方案是使用bytestuffing,从而删除所有0字节。
但我肯定是在什么地方出错了
应要求:
内部,我们使用ZrOMQ的C++库,但是自从WebSoCube 目前,我们需要将c版本的扩展信息转换为c样式的消息。如前所述,数据已填充
void CZeroMQConnection::Send(zmq::message_t& message)
{
zmsg_t* msg = zmsg_new();
std::vector<unsigned char> rpl;
std::copy_n(reinterpret_cast<char*>(message.data()), message.size(),std::back_inserter(rpl));
// Temporary stuffing on Websockets
char stuffChar = 1;
char invalidChar = 0;
std::vector<unsigned char> stuffed;
for (auto it = rpl.begin(); it != rpl.end(); ++it)
{
if (*it == invalidChar || *it == stuffChar)
{
stuffed.push_back(stuffChar);
stuffed.push_back((*it) + 1);
}
else
stuffed.push_back(*it);
}
// As mentioned added extra 0 byte, preventing superfluos data
stuffed.push_back(0);
zmsg_push(msg, _id);
zmsg_addstr(msg, reinterpret_cast<char*> (&stuffed[0]));
zwssock_send(_socket, &msg);
}
void CZeroMQConnection::Send(zmq::message\u t&message)
{
zmsg_t*msg=zmsg_new();
std::向量rpl;
std::copy_n(reinterpret_cast(message.data())、message.size()、std::back_inserter(rpl));
//腹板上的临时填料
char-stuffChar=1;
char invalidChar=0;
std::载体填充;
for(auto it=rpl.begin();it!=rpl.end();++it)
{
if(*it==invalidChar | |*it==stuffChar)
{
填充。推回(填充字符);
填充。推回((*it)+1);
}
其他的
塞满。推回(*它);
}
//如上所述,添加了额外的0字节,防止了多余的数据
填充。推回(0);
zmsg_推送(消息,id);
zmsg_addstr(msg,重新解释演员阵容(&cullet[0]);
zwssock_send(_socket,&msg);
}
目前还没有双向数据流,这将在不久的将来出现。我试图用chrome(40.0.2)重现这个问题,但没有成功,我得到了包括零在内的全部信息 c代码:
static char *listen_on = "tcp://127.0.0.1:86";
int main(int argc, char **argv)
{
zctx_t *ctx;
zwssock_t *sock;
char *l = argc > 1 ? argv[1] : listen_on;
ctx = zctx_new();
sock = zwssock_new_router(ctx);
zwssock_bind(sock, l);
zmsg_t* msg;
zframe_t *id;
while (!zctx_interrupted)
{
msg = zwssock_recv(sock);
if (!msg)
break;
// first message is the routing id
id = zmsg_pop(msg);
while (zmsg_size(msg) != 0)
{
char * str = zmsg_popstr(msg);
printf("%s\n", str);
free(str);
}
zmsg_destroy(&msg);
msg = zmsg_new();
zmsg_push(msg, id);
char buf[226] = { 10, 4, 78, 77, 69, 65, -110, 3, 19, 8, 101, -86, 6, 14, 8, 1, 16, -40, -126, -27, -14, -12,
-87, -61, 2, -16, 1, 1, -110, 3, 93, 8, 100, 80, 0, -94, 6, 86, 34, 73, 36, 71, 80, 82,
77, 67, 44, 49, 48, 51, 50, 51, 49, 46, 54, 48, 49, 44, 86, 44, 53, 50, 48, 53, 46, 54, 52, 50,
54, 55, 48, 44, 78, 44, 48, 48, 54, 49, 57, 46, 54, 49, 51, 57, 52, 55, 44, 69, 44, 52, 51, 46,
50, 44, 51, 54, 46, 52, 44, 50, 51, 48, 49, 49, 53, 4, 4, 44, 44, 83, 42, 54, 68, 32, 74, 9, 69, 80,
83, 71, 58, 52, 51, 50, 54, -30, 3, 97, 10, 4, 78, 77, 69, 65, 18, 73, 36, 71, 80, 82, 77, 67, 44, 49,
48, 51, 50, 51, 49, 46, 54, 48, 49, 44, 86, 44, 53, 50, 48, 53, 4, 6, 54, 52, 50, 54, 55, 48, 44, 78,
44, 48, 48, 54, 49, 57, 46, 54, 49, 51, 57, 52, 55, 44, 69, 44, 52, 51, 46, 50, 44, 51, 54, 46, 52,
44, 50, 51, 48, 49, 49, 53, 44, 44, 44, 83, 42, 54, 68, 32, 82, 14, 10, 5, 8, 2, -80, 1, 2, -94, 6, 4, 8, 8, 16, 6 };
zmsg_addmem(msg, buf, 226);
zwssock_send(sock, &msg);
}
zwssock_destroy(&sock);
zctx_destroy(&ctx);
}
以及以下javascript:
var dealer = new JSMQ.Dealer();
dealer.connect("ws://localhost:86");
// we must wait for the dealer to be connected before we can send messages, any messages we are trying to send
// while the dealer is not connected will be dropped
dealer.sendReady = function() {
var message = new JSMQ.Message();
message.addString("Hello");
dealer.send(message);
};
dealer.onMessage = function (message) {
// the response from the server
var buffer = message.popBuffer();
console.log(buffer.length);
console.log(buffer);
};
function send() {
var message = new JSMQ.Message();
message.addString(document.getElementById("messageTextBox").value);
dealer.send(message);
}
谢谢somdoron
您的帖子使用:
zmsg_addmem(msg, buf, 226);
当我使用时:
zmsg_addstr(msg, reinterpret_cast<char*> (&stuffed[0]));
zmsg_addstr(msg,reinterpret_cast(&fucked[0]);
这可能会将输入解释为C字符串
这解决了问题,thanx很多 是以两种方式发生还是仅从zeromq到jsmq?是否发送多部分消息?你能用zwssock添加代码吗?@somdoron我在周二之前无法访问src,我会尽快发送示例。我看到前面的0字节的修复已经被提出。我确实执行了字节填充,并且能够获取所有数据,但是我不得不在缓冲区的末尾添加一个额外的0字节,因为我在接收端获取了太多的数据。@somdoron,更新了代码