Beispiel #1
0
        public async Task REQ_GetLatestKLine(string sym, Models.MktKLineType typ, int count, Action <int, Models.MktQueryKLineHistoryResult> cb, CancellationToken cancellationToken)
        {
            var args = new Models.MktQueryKLineHistoryRequestArgs
            {
                Sym   = sym,
                Typ   = typ,
                Count = count
            };
            var req = new WsMarketMessageRequest("GetLatestKLine", args);

            await this.SendRequestAsync(req, (code, data) =>
            {
                //Models.MxQueryKLineHistoryResult result = new Models.MxQueryKLineHistoryResult();
                if (code == 0)
                {
                    var res = Helper.MyJsonSafeToObj <Models.MktQueryKLineHistoryResult>(data);
                    cb(code, res);
                }
                else
                {
                    Debug.WriteLine("[-] GetLatestKLine failed: " + data.ToString());
                    cb(code, null);
                }
            },
                                        cancellationToken);
        }
Beispiel #2
0
        public async Task REQ_Sub(List <string> subjects, Action <int, string> cb, CancellationToken cancellationToken, bool unSub)
        {
            var req = new WsMarketMessageRequest(unSub ? "UnSub" : "Sub", subjects);

            await this.SendRequestAsync(req, (code, data) =>
            {
                var msg = string.Empty;
                if (data != null)
                {
                    msg = data.ToString();
                }
                cb(code, msg);
            },
                                        cancellationToken);
        }
Beispiel #3
0
        //public Task ConnectAsync(string url, CancellationToken cancellationToken)
        //{
        //    this._ws?.Dispose();

        //    this._ws = new ClientWebSocket();
        //    return this._ws.ConnectAsync(new Uri(url), cancellationToken);
        //}

        private Task SendRequestAsync(WsMarketMessageRequest msg, Action <int, object> action, CancellationToken cancellationToken)
        {
            if (this._ws == null || this._ws.State != WebSocketState.Open)
            {
                throw new InvalidOperationException("websocket is not open");
            }

            try
            {
                _request_callback[msg.ReqID] = action; //_request_callback.TryAdd(msg.ReqID, action);
                return(this._ws?.SendAsync(new ArraySegment <byte>(msg.GetMsgBuffer()), WebSocketMessageType.Text, true, cancellationToken));
            }
            catch (Exception ex)
            {
                _request_callback.Remove(msg.ReqID);
                throw ex;
            }
        }
Beispiel #4
0
        public async Task REQ_GetCompositeIndexAsync(Action <int, List <string> > cb, CancellationToken cancellationToken)
        {
            // {"rid":"41","code":0,"data":["GMEX_CI_ETH","GMEX_CI_ETC","GMEX_CI_LTC","GMEX_CI_BTC","GMEX_CI_EOS","GMEX_CI_XRP"]}
            var req = new WsMarketMessageRequest("GetCompositeIndex", null);

            await this.SendRequestAsync(req, (code, data) =>
            {
                List <string> indices = new List <string>();
                if (code == 0)
                {
                    var array = data as Newtonsoft.Json.Linq.JArray;
                    //var lines = data as Array;
                    foreach (var l in array)
                    {
                        indices.Add(l.ToObject <string>());
                    }
                }
                cb(code, indices);
            },
                                        cancellationToken);
        }
Beispiel #5
0
        public async Task REQ_TimeAsync(Action <int, long> cb, CancellationToken cancellationToken)
        {
            // {"req":"Time","rid":"123","expires":1545722275616,"args":1545722274616}
            // {"rid":"123","code":0,"data":{"time":1545722274605,"data":"1545722274616"}}

            long localtm = DateTimeOffset.Now.ToUnixTimeMilliseconds();
            var  req     = new WsMarketMessageRequest("Time", localtm);

            await this.SendRequestAsync(req, (code, data) =>
            {
                long delta = 0;
                if (code == 0)
                {
                    var obj = data as Newtonsoft.Json.Linq.JObject;
                    if (obj != null)
                    {
                        var servertm = obj["time"].ToObject <long>();
                        delta        = servertm - localtm;
                    }
                }
                cb(code, delta);
            },
                                        cancellationToken);
        }