Exemplo n.º 1
0
        public async Task <WebSocketResponse> GetStatusesAsync(int millisecondsTimeout)
        {
            WebSocketResponse response = null;
            var seq = NextSeq();
            var msg = new WebSocketRequest {
                Action = "get_statuses",
                Seq    = seq,
            };

            using var sem = new SemaphoreSlim(0);

            void OnWebSocketResponseAction(WebSocketResponse r)
            {
                if (r.SeqReply != seq)
                {
                    return;
                }
                response = r;
                // ReSharper disable once AccessToDisposedClosure - its not true!
                sem.Release();
            }

            OnWebSocketResponse += OnWebSocketResponseAction;
            await SendAsync(Serialize(msg));

            await sem.WaitAsync(millisecondsTimeout, _cancellationToken);

            OnWebSocketResponse -= OnWebSocketResponseAction;
            return(response);
        }
Exemplo n.º 2
0
        public static bool TryGetData(this WebSocketResponse r, string @event, string key, out string data)
        {
            object dobj = null;
            var    ret  = r?.Data != null && r.Event == @event && r.Data.TryGetValue(key, out dobj);

            data = dobj?.ToString();
            return(ret && data != null);
        }
Exemplo n.º 3
0
        public static string GetDataOrDefault(this WebSocketResponse r, string key)
        {
            if (r?.Data != null && r.Data.TryGetValue(key, out var data))
            {
                return(data.ToString());
            }

            return(null);
        }