1
1

More than 1 year has passed since last update.

C#から仮想通貨サイトのAPI(Node.js)を呼ぶ C#とNode.jsを連携させる

Last updated at Posted at 2021-11-03

C#から仮想通貨サイトのAPI(Node.js)を呼ぶ

追記

C#だけで設計したほうが効率がよいことがわかりました。
C#のバイナンスの仮想通貨のライブラリを使ってみる

バイナンスAPIをインストールする

Node.jsからnpmを使いインストールします。
Screenshot from 2021-11-03 19-51-23.png

npm install -s node-binance-api

サンプルスクリプト

const Binance = require('node-binance-api');
const binance = new Binance();
binance.prices('BTCUSDT', (error, ticker) => {
    console.info(ticker);
});  

Node.js側

C#とNode.jsを連携させます。
C#から引数を受けます。
バイナンスAPIを実行し結果をJsonで返します。

const Binance = require('node-binance-api');
const binance = new Binance();

//C#から引数を受ける
var SymbolCode = '';
if( process.argv.length > 2){
    SymbolCode = process.argv[2];
}

if(SymbolCode != ''){
    binance.prices(SymbolCode, (error, ticker) => {

        var obj = { 'price' : ticker[SymbolCode] };

        var jesonStr = JSON.stringify(obj);

        console.clear();
        //標準出力する
        console.info(jesonStr);
    });  
}

C#側

C#から引数で仮想通貨のシンボルコード(BNBBTC)を投げます。
結果をJson標準出力を取得しモデルに変換しています。

同期版

using System.Text.Json;

private void Button1_Clicked(object sender, EventArgs a){

    //同期版
    System.Diagnostics.Process p = new System.Diagnostics.Process();
    //nodeJSのパス
    p.StartInfo.FileName = @"/usr/local/bin/node";
    //引数
    p.StartInfo.Arguments = "/binance.js BNBBTC";
    //出力を読み取れるようにする
    p.StartInfo.UseShellExecute = false;
    p.StartInfo.RedirectStandardOutput = true;
    p.StartInfo.RedirectStandardInput = false;
        p.StartInfo.RedirectStandardError = true;
    //ウィンドウを表示しないようにする
    p.StartInfo.CreateNoWindow = false;

    p.Start();
    string jsonStr = p.StandardOutput.ReadToEnd();
    //string error = p.StandardError.ReadToEnd();
    //プロセス終了まで待機する
    p.WaitForExit();
    p.Close();

    //モデルにフェッチする
    BinancePriceModel BinancePriceModel1 = JsonSerializer.Deserialize<BinancePriceModel>(jsonStr);
    Console.WriteLine(BinancePriceModel1);
} 

非同期版

private void Button1_Clicked(object sender, EventArgs a){
    //非同期
    var p = new System.Diagnostics.Process();
    p.StartInfo.FileName = @"/usr/local/bin/node";
    p.StartInfo.Arguments = "/binance.js BNBBTC";
    p.StartInfo.UseShellExecute = false;
    p.StartInfo.RedirectStandardOutput = true;
    p.StartInfo.RedirectStandardInput = false;
    p.StartInfo.RedirectStandardError = true;
    p.StartInfo.CreateNoWindow = false;
    p.EnableRaisingEvents = true;
    p.Exited += Proc_Exited;
    p.Start();
}

private void Proc_Exited(object sender, EventArgs e){

    System.Diagnostics.Process p = ((System.Diagnostics.Process)sender);
    string jsonStr = p.StandardOutput.ReadToEnd(); 
    //string error = p.StandardError.ReadToEnd();
    p.Close();
    BinancePriceModel BinancePriceModel1 = JsonSerializer.Deserialize<BinancePriceModel>(jsonStr);
    Console.WriteLine(BinancePriceModel1);
}
private void Button1_Clicked(object sender, EventArgs a){
    //非同期
    var p = new System.Diagnostics.Process();
    p.StartInfo.FileName = @"/usr/local/bin/node";
    p.StartInfo.Arguments = "/binance.js BNBBTC";
    p.StartInfo.UseShellExecute = false;
    p.StartInfo.RedirectStandardOutput = true;
    p.StartInfo.RedirectStandardInput = false;
    p.StartInfo.RedirectStandardError = true;
    p.StartInfo.CreateNoWindow = false;
    p.EnableRaisingEvents = true;
    p.Exited += delegate(object sender, EventArgs e){
        System.Diagnostics.Process p2 = ((System.Diagnostics.Process)sender);
        string jsonStr = p2.StandardOutput.ReadToEnd();     
        p2.Close();
        BinancePriceModel BinancePriceModel1 = JsonSerializer.Deserialize<BinancePriceModel>(jsonStr);
        Console.WriteLine(BinancePriceModel1);
   }
    p.Start();
}

Model

Jsから返されるJsonのモデルを事前に作っておきます。

    public class BinancePriceModel  
    {
        public string price { get; set; }
    } 

バイナンスとは

バイナンスへ

Gtk3アプリGtk.ListStoreを使ってコントロールを更新するに続く

1
1
0

Register as a new user and use Qiita more conveniently

  1. You get articles that match your needs
  2. You can efficiently read back useful information
  3. You can use dark theme
What you can do with signing up
1
1