2016-04-25 36 views
0

我正在嘗試爲演示放置一個快速的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#代碼之間進行通信?

回答

0

我已經結束了是這樣的:有上傳遞的數據定義的函數邊緣,然後在收到新消息時調用哪條邊。然後該函數等待響應,並將其傳回邊緣,該邊緣接收(當然)另一個回調的結果。

private static async Task Start() { 
    dynamic payload = new ExpandoObject(); 
    payload.msgHook = NewMessage; 
    payload.login = new { 
     email, 
     password 
    }; 

    var receive = Edge.Func(@"  

      return function(payload, edge_callback) { 

       var login = require('index.js'); 

       login({ 
        email: payload.login.email, 
        password: payload.login.password 
       }, function callback(err, api) { 

        if (err) { 
         edge_callback(err); 
        } 

        api.listen(function callback(err, message) { 
         if (err) { edge_callback(err); } 

         payload.msgHook(message, 
          function callback(err, result) { 
           if (err) { 
            edge_callback(err); 
           } 


           var msg = { 
             body: result.body, 
             url: result.url 
            } 

           api.sendMessage(msg, result.threadId); 
          });     
         }); 
        }); 
       }  
     "); 

    var _ = await receive(payload) as IDictionary<string, object>; 
} 

private static Func<object, Task<object>> NewMessage { 
    get { 
     Func<object, Task<object>> hook = async m => { 
       string body, threadId; 

       if (!ProcessMessage(m as IDictionary<string, object>, out body, out threadId)) { 
        log.Error("Failed to process message: " + m.ToString()); 
       } 

       api.SendMessage(body, threadId, phone); 
       var reply = await waitForReply(threadId); 

      var result = new { 
       body = reply 
      }; 

      // Return the _result_ of the task. 
      return Task.FromResult<object>(result).Result; 
     }; 

     return hook; 
    } 
} 
1

你是對的,它是通過回調。由於您使用異步代碼,您必須包裝所有的代碼返回(邊緣)函數裏面,像這樣:

private static async Task Start() { 
    Func<object, Task<object>> edge = EdgeJs.Edge.Func(@" 
     // edge_callback is used to return values to the C# code 
     return function(data, edge_callback) { 
      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); 
      // possible enhancement here by letting C# know there is an error 
      // edge_callback(err); 

      // This will keep listening until terminated 
      api.listen(function callback(err, message) { 
       if (err) return console.error(err); 
       // same thing here: edge_callback(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# 
       // use the callback, first param is error if there is any, second is the data 
       edge_callback(null, message); 

       // ... and then return the response via the api 
       api.send('response goes here'); 
      }); 
      }); 
     }  
    ");  
} 
+0

這是非常有用的謝謝你:不幸的是邊緣不喜歡被多次調用(呼叫邊緣回調終止邊緣會話),所以我不得不建立它。 – stuartd