我正在嘗試爲演示放置一個快速的node.js/edge.js/C#橋。從edge.js與C#主機通信
我必須使用".Net calling Node.js"樣式,因爲現有的C#代碼使用了許多配置值,我無法將其添加到node.exe.config,因爲我需要同時運行多個版本。
所以我有這樣的代碼:
private static async Task Start() {
Func<object, Task<object>> edge = EdgeJs.Edge.Func(@"
var login = require('login.js');
var edge = require('edge')
login({ email: '[email protected]', password: 'shh' }, function callback(err, api) {
if (err) return console.error(err);
// This will keep listening until terminated
api.listen(function callback(err, message) {
if (err) return console.error(err);
// At this point I need to send the message back to this class so it can be processed..
console.log(message); // send the message to C#
// ... and then return the response via the api
api.send('response goes here');
});
});
return function (data, callback) {
callback(null, er...);
}
");
}
因此,該代碼是在一個事件循環等待消息和響應。這一切都適用於硬編碼值。但我需要將消息提交回C#進行處理,並且我無法弄清楚如何在edge.js和C#應用程序之間來回通信。
它肯定是通過回調,但我似乎無法開始弄清楚如何構造它,時間越來越短。我絕不是JavaScript專家。
如何在使用回調的事件循環中在邊緣代碼和C#代碼之間進行通信?
這是非常有用的謝謝你:不幸的是邊緣不喜歡被多次調用(呼叫邊緣回調終止邊緣會話),所以我不得不建立它。 – stuartd