节点 JS TCP 服务器不立即刷新缓冲区中的数据
Node JS TCP Server not flushing immediately the data from buffer
我有以下代码:
// Setting up the TCP:
var server = net.createServer(function(socket) {
console.log('Android Client connected');
socket.setNoDelay(true);
socket.on('data', function(data) {
console.log(data.toString());
var json = JSON.parse(data.toString());
if(json.id == 1) {
socket.world = findAnEmptyWorld();
socket.player = socket.world.addPlayer(json.data);
socket.player.socket = socket;
tcpClients.push(socket);
socket.write(JSON.stringify({id: 3, data: socket.player.toJSON()}));
socket.pipe(socket);
}
});
});
server.listen(4444, function() {
console.log("TCP server listening on port 4444");
});
当我写入消息时,它存储在一个缓冲区中,但只有在我关闭服务器后才会刷新...我应该怎么做才能在写入后立即刷新缓冲区? (如你所见,我尝试 'setNoDelay' 为真)
我应该在字符串末尾换一行...f# 是什么?^&k....
像这样
socket.write(JSON.stringify({id: 3, data: socket.player.toJSON()}) + "\n");
我有以下代码:
// Setting up the TCP:
var server = net.createServer(function(socket) {
console.log('Android Client connected');
socket.setNoDelay(true);
socket.on('data', function(data) {
console.log(data.toString());
var json = JSON.parse(data.toString());
if(json.id == 1) {
socket.world = findAnEmptyWorld();
socket.player = socket.world.addPlayer(json.data);
socket.player.socket = socket;
tcpClients.push(socket);
socket.write(JSON.stringify({id: 3, data: socket.player.toJSON()}));
socket.pipe(socket);
}
});
});
server.listen(4444, function() {
console.log("TCP server listening on port 4444");
});
当我写入消息时,它存储在一个缓冲区中,但只有在我关闭服务器后才会刷新...我应该怎么做才能在写入后立即刷新缓冲区? (如你所见,我尝试 'setNoDelay' 为真)
我应该在字符串末尾换一行...f# 是什么?^&k.... 像这样
socket.write(JSON.stringify({id: 3, data: socket.player.toJSON()}) + "\n");