Node.js,数据在服务器和客户端之间来回循环

时间:2011-07-25 15:25:10

标签: html5 node.js

我用了一个小node.js服务器 “网”模块。客户端和服务器代码如下所示 客户.. socket在全局声明并由此函数调用。我正在使用第三方API的套接字,它可以正常使用php或C ++ socket。所以它肯定不是基于API的bug。

                   function SocketAPI()
            {
                socket.onConnect = function(success)
                {
                    if(success)
                    {
                        Connected = true;
                    }else{
                        alert("Failed to connect to server!");
                    }
                }   
                socket.onClose = function()
                {
                    alert("Connection lost to server!");
                }
                socket.onData = function(text)
                {
                    if(text==">Send_AuthenTication")
                    { // Tells the client to send authentication data
                        if(localStorage.ID)
                        {
                            socket.send(">"+localStorage.ID); //encrypted
                        }else{
                            socket.send("<");
                        }

                    }else if(text.substr(0,2)==">>"){
                        // User inforamation accept and apply it
                        Packet = JSON.parse(text.substr(2,text.length-2));
                        while(Users.length){ // users is an array which contains    other peers connected to the server 
                        Users.pop();
                        }
                        me = Packet;
                            me.Prepare();
                        Users.push(me.NameBOX);
                        Usrlst.ShowTab(0);
                        // Now make the socket only listen for output buffer.
                        socket.onData = function( Data ){
                            //alert(Data);
                            setTimeout( Jparse,100, Data );
                        } 
                    }

                }
                socket.connect('localhost',1337);// makes connection
            }

其中Jparse是一个解析服务器发送的json的函数 并使用socket.send(输入)发送数据;

服务器看起来像这样::

...

    var server = net.createServer(function(socket){
socket.setEncoding('utf8');
socket.Peer = new Client();

socket.on('data',function(Data){
    if(Data[0]==">") // this tells that the request is authentication event
    {
     // Loads from Pool
    }else if(Data[0]=="<"){
        NewUSER(this.Peer); // Makes new user
    }
    this.write(">>"+JSON.stringify(this.Peer)+"\0","utf8",function(){});
    this.on('data',function(Data){
        console.log(Data);
        this.write(Data,'utf8',function(){});
    });
});
socket.on('end',function(){
    console.log("Connection End");
});
// Socket Closed by client/errors
socket.on('close',function(error){
    console.log("Connection is closed by Had errors?:"+error);
});
// Handshake started
socket.on("connect",function()
{ // add server connection in here aha we now have a new user.
    sys.puts("Connection from"+ socket.remoteAddress);
    this.write(">Send_AuthenTication\0",'utf8',function(){});
});

   });
 // starting the server.
 server.listen(1337,"localhost");

当我使用客户端HTML页面发送数据时,数据会发送到服务器并按照预期返回,但是进程变得经常发生,我得到的相同内容更多一次,我指的是服务器和客户端。

说。如果我从客户端发送“hello”,它会在客户端从服务器上收到2-3“hello”...我刚注意到的导入内容是每次向服务器发送数据时,reurrsions的数量都会增加。

任何人都知道什么是错的?

1 个答案:

答案 0 :(得分:2)

每次执行外部on('data' ...时,您都会添加另一个on('data' ...元素;这就是为什么你在每次请求时获得增加的数字。您正在修改处理程序结构以在处理程序中添加处理程序。