nodejs套接字编程 - 发送数据长度

时间:2016-06-26 14:13:31

标签: javascript node.js sockets io

我有一个服务器正在侦听特定端口,并且需要以下方式命令 -

前四个字节应包含命令的长度,剩余字节包含实际命令。例如:

如果我要发送的命令是{cmd:"EL",ptno:1234},我发送的前四个字节应该以big-endian表示法包含数字20,因为命令长度为20,因为命令是UTF-8格式。我发送的剩余字节将包含命令。

我想知道如何在nodejs中执行此操作。此外,当服务器发回数据时,我需要读取前四个字节并确定数据长度并相应地读取套接字输入流。请帮忙。

1 个答案:

答案 0 :(得分:2)

我做了一个完全符合你需要的lib:https://www.npmjs.com/package/node-easysocket(除了在Little Endian中编写4个字节,但它很容易修复)

关于你的问题,我们走了:

对于发送消息,比接收要简单得多,您只需要将消息转换为ByteArray并添加一个包含ByteArray + 4大小的整数(4字节Big Endian):

var buffer = new Buffer("Hello World or a JSON String", "binary");

//create a buffer with +4 bytes
var consolidatedBuffer = new Buffer(4 + buffer.length);

//write at the beginning of the buffer, the total size
consolidatedBuffer.writeInt32BE(buffer.length, 0);

//Copy the message buffer to the consolidated buffer at position 4     (after the 4 bytes about the size)
buffer.copy(consolidatedBuffer, 4);

//Send the consolidated buffer
socket.write(consolidatedBuffer, function(err) {
     if (err) console.log(err)
});

如果你想阅读,它会有点复杂,因为你有可能读取一个拼接在一起的缓冲区。

示例:我的缓冲区大小为10mb,但我的网络连接每秒可传输~100个字节,因此服务器将接收大量数据,您需要将它们存储起来,直到根据通知长度完成所需的大小在前4个字节中。

Javascript,如果是动态语言,那么我可以创建一个运行时属性,套接字对象来存储收集的块:

socket.on('data', function(data) {
    console.log("server bytes in:"+data.length);
    receive(socket,data);
});


function receive(socket, data){
    //Create a chunk prop if it does not exist
    if(!socket.chunk){
        socket.chunck = {
            messageSize : 0,
            buffer: new Buffer(0),
            bufferStack: new Buffer(0)
        };
    }
    //store the incoming data
    socket.chunck.bufferStack = Buffer.concat([socket.chunck.bufferStack, data]);
    //this is to check if you have a second message incoming in the tail of the first
    var reCheck = false;
    do {
        reCheck = false;
        //if message size == 0 you got a new message so read the message size (first 4 bytes)
        if (socket.chunck.messageSize == 0 && socket.chunck.bufferStack.length >= 4) {
            socket.chunck.messageSize = socket.chunck.bufferStack.readInt32BE(0);
        }

        //After read the message size (!= 0) and the bufferstack is completed and/or the incoming data contains more data (the next message)
        if (socket.chunck.messageSize != 0 && socket.chunck.bufferStack.length >= socket.chunck.messageSize + 4) {
            var buffer = socket.chunck.bufferStack.slice(4, socket.chunck.messageSize + 4);
            socket.chunck.messageSize = 0;
            socket.chunck.bufferStack = socket.chunck.bufferStack.slice(buffer.length + 4);
            onMessage(socket, buffer);
            //if the stack contains more data after read the entire message, maybe you got a new message, so it will verify the next 4 bytes and so on...
            reCheck = socket.chunck.bufferStack.length > 0;
        }
    } while (reCheck);
}

function onMessage(socket, buffer){
    console.log("message received from: "+socket+" with data:"+data.toString()+");
}