2011-07-25 78 views
1

我使用 「net」模塊編寫了一個小型node.js服務器。客戶端和服務器代碼如下所示 Client .. 套接字在全局中聲明並由此函數調用。我正在使用第三方API的套接字,它可以很好地使用php或C++套接字。所以它肯定不是基於API的錯誤。Node.js,數據在服務器和客戶端之間來回循環

    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」。我剛剛注意到的一個導入事件是每次向服務器發送數據時遞增的遞歸次數。

任何人都知道什麼是錯的線索?

+0

是沒有可能的錯誤是越來越引起由於改變昂達事件服務器和客戶端上的一個認證的我糟糕的代碼的做法是完全..我該使用如果不是直接更改事件 – ShrekOverflow

回答

2

每次執行外部的on('data' ...時,都會添加另一個on('data' ...元素;這就是爲什麼你會在每個請求中獲得增加的數字。您正在修改您的處理程序結構以在處理程序中添加處理程序。

+0

O操作。那intresting我認爲在(「數據」,函數(){})將取代外的數據,但我錯了......嗯soundz intresting。 – ShrekOverflow

+0

非常感謝..其工作!:D:D:D:D:D我希望我可以投票你回答.. – ShrekOverflow

相關問題