2017-05-23 13 views
0

私は非同期プログラミングの使い方を理解するのに苦労しています。私は接続をリッスンし、パケットを受け取り、鍵ペアを生成し、postgresqlにすべての混乱を格納するモジュールに含まれているデータを渡すtcpサーバを持っています。元のパケット送信者に戻すために、公開鍵をtcpサーバーに戻す必要があります。私が今働いているのは動作しますが、私はそれが公開鍵を残して未定義のものをクライアントに渡すと信じています。私は、私がかなり正しい方法を使っているプロセスを理解していないか、またはプロセス全体を完全に誤解しているかどうかを判断できません。Node.js非同期モジュールの値を返す

var net = require('net'); 
    var keys = require('./genKey.js'); 

    var HOST = '10.0.0.1'; 
    var PORT = 5555; 

    net.createServer(function(sock) { 

     console.log('CONNECTED: ' + sock.remoteAddress +':'+ sock.remotePort); 

     sock.on('data', function(data) { 

      // console.log('DATA ' + sock.remoteAddress + ': ' + data); 

      keys.genKey(data, out); 

      //var public = console.log(String(keys.pub)); 
      **function out (pk) { 
      console.log('out function called'); 
      var public = keys.pub; 
      sock.write(public);** 
      console.log(public); 
      } 

      //var promise = keys.genKey(data); 
      //promise.then(console.log, console.error); 

      //console.log(public); 

      //sock.write(String(keys.pub)); 

     }); 
     sock.on('error', function (exc) { 
     console.log("ignoring exception: " + exc); 
    }); 

     sock.on('close', function(data) { 
      console.log('CLOSED: ' + sock.remoteAddress +' '+ sock.remotePort); 
     }); 

    }).listen(PORT, HOST); 

    console.log('Server listening on ' + HOST +':'+ PORT); 

モジュール:

はここに私のコード

サーバーだ、私は汚いコードをお詫び申し上げます

var Promise = require('promise'); 
    var cp = require('child_process') 
     , assert = require('assert') 
     ; 
    const pool = require('./db'); 

    var prkey = ''; 
    var pukey = ''; 

    var privateKey, publicKey; 
    publicKey = ''; 

    module.exports = { 
    genKey: function(input) { 
    cp.exec('openssl genrsa 2048', function(err, stdout, stderr) { 
     assert.ok(!err); 
     prkey = stdout; 
     privateKey = stdout; 
     //console.log(privateKey); 
     //console.log('stdout: ', stdout); 
     //prkey = privateKey; 
     //console.log(prkey); 
     makepub = cp.spawn('openssl', ['rsa', '-pubout']); 
     makepub.on('exit', function(code) { 
     assert.equal(code, 0); 
     //console.log(publicKey); 
     //pukey = publicKey; 
     //console.log(pukey); 
     }); 
     makepub.stdout.on('data', function(data) { 
     publicKey += data; 
     pukey = String(publicKey); 
     //console.log('stdout: ', publicKey); 
     writeSQL(input, prkey, pukey); 
     //console.log(pukey); 
     //return { 
     //pub:() => pukey 
     // }; 
     **return pukey;** 
     }); 
     makepub.stdout.setEncoding('ascii'); 
     makepub.stdin.write(privateKey); 
     makepub.stdin.end(); 
    //console.log('stdout: ', stdout); 
    }); 

    //console.log('output keys'); 
    //console.log(prkey); 
    //console.log(pukey); 
     //writeSQL(prkey, pukey); 
    } 
    }; 

    function writeSQL(uuid, private, public) { 
     //console.log(private); 
     //console.log(public); 
     //console.log(uuid); 
     console.log('write sql'); 
     //pool.query('INSERT INTO uuidkeys(uuid, public, private) values($1,$2, $3)', 
     // [uuid, public, private]); 

    }; 

、私はそれが非常に醜いとなっているので、多くの異なるものを試してみました。 誰かが私のサーバー機能の適切なポイントで自分の公開キーを私のサーバーに戻す方法を教えてくれれば嬉しいです。うまくいけば、これを理解するために私を正しい道に導くのに十分だろう。

答えて

0

私の頭を壁に打ち負かすと、私は最終的にこの仕組みを理解します。私が読んだドキュメントやチュートリアル(何十というもの)のどれも、これを私のためにクリックした方法で説明したわけではありません。

情報の欠落ビットは、コールバックが何であるかの非常に基本的な説明でした。誰か他の人が私がいたこの苦労を見つけたときに私は私の考えを伝えます。

コールバックは、使用したい関数への呼び出しで指定した別の関数で、元のプロセスに戻るために使用している関数によって呼び出されます。上記の私のコードからそう

サーバー

var keys = require('./genKey3.js'); 
var r = new keys(); 

call the function from the module 
r.generate(data, /*pass this function to that function*/ function 
backed(key) 
{ 
     //do this when the backed function is called from the other function 
     console.log('out function called'); 
     console.log(key); 
     sock.write(key); 
     sock.on('close', function(data) { 
      console.log('CLOSED: ' + sock.remoteAddress +' '+ 
     sock.remotePort); 
     }); 
    }); 

モジュールは

this.generate = function(input, /*get your callback function here*/backed) { 
    var privateKey, publicKey; 
    publicKey = ''; 
    console.log('generate function called'); 
    cp.exec('openssl genrsa 2048', function(err, stdout, stderr) { 
     assert.ok(!err); 
     privateKey = stdout; 
     //console.log(privateKey); 
     makepub = cp.spawn('openssl', ['rsa', '-pubout']); 
     makepub.on('exit', function(code) { 
      assert.equal(code, 0); 
      //console.log(publicKey); 
      writeSQL(input, privateKey, publicKey); 

      //Get your info together and call the function passed from the 
      //starting point to return your data 
      backed(publicKey); 
     }); 
     //console.log('passed makepub.on'); 
     makepub.stdout.on('data', function(data) { 
      publicKey += data; 
     }); 
     makepub.stdout.setEncoding('ascii'); 
     makepub.stdin.write(privateKey); 
     makepub.stdin.end(); 
    }); 
}; 

それは実際にあなたがそれの周りにあなたの頭を取得ケーキだと私はその説明全体を実行した場合、私は思います以前はそれまでに15分かかりました。

関連する問題