Ejemplo n.º 1
0
        /// <summary>
        /// Connect to the live event server, and join our channel
        /// </summary>
        /// <param name="channelId">Our channelId</param>
        /// <returns></returns>
        public async Task ConnectAndJoinAsync(uint channelId, string token)
        {
            _channel = _factory.CreateJsonRpcWebSocket(_logger, _parser);

            // Connect to the chat endpoint
            var continueTrying = true;

            while (continueTrying && !await _channel.TryConnectAsync(() => WS_URL, token, async() => {
                // Join the channel and subscribe to events
                continueTrying = await _channel.SendAsync("livesubscribe",
                                                          $"channel:{channelId}:update",
                                                          $"channel:{channelId}:followed",
                                                          $"channel:{channelId}:hosted",
                                                          $"channel:{channelId}:unhosted",
                                                          $"channel:{channelId}:subscribed",
                                                          $"channel:{channelId}:resubscribed",
                                                          $"channel:{channelId}:resubShared"
                                                          );
            }))
            {
                ;
            }

            if (!continueTrying)
            {
                _logger.LogError("Failed to connect to live endpoint {0}, giving up! (Channel wrong?)", WS_URL);
                _channel.Dispose();
                _channel = null;
                return;
            }
        }
Ejemplo n.º 2
0
        /// <summary>
        /// Connect to the live event server, and join our channel
        /// </summary>
        /// <param name="channelId">Our channelId</param>
        /// <returns></returns>
        public async Task ConnectAndJoinAsync(uint channelId)
        {
            // Include token on connect if available
            var token = _config["StreamServices:Mixer:Token"];

            if (string.IsNullOrWhiteSpace(token))
            {
                token = null;
            }

            _channel = _factory.CreateJsonRpcWebSocket(_logger, _parser);

            // Connect to the chat endpoint
            var continueTrying = true;

            while (continueTrying && !await _channel.TryConnectAsync(() => WS_URL, token, async() => {
                // Join the channel and subscribe to events
                continueTrying = await _channel.SendAsync("livesubscribe",
                                                          $"channel:{channelId}:update",
                                                          $"channel:{channelId}:followed",
                                                          $"channel:{channelId}:hosted",
                                                          $"channel:{channelId}:unhosted",
                                                          $"channel:{channelId}:subscribed",
                                                          $"channel:{channelId}:resubscribed",
                                                          $"channel:{channelId}:resubShared"
                                                          );
            }))
            {
                ;
            }

            if (!continueTrying)
            {
                _logger.LogError("Failed to connect to live endpoint {0}, giving up! (Channel wrong?)", WS_URL);
                _channel.Dispose();
                _channel = null;
                return;
            }
        }
Ejemplo n.º 3
0
        /// <summary>
        /// Connect to the chat server, and join our channel
        /// </summary>
        /// <param name="userId">Our userId</param>
        /// <param name="channelId">Out channelId</param>
        /// <returns></returns>
        public async Task ConnectAndJoinAsync(uint userId, uint channelId)
        {
            var token = _config["StreamServices:Mixer:Token"];

            _myUserId = userId;
            _channel  = _factory.CreateJsonRpcWebSocket(_logger, _parser);

            // Get chat authkey and endpoints
            var chatData = await _restClient.GetChatAuthKeyAndEndpointsAsync();

            var endpointIndex = Math.Min(1, chatData.Endpoints.Count - 1);             // Skip 1st one, seems to fail often

            // Local function to choose the next endpoint to try
            string getNextEnpoint()
            {
                var endpoint = chatData.Endpoints[endpointIndex];

                endpointIndex = (endpointIndex + 1) % chatData.Endpoints.Count;
                return(chatData.Endpoints[endpointIndex]);
            }

            var continueTrying = true;

            // Local function to join chat channel
            async Task joinAndAuth()
            {
                if (string.IsNullOrEmpty(chatData.Authkey))
                {
                    continueTrying = await _channel.SendAsync("auth", channelId);                      // Authenticating anonymously
                }
                else
                {
                    continueTrying = await _channel.SendAsync("auth", channelId, userId, chatData.Authkey);
                }
            }

            // Local function to join chat channel
            async Task postConnect()
            {
                // Join the channel and send authkey
                await joinAndAuth();

                if (!continueTrying && !string.IsNullOrEmpty(chatData.Authkey))
                {
                    // Try again with a new chatAuthKey
                    chatData = await _restClient.GetChatAuthKeyAndEndpointsAsync();

                    endpointIndex = Math.Min(1, chatData.Endpoints.Count - 1);

                    await joinAndAuth();                     // If this fail give up !
                }
            }

            // Connect to the chat endpoint
            while (continueTrying)
            {
                if (await _channel.TryConnectAsync(getNextEnpoint, null, postConnect))
                {
                    break;
                }
            }

            if (!continueTrying)
            {
                _logger.LogError("Failed to connect to chat endpoint, giving up! (Channel or Token wrong?)");
                _channel.Dispose();
                _channel = null;
                return;
            }
        }