如何将 websocket enpoint 初始化为 azure webjob?

How to initialize websocket enpoint into azure webjob?

我有网络套接字端点,想要初始化并在每次有数据时连续调用它 可从 api.

获得

我有意识地创建了 运行ning azure web 作业但不确定 how/where 我需要添加 web socket ticker service 代码,它将 运行 连续并从 api.

Program.cs

class Program
{
    static void Main()
    {
        var config = new JobHostConfiguration();

        if (config.IsDevelopment)
        {
            config.UseDevelopmentSettings();
        }

        var host = new JobHost(config);
 
        // The following code ensures that the WebJob will be running continuously
        host.RunAndBlock();
    }
}

TickerService.cs

 class TickerService
    {
        private Ticker _ticker; // THIS OBJECT YOU WILL GET IT FROM KITECONNECT
        private string _myAccessToken;
        private UInt32[] intrument_tokent = new UInt32[] { 256265 };

        public TickerService(string myAccessToken)
        {
            this._myAccessToken = myAccessToken; // THIS TOKEN IS ONE WHICH WE GET AFTER LOGIN. 
            initTicker();
        }

        private void initTicker()
        {
            _ticker = new Ticker("apikey", this._myAccessToken);

            _ticker.OnTick += OnTick;
            _ticker.OnReconnect += OnReconnect;
            _ticker.OnNoReconnect += OnNoReconnect;
            _ticker.OnError += OnError;
            _ticker.OnClose += OnClose;
            _ticker.OnConnect += OnConnect;
            _ticker.OnOrderUpdate += OnOrderUpdate;

            _ticker.EnableReconnect(Interval: 5, Retries: 50);

            //init and connect to web socket api
            _ticker.Connect();

            // Subscribing to BANKNIFTY and setting mode to FULL MODE 
            _ticker.Subscribe(Tokens: intrument_tokent); // <- THIS METHOD USED FOR SUBSCRIPTION
            _ticker.SetMode(Tokens: intrument_tokent, Mode: Constants.MODE_FULL); // <- THIS METHOD USED FOR SETTING MODE
        }

        private void OnTokenExpire()
        {
            Console.WriteLine("Need to login again");
        }

        private void OnConnect()
        {
            Console.WriteLine("Connected ticker");
        }

        private void OnClose()
        {
            Console.WriteLine("Closed ticker");
        }

        private void OnError(string Message)
        {
            Console.WriteLine("Error: " + Message);
        }

        private void OnNoReconnect()
        {
            Console.WriteLine("Not reconnecting");
        }

        private void OnReconnect()
        {
            Console.WriteLine("Reconnecting");
        }

        private void OnTick(Tick TickData)
        {
            ProcessTick(TickData);
        }

        private void OnOrderUpdate(Order OrderData)
        {
            Console.WriteLine("OrderUpdate " + Utils.JsonSerialize(OrderData));
        }
        //Method wch listen data , here you will get LTP 
        private void ProcessTick(Tick tickData)
        {
            try
            {
                var candleTickData = new List<CandleTickData>();

                var tickDataHis = new CandleTickData()
                {
                    InstrumentID = tickData.InstrumentToken,
                    Close = tickData.LastPrice,
                    High = tickData.LastPrice,
                    Low = tickData.LastPrice,
                    Open = tickData.LastPrice,
                    TimeStamp = tickData.Timestamp.HasValue ? tickData.Timestamp.Value : DateTime.Now,
                    Volume = tickData.Volume

                };

                candleTickData.Add(tickDataHis);
                // TODO:->Logic here to generate and process 5 min candle

            }
            catch (Exception ex)
            {

            }
        }
    }

    class CandleTickData
    {
        public uint InstrumentID { get; set; }
        public decimal Close { get; set; }
        public decimal High { get; set; }
        public decimal Low { get; set; }
        public decimal Open { get; set; }
        public DateTime TimeStamp { get; set; }
        public uint Volume { get; set; }
    }

Question - what is the correct way to implement TickerService.cs into Program.cs

在console slave程序中,程序是自上而下运行的,所以只需要定义一次TickerService

喜欢:

class Program
{
    static void Main()
    {
        var config = new JobHostConfiguration();

        if (config.IsDevelopment)
        {
            config.UseDevelopmentSettings();
        }

        var host = new JobHost(config);

        // The following code ensures that the WebJob will be running continuously
        
        TickerService _tickerservice=new TickerService();
        while(true){
            // The specific judgment conditions are based on actual scenarios.
            if(_tickerservice._myAccessToken==null){
                _tickerservice=new TickerService();
            }
        }
        
        host.RunAndBlock();
    }
}

找不到Ticker包,所以只提供一般场景的方法,仅供参考。