我正在从Java + Rhino编写TCP服务器到nodeJS ...
我遇到了阻止执行代码而没有阻塞TCP侦听器的问题(socket.on('data',()=> {})...
为什么我需要它:我需要通过挂起GUI来实现同步响应...在Java中,它由子TCP侦听器进程覆盖,并且在主进程中有一个循环,如:
// simulate message loop
boolean enddialog = false;
int endtype = 0;
try {
while (!end) {
String s = "";
s = ess.dequeueCommand();
if (s.equals("mennu1")) {
end = true;
endtype = 0;
}
if (s.equals("menu2")) {
end = true;
endtype = 1;
}
}
} catch (Exception e) {
}
Deasync通过让DB调用'同步'来帮助我......
var deasync = require('deasync');
module.exports = function DB(){
var knex = require('knex')(require("./../../config").DB);
this.executeRowsetParam = (sql, param) => {
let done = false;
knex.raw(sql, param).then((r => {
done = r;
})).catch((e => {
throw "Error in query "+ e
}))
deasync.loopWhile(function () { return !done; });
return done
}
}
我尝试过deasync但它会阻止事件监听器...... 我让它与Promise一起工作,但是在这种情况下它不起作用,因为用“旧”服务器用js编写的代码库,我没办法重写那个代码库 承诺的例子:
return new Promise( (res) => {
if(this.sel == null){
let callb = (d)=>{
var ed = Buffer.from(d);
this.GetDataStricte(ed).then((da)=>{
res(da);
this.socket.removeListener('data',callb);
});
}
this.sel = this.socket.on('data',callb);
}
});
你可以帮帮我吗?