bitFlyer からリアルタイムデータを取得する方法「C#編」Realtime API via PubNub

Python編はこちら

for .NET 4.5.2

Nuget
- PubNub
- Utf8Json

Add Reference
- Runtime.Serialization;

//Program.cs
using System;

namespace bf_PubNub
{
    class Program
    {
        static void Main(string[] args)
        {
            var api = new Apis();

            api.Subscribe<Tick>(api.TickerFxBtcJpy, OnReceive, OnConnect, OnError);

            Console.ReadKey();
        }

        static void OnConnect(string message)
        {
            Console.WriteLine(message);
        }

        static void OnReceive(Tick data)
        {
            Console.WriteLine(data);
        }

        static void OnError(string message, Exception ex)
        {
            Console.WriteLine(message);
            if (ex != null)
            {
                Console.WriteLine(ex);
            }
        }
         
    }
}

 

//Apis.cs
using System;
using System.Text;
using PubnubApi;
using System.Runtime.Serialization;
using Utf8Json;

namespace bf_PubNub
{
    public class Apis
    {
           
        private readonly Pubnub _pubnub;

        public string TickerFxBtcJpy = "lightning_ticker_" + "FX_BTC_JPY";
         
        public Apis()
        {
            var config = new PNConfiguration();
            config.SubscribeKey = "sub-c-52a9ab50-291b-11e5-baaa-0619f8945a4f";
            config.PublishKey = "";
            _pubnub = new Pubnub(config);
        }

        public void Subscribe<T>(string channel, Action<T> onReceive, Action<string> onConnect, Action<string, Exception> onError)
        {


            _pubnub.AddListener(new SubscribeCallbackExt(
                (pubnubObj, message) => {

                    if (message != null)
                    {

                        string json = message.Message.ToString();

                        T deserialized;
                        try
                        {
                            deserialized = JsonSerializer.Deserialize<T>(json);
                            onReceive(deserialized);
                        }
                        catch (Exception ex)
                        {
                            onError(ex.Message, ex);
                            return;
                        }
                    }


                },
                (pubnubObj, presence) => { },
                (pubnubObj, status) => {

                    if (status.Category == PNStatusCategory.PNUnexpectedDisconnectCategory)
                    {
                        onError("unexpected disconnect.", null);
                    }
                    else if (status.Category == PNStatusCategory.PNConnectedCategory)
                    {
                        onConnect("connected.");
                    }
                    else if (status.Category == PNStatusCategory.PNReconnectedCategory)
                    {
                        onError("reconnected.", null);
                    }
                    else if (status.Category == PNStatusCategory.PNDecryptionErrorCategory)
                    {
                        onError("messsage decryption error.", null);
                    }

                }
            ));


            _pubnub.Subscribe<string>()
                .Channels(new[] { channel })
                .Execute();

        }

    }

 
    public class Tick
    {
        [DataMember(Name = "product_code")]
        public string ProductCode { get; set; }

        [DataMember(Name = "timestamp")]
        public DateTime Timestamp { get; set; }

        [DataMember(Name = "tick_id")]
        public long TickId { get; set; }

        [DataMember(Name = "best_bid")]
        public double BestBid { get; set; }

        [DataMember(Name = "best_ask")]
        public double BestAsk { get; set; }

        [DataMember(Name = "best_bid_size")]
        public double BestBidSize { get; set; }

        [DataMember(Name = "best_ask_size")]
        public double BestAskSize { get; set; }

        [DataMember(Name = "total_bid_depth")]
        public double TotalBidDepth { get; set; }

        [DataMember(Name = "total_ask_depth")]
        public double TotalAskDepth { get; set; }

        [DataMember(Name = "ltp")]
        public double LatestPrice { get; set; }

        [DataMember(Name = "volume")]
        public double Volume { get; set; }

        [DataMember(Name = "volume_by_product")]
        public double VolumeByProduct { get; set; }
         

        public override string ToString()
        {
            return Encoding.UTF8.GetString(JsonSerializer.Serialize(this));
        }
    }

}

 

   
参考サイト
GitHub : bitFlyer HTTP APIs Client Library for .NET (C#)
PubNub C# V4 SDK 4.0.12.0
bitFlyer Lightning API Documentation