Warning: file_get_contents(/data/phpspider/zhask/data//catemap/2/node.js/40.json): failed to open stream: No such file or directory in /data/phpspider/zhask/libs/function.php on line 167

Warning: Invalid argument supplied for foreach() in /data/phpspider/zhask/libs/tag.function.php on line 1116

Notice: Undefined index: in /data/phpspider/zhask/libs/function.php on line 180

Warning: array_chunk() expects parameter 1 to be array, null given in /data/phpspider/zhask/libs/function.php on line 181

Warning: file_get_contents(/data/phpspider/zhask/data//catemap/3/sockets/2.json): failed to open stream: No such file or directory in /data/phpspider/zhask/libs/function.php on line 167

Warning: Invalid argument supplied for foreach() in /data/phpspider/zhask/libs/tag.function.php on line 1116

Notice: Undefined index: in /data/phpspider/zhask/libs/function.php on line 180

Warning: array_chunk() expects parameter 1 to be array, null given in /data/phpspider/zhask/libs/function.php on line 181
Node.js 是socket.write thread“;安全吗?_Node.js_Sockets - Fatal编程技术网

Node.js 是socket.write thread“;安全吗?

Node.js 是socket.write thread“;安全吗?,node.js,sockets,Node.js,Sockets,我使用node.js通过json中的套接字进行通信。其中一个操作以base64格式通过套接字发送一个大文件。因此,消息被拆分为数据包,并在客户端上触发各种数据事件。我使用客户端的缓冲区来处理这个问题 var response={ file: fs.readFileSync("FileName","base64") } socket.write(JSON.stringify(response)); 在客户端,使用缓冲区 cleartextStream.on("data",function

我使用node.js通过json中的套接字进行通信。其中一个操作以base64格式通过套接字发送一个大文件。因此,消息被拆分为数据包,并在客户端上触发各种数据事件。我使用客户端的缓冲区来处理这个问题

var response={
    file: fs.readFileSync("FileName","base64")
}
socket.write(JSON.stringify(response));
在客户端,使用缓冲区

cleartextStream.on("data",function(data){
            console.log("Received data")

            try {

                data=JSON.parse(data);
                _buffer="";
                events.trigger(data.event,data);
            }
            catch(e){
                console.log("Incomplete message received. Building Buffer");
                _buffer+=data;
                if(/\}$/.test(data) ){//Tests for json boundary
                    data=JSON.parse(_buffer);
                    events.trigger(data.event,data);

                }
            }
这对我来说现在还不错。我预期的唯一问题是,在发送文件时,其他事件是否会触发套接字上的写操作

t=0 Start sending file
t=5 Still sending file
t=6 Another event uses socket.write to start sending message
t=7 still sending file
t=8 Another event sending message
这将导致信息混乱。因此,socket.write会在发送单个消息时阻塞,还是会允许其他方法在传输完成之前使用socket.write