开发者

Node.js , data getting cycled back and forth between server and client

I wrote a small node.js server using "net" module. the client and server code looks like follows Client.. socket is declared in global and invoked by this function. i am using a 3rd party API for sockets which works fine with php or C++ socket.so its surely not an API based 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
            }

where Jparse is a function which parses the json sent by server according and data is sent using socket.send(input);

ANd the server looks like this ::

..

    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){
    cons开发者_高级运维ole.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");

When i send data using the client HTML Page the data goes to server and comes back as its supposed to but the process becomes recurrent and i get the same recieved more then once on both the sides i mean the server and the client ..

say. if i send "hello" from client it recieves 2-3 "hello" back on client side from server.. an importing thing that i just noticed is the number of recurrsions increases everytime i send a data to server.

Anyone got a clue on whats wrong ?


Every single time you execute the outer on('data' ... , you're adding another on('data' ... element; that's why you're getting the increased number at each request. You're modifying your handler structure to add handlers in your handler.

0

上一篇:

下一篇:

精彩评论

暂无评论...
验证码 换一张
取 消

最新问答

问答排行榜