コード例 #1
0
        protected override async Task <string> SendUnsubscribe(string key, CancellationToken cancellationToken = default)
        {
            string clientid = Guid.NewGuid().ToString();
            string unsubkey = GetUnsubscribeKey(key, clientid);
            WebSocketRequestBuilder builder = new WebSocketRequestBuilder()
                                              .AddParam("op", "unsub")
                                              .AddParam("cid", clientid)
                                              .AddParam("topic", key);

            string json = builder.Build();

            await SendAsync(json, cancellationToken);

            var task = Task.Factory.StartNew(() =>
            {
                while (true)
                {
                    Thread.Sleep(10);
                    if (_messagePool.TryGetValue(unsubkey, out string response))
                    {
                        _messagePool.Remove(unsubkey);
                        return(response);
                    }
                }
            }, cancellationToken);

            var repJson = await task;

            return(repJson);
        }
コード例 #2
0
        private async Task OnPing(long ts)
        {
            WebSocketRequestBuilder builder = new WebSocketRequestBuilder()
                                              .AddParam("pong", ts);

            await this.SendAsync(builder.Build(), CancellationToken.None);
        }
コード例 #3
0
        public async Task Auth(CancellationToken cancellationToken = default)
        {
            string authkey = GetAuthKey();

            DateTime now = DateTime.UtcNow;

            string signature = GetSignature(now);

            WebSocketRequestBuilder builder2 = new WebSocketRequestBuilder()
                                               .AddParam("op", "auth")
                                               .AddParam("type", "api")
                                               .AddParam(_aKKey, _accesskey)
                                               .AddParam(_sMKey, _sMValue)
                                               .AddParam(_sVKey, _sVValue)
                                               .AddParam(_tKey, now.ToString("s"))
                                               .AddParam(_sKey, signature);

            await this.SendAsync(builder2.Build(), cancellationToken);

            var task = Task.Factory.StartNew(() =>
            {
                while (true)
                {
                    Thread.Sleep(10);
                    if (_messagePool.TryGetValue(authkey, out string response))
                    {
                        _messagePool.Remove(authkey);
                        return(response);
                    }
                }
            }, cancellationToken);

            var repJson = await task;

            JToken jt       = JToken.Parse(repJson);
            int    err_code = jt["err-code"].ToObject <int>();

            if (err_code != 0)
            {
                throw new Exception($"{GetType()}.{nameof(Auth)} failed: {jt["err-msg"].ToObject<string>()}({err_code})");
            }
        }