我很难掌握如何使用异步编程。我有一个tcp服务器,它侦听连接,接受数据包,然后将包含的数据传递给生成密钥对的模块,并将整个混乱存储在postgresql中。我需要将公钥返回到tcp服务器以传递回原始数据包发送方。我现在有什么工作,但我相信它留下了公钥,并将未定义传递给客户端。我不能决定我是否只是不理解我正在使用的过程,或者我是否完全误解了整个过程。
这是我的代码
服务器:
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]);
};
模块:
A
我为凌乱的代码道歉,我尝试了很多不同的东西,它变得非常难看。 如果有人能告诉我如何在我的服务器功能中的正确位置将我的公钥返回到我的服务器那将是很好的。希望这足以让我走上正确的道路来理解这一点。
答案 0 :(得分: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分钟来达到这一点。