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

Warning: file_get_contents(/data/phpspider/zhask/data//catemap/6/haskell/9.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 NodeJS上多人游戏的TCP套接字处理_Node.js_Sockets_Tcp - Fatal编程技术网

Node.js NodeJS上多人游戏的TCP套接字处理

Node.js NodeJS上多人游戏的TCP套接字处理,node.js,sockets,tcp,Node.js,Sockets,Tcp,我有一个多人游戏,我的服务器使用nodejs和TCPSocket(net.createServer)与客户端通信 我有成千上万的客户机连接到服务器,许多人抱怨他们经常与服务器断开连接 下面是我的服务器现在处理连接的方式: Init: var net = require("net"); server = net.createServer(function(socket) { socket.setTimeout(15000); socket.setKeepAlive(true);

我有一个多人游戏,我的服务器使用nodejs和TCPSocket(net.createServer)与客户端通信

我有成千上万的客户机连接到服务器,许多人抱怨他们经常与服务器断开连接

下面是我的服务器现在处理连接的方式:

Init:

var net = require("net"); 
server = net.createServer(function(socket) {
    socket.setTimeout(15000);
    socket.setKeepAlive(true);
    socket.myBuffer = "";
    socket.msgsQue = [];

    socket.on("data", onData); 
    socket.on("error", onError);            
    socket.on("end", onClientDisconnect);
    socket.on("timeout", onClientDisconnect); 
});  
server.listen(port);
var stringData = JSON.stringify({name:event, message:data});
if (!this.socket.msgsQue || typeof this.socket.msgsQue == "undefined") 
        this.socket.msgsQue = [];

this.socket.msgsQue.unshift(stringData);
var i = this.socket.msgsQue.length;
while(i--) {
    if (this.socket.writable) {
        var elem = this.socket.msgsQue[i];
        this.socket.write(elem+"\0");
        this.socket.msgsQue.splice(i, 1);
     } else {
         //Unable to write at index "i"
         break;//will send the rest on the next attempt
     }
}
发送到客户端:

var net = require("net"); 
server = net.createServer(function(socket) {
    socket.setTimeout(15000);
    socket.setKeepAlive(true);
    socket.myBuffer = "";
    socket.msgsQue = [];

    socket.on("data", onData); 
    socket.on("error", onError);            
    socket.on("end", onClientDisconnect);
    socket.on("timeout", onClientDisconnect); 
});  
server.listen(port);
var stringData = JSON.stringify({name:event, message:data});
if (!this.socket.msgsQue || typeof this.socket.msgsQue == "undefined") 
        this.socket.msgsQue = [];

this.socket.msgsQue.unshift(stringData);
var i = this.socket.msgsQue.length;
while(i--) {
    if (this.socket.writable) {
        var elem = this.socket.msgsQue[i];
        this.socket.write(elem+"\0");
        this.socket.msgsQue.splice(i, 1);
     } else {
         //Unable to write at index "i"
         break;//will send the rest on the next attempt
     }
}
断开连接时

var onClientDisconnect = function() {
    this.myBuffer = "";
    delete this.myBuffer;
    this.msgsQue = [];
    delete this.msgsQue;
    this.destroy();
}
从客户处接收

    var onData = function(data) {
        if (!data || !data.toString()) return;
        var raw = data.toString();
        this.myBuffer += raw;
        var d_index = this.myBuffer.indexOf('\0'); // Find the delimiter

         // While loop to keep going until no delimiter can be found
        var string;
        while (d_index > -1) {
            // Create string up until the delimiter                 
            string = this.myBuffer.substring(0,d_index);
            // Cuts off the processed chunk          
            this.myBuffer = this.myBuffer.substring(d_index+1); 
            onMessage(string, this);//handle
            // Find the new delimiter
            d_index = this.myBuffer.indexOf('\0'); 
        } 
    }
我注意到的一个问题是,
msgsQue
变得巨大,因为套接字不可写,但断开连接处理程序没有被激发(或稍后被雇佣)

你能给我一些如何优化的建议吗?

我注意到有时会断开连接,但我可以ping服务器,因此这肯定是一个与服务器相关的问题。这可能是因为服务器本身的高负载吗


(我不想使用socket.io,因为去年我遇到了很多问题,如内存泄漏、服务器应用程序冻结、不支持等)

无法给出任何建议,但是的,它可能与平均负载高有关,例如,这可能会导致负载平衡器出现,并且可能会产生干扰。