public TwitchPubsubApiClient(RestClientProvider restClientProvider, SocketClientProvider socketClientProvider, string clientId, string userAgent,
                                     string webSocketUrl, RetryMode defaultRetryMode = RetryMode.AlwaysRetry, JsonSerializer serializer = null)
            : base(restClientProvider, clientId, userAgent, serializer)
        {
            _webSocketUrl   = webSocketUrl;
            WebSocketClient = socketClientProvider() as IWebSocketClient;
            _callbacks      = new ConcurrentDictionary <string, Func <PubsubFrame <string>, Task> >();

            WebSocketClient.TextMessage += async text =>
            {
                using (var reader = new StringReader(text))
                    using (var jsonReader = new JsonTextReader(reader))
                    {
                        var msg = _serializer.Deserialize <PubsubFrame <string> >(jsonReader);
                        if (msg != null)
                        {
                            await _receivedPubsubEvent.InvokeAsync(msg).ConfigureAwait(false);
                        }
                    }
            };
            WebSocketClient.Closed += async ex =>
            {
                await DisconnectAsync().ConfigureAwait(false);

                await _disconnectedEvent.InvokeAsync(ex).ConfigureAwait(false);
            };
        }
Esempio n. 2
0
        public TwitchChatApiClient(RestClientProvider restClientProvider, SocketClientProvider socketClientProvider, CacheClientProvider cacheClientProvider, string clientId, string userAgent,
                                   string socketUrl, RetryMode defaultRetryMode = RetryMode.AlwaysRetry, JsonSerializer serializer = null)
            : base(restClientProvider, clientId, userAgent, serializer)
        {
            _socketUrl   = socketUrl;
            SocketClient = socketClientProvider();
            CacheClient  = cacheClientProvider(100);

            SocketClient.TextMessage += async text =>
            {
                var msg = ChatResponse.Parse(text);
                await _receivedChatEvent.InvokeAsync(msg).ConfigureAwait(false);
            };
            SocketClient.Closed += async ex =>
            {
                await DisconnectAsync().ConfigureAwait(false);

                await _disconnectedEvent.InvokeAsync(ex).ConfigureAwait(false);
            };
        }