Exemplo n.º 1
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;
            }
        }