Ejemplo n.º 1
0
        private async Task TwitchHandshake(ClientWebSocket webSocket, CancellationToken cancellationToken,
                                           TwitchService twitchService, TwitchManager twitchManager)
        {
            await CheckToken(cancellationToken, twitchService);

            var token = await twitchService.GetToken();

            var user = await twitchManager.GetUser();

            Send($"PASS oauth:{token.access_token}");
            Send($"NICK {user.login}"); // TODO: get nick from twitch API
        }
Ejemplo n.º 2
0
        public async Task <IActionResult> Account()
        {
            var user = await twitchManager.GetUser();

            return(View("User", user));
        }
Ejemplo n.º 3
0
        private async Task Receive(ClientWebSocket webSocket, CancellationToken cancellationToken, TwitchService twitchService,
                                   TwitchManager twitchManager, AccessToken accessToken)
        {
            if (webSocket.State != WebSocketState.Open)
            {
                throw new InvalidOperationException($"[TwitchBackgroundService] Twitch socket {webSocket.State.ToString()}");
            }
            var user = await twitchManager.GetUser();

            var encoder = new UTF8Encoding();
            var partial = string.Empty;

            while (webSocket.State == WebSocketState.Open &&
                   !cancellationToken.IsCancellationRequested &&
                   await twitchService.IsEnabled() &&
                   accessToken.Status == AccessTokenStatus.Ok)
            {
                var buffer = new byte[receiveChunkSize];
                logger.LogDebug($"[TwitchBackgroundService] Listening to socket");
                var result = await webSocket.ReceiveAsync(new ArraySegment <byte>(buffer), cancellationToken);

                logger.LogDebug($"[TwitchBackgroundService] Receive status {result.MessageType}");

                if (result.MessageType == WebSocketMessageType.Close)
                {
                    await webSocket.CloseAsync(WebSocketCloseStatus.NormalClosure, string.Empty, cancellationToken);
                }
                else if (result.MessageType == WebSocketMessageType.Text)
                {
                    var text = partial + encoder.GetString(buffer).Replace("\0", "");
                    partial = string.Empty;

                    if (string.IsNullOrEmpty(text))
                    {
                        continue;
                    }

                    var lines = (text.Substring(0, text.LastIndexOf('\r'))).Split('\r');

                    foreach (var line in lines)
                    {
                        //logger.LogDebug($"[TwitchBackgroundService] Twitch Chat < {line}");

                        if (line.Contains("PING"))
                        {
                            var host = line.Substring(line.IndexOf(':'));
                            Send($"PONG :{host}");
                            await UpdateDashboardStatus(IntegrationStatus.Connected,
                                                        cancellationToken);

                            continue;
                        }

                        await twitchService.ProcessMessage(line);
                    }

                    partial = text.Substring(text.LastIndexOf('\r'));
                }

                await Task.Delay(delay, cancellationToken);
            }
        }