-
Notifications
You must be signed in to change notification settings - Fork 22
Expand file tree
/
Copy pathWebsocketHostedServiceWithoutDI.cs
More file actions
90 lines (76 loc) · 3.76 KB
/
WebsocketHostedServiceWithoutDI.cs
File metadata and controls
90 lines (76 loc) · 3.76 KB
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
61
62
63
64
65
66
67
68
69
70
71
72
73
74
75
76
77
78
79
80
81
82
83
84
85
86
87
88
89
90
using System;
using System.Threading;
using System.Threading.Tasks;
using Microsoft.Extensions.Hosting;
using Microsoft.Extensions.Logging;
using TwitchLib.Api;
using TwitchLib.EventSub.Websockets.Core.EventArgs;
using TwitchLib.EventSub.Core.EventArgs.Channel;
namespace TwitchLib.EventSub.Websockets.Example.NetStandard
{
public class WebsocketHostedServiceWithoutDI : IHostedService
{
private readonly ILogger<WebsocketHostedService> _logger;
private readonly EventSubWebsocketClient _eventSubWebsocketClient;
private readonly TwitchAPI _twitchApi = new();
private string _userId;
public WebsocketHostedServiceWithoutDI(ILogger<WebsocketHostedService> logger, ILoggerFactory loggerFactory)
{
_logger = logger;
_eventSubWebsocketClient = new EventSubWebsocketClient(loggerFactory);
_eventSubWebsocketClient.WebsocketConnected += OnWebsocketConnected;
_eventSubWebsocketClient.WebsocketDisconnected += OnWebsocketDisconnected;
_eventSubWebsocketClient.WebsocketReconnected += OnWebsocketReconnected;
_eventSubWebsocketClient.ErrorOccurred += OnErrorOccurred;
_eventSubWebsocketClient.ChannelFollow += OnChannelFollow;
}
public async Task StartAsync(CancellationToken cancellationToken)
{
await _eventSubWebsocketClient.ConnectAsync();
}
public async Task StopAsync(CancellationToken cancellationToken)
{
await _eventSubWebsocketClient.DisconnectAsync();
}
private async Task OnErrorOccurred(object sender, ErrorOccuredArgs e)
{
_logger.LogError($"Websocket {_eventSubWebsocketClient.SessionId} - Error occurred!");
}
private async Task OnChannelFollow(object sender, ChannelFollowArgs e)
{
var eventData = e.Payload.Event;
_logger.LogInformation($"{eventData.UserName} followed {eventData.BroadcasterUserName} at {eventData.FollowedAt}");
}
private async Task OnWebsocketConnected(object sender, WebsocketConnectedArgs e)
{
_logger.LogInformation($"Websocket {_eventSubWebsocketClient.SessionId} connected!");
if (!e.IsRequestedReconnect)
{
// subscribe to topics
// Get ClientId and ClientSecret by register an Application here: https://dev.twitch.tv/console/apps
// https://dev.twitch.tv/docs/authentication/register-app/
_twitchApi.Settings.ClientId = "YOUR_APP_CLIENT_ID";
// Get Application Token with Client credentials grant flow.
// https://dev.twitch.tv/docs/authentication/getting-tokens-oauth/#client-credentials-grant-flow
_twitchApi.Settings.AccessToken = "YOUR_APPLICATION_ACCESS_TOKEN";
// You need the UserID for the User/Channel you want to get Events from.
// You can use await _api.Helix.Users.GetUsersAsync() for that.
_userId = "USER_ID";
}
}
private async Task OnWebsocketDisconnected(object sender, EventArgs e)
{
_logger.LogError($"Websocket {_eventSubWebsocketClient.SessionId} disconnected!");
// Don't do this in production. You should implement a better reconnect strategy
while (!await _eventSubWebsocketClient.ReconnectAsync())
{
_logger.LogError("Websocket reconnect failed!");
await Task.Delay(1000);
}
}
private async Task OnWebsocketReconnected(object sender, EventArgs e)
{
_logger.LogWarning($"Websocket {_eventSubWebsocketClient.SessionId} reconnected");
}
}
}