2016-04-25 6 views
0

デモ用にnode.js/edge.js/C#ブリッジをまとめようとしています。edge.jsからC#ホストと通信します

既存のC#コードではいくつかの設定値を使用するので、".Net calling Node.js"スタイルを使用する必要があります。これは複数のバージョンを同時に実行する必要があるため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

関連する問題