2017-02-20 12 views
1

ちょっと私はアプリケーションを作成しています。私は受け取ったメッセージに応じて特定のメッセージを入力するようクライアントに依頼しています。私はサーバーからクライアントにデータを送り返しています。何が起こるかは、ユーザーがクライアント側からデータを入力すると、応答が来る前にクライアントがもう一度「メッセージを入力」します...私はnode.jsのreadlineとnetモジュールを使用していますここに私のコードは次のとおりです。クライアントが別のメッセージを入力する前にサーバーの応答を待つ

var server = net.createServer( //My server side code which sends response. 
function(socket){ 
    console.log("Client connection..."); 


    socket.on('end', function(){ 
     console.log("Client disconnected..."); 

    }); 

    socket.on('data', function(data){ 
     console.log(" Received: ", data.toString()); 

     a=data.toString(); 

     if(a=="lookupByLastName Smith") //json data send if client sends this 
     { 
      arr= employees.lookupByLastName('Smith'); 
      socket.write("Data"+JSON.stringify(arr)); 
     } 




    }); 

    socket.write("Hello from server"); 
}); 

//クライアント側コード:それを解決するために必要な

var rl = readline.createInterface({ 
    input: process.stdin, 
    output: process.stdout 
}); 

var readMessage = function(client) { 
     rl.question("Enter Message: ", function (line){ 
     client.write(line);//enter message appears twice before server res.      
     if (line == "bye") 
      client.end(); 
     else 
      readMessage(client); 
    }); 
}; 

var client = net.connect({port:3000}, 
    function(){ 
     console.log("Connected to server"); 
     readMessage(client); 
    }); 

client.on('end', function(){ 
    console.log("Client disconnected..."); 
    return; 
    }); 

    client.on('data', function(data){ 
    console.log("\n --Received:", data.toString()); 
}); 

Check the second message "Enter message" it shouldn't appear second time till server responds back。だからヘルプ。

答えて

0

サーバーからデータを受信したときにユーザーの入力を求めることができます。

var readline = require('readline'); 
var net = require('net'); 

var port = 3000; 

var helloTimeout; 
var responseTimeout; 
var timeout = 4000; 

var rl = readline.createInterface({ 
    input: process.stdin, 
    output: process.stdout 
}); 

var readMessage = function(client) { 
    rl.question("Enter Message: ", function(line) { 
     client.write(line); //enter message appears twice before server res.      
     if (line == "bye") { 
      client.end(); 
     } else { 
      responseTimeout = setTimeout(function() { 
       console.log('not response from server for ' + timeout + "ms"); 
       client.emit("wait_for_input"); 
      }, timeout); 
     } 
    }); 
}; 

var client = net.connect({ port: port }, 
    function() { 
     console.log("Connected to server"); 
     helloTimeout = setTimeout(function() { 
      console.log('no hello received for ' + timeout + "ms, closing ..."); 
      client.end(); 
     }, timeout); 
    }); 

client.on('end', function() { 
    console.log("Client disconnected..."); 
    rl.close(); 
}); 

client.on('wait_for_input', function() { 
    readMessage(client); 
}); 

client.on('data', function(data) { 
    clearTimeout(helloTimeout); 
    clearTimeout(responseTimeout); 
    console.log("\n --Received:", data.toString()); 
    client.emit("wait_for_input"); 
}); 

私がトリガwait_for_input呼ばclientに別のイベントを作成しました:次の例では

は、私はまた、サーバーハローメッセージとするとき、サーバーが4秒のために、クライアントのメッセージに応答していないため、一部のタイムアウトを追加しましたサーバーからのデータ(このデータの計算後)およびユーザーがコマンドを入力した後にサーバーからの応答を4秒以上受信しなかった場合のみ

+0

ちょうどタイムアウトで問題を解決しましたか?その他の方法で。第二に、サーバーコードは私が投稿したのと同じままです。私はあなたのロジックを取得しませんでした。 –

+0

タイムアウトを忘れると、ユーザーはサーバーからデータを受信した後にのみ入力を要求されます。入力がタイプされた後、クライアントはデータ応答がサーバーなどから来るまで何もしません。 –

関連する問題