1717
1818namespace TrackMaster . Services . TwitchServices
1919{
20- public interface ITimerHostedService : IHostedService { }
21- public class TwitchBot : ITimerHostedService
20+ public class TwitchBot : IHostedService
2221 {
2322 private TwitchClient client ;
2423 private readonly IHubContext < TrackistHub > _tracklisthubContext ;
@@ -29,7 +28,6 @@ public class TwitchBot : ITimerHostedService
2928 private Timer _timer ;
3029 private readonly DataFields _dataFields ;
3130
32-
3331 private static TwitchBot _instance ;
3432
3533 public static TwitchBot Instance => _instance ;
@@ -46,10 +44,12 @@ public Task StartAsync(CancellationToken cancellationToken)
4644 {
4745 _logger . LogInformation ( "TwitchBot Service is Starting" ) ;
4846
49- DoWork ( ) ;
50-
51- _timer = new Timer ( CheckStatus , null , TimeSpan . Zero , TimeSpan . FromSeconds ( 20 ) ) ;
52-
47+ Task . Run ( async ( ) =>
48+ {
49+ await DoWork ( cancellationToken ) ;
50+
51+ } , cancellationToken ) ;
52+ _timer = new Timer ( CheckStatus , null , TimeSpan . Zero , TimeSpan . FromSeconds ( 5 ) ) ;
5353 return Task . CompletedTask ;
5454 }
5555
@@ -59,9 +59,17 @@ private void CheckStatus(object state)
5959 {
6060 _tracklisthubContext . Clients . All . SendAsync ( "DeviceAndTwitchStatus" , 2 , "Connected to Twitch Bot!" ) ;
6161 }
62+ else
63+ {
64+ Task . Run ( async ( ) =>
65+ {
66+ await DoWork ( state ) ;
67+
68+ } ) ;
69+ }
6270 }
6371
64- private async void DoWork ( )
72+ private async Task DoWork ( object state )
6573 {
6674 _logger . LogInformation ( "Timed Background Service is working." ) ;
6775
@@ -96,22 +104,23 @@ private async Task Bot()
96104 if ( string . IsNullOrEmpty ( _twitchUsername ) || string . IsNullOrEmpty ( _twitchPassword ) )
97105 throw new ArgumentException ( ) ;
98106
99- ConnectionCredentials credentials = new ( _twitchUsername , _twitchPassword ) ;
100- var clientOptions = new ClientOptions
101- {
102- MessagesAllowedInPeriod = 750 ,
103- ThrottlingPeriod = TimeSpan . FromSeconds ( 30 )
104- } ;
105- WebSocketClient customClient = new ( clientOptions ) ;
106- client = new TwitchClient ( customClient ) ;
107- client . Initialize ( credentials , _twitchChannel ) ;
108- client . OnMessageReceived += Client_OnMessageReceived ;
109- client . OnConnectionError += Client_OnConnectionError ;
110- client . OnIncorrectLogin += Client_OnIncorrectLogin ;
111- client . OnConnected += Client_OnConnected ;
112- client . OnJoinedChannel += Client_OnJoinedChannel ;
113- client . Connect ( ) ;
114-
107+ await Task . Run ( ( ) => {
108+ ConnectionCredentials credentials = new ( _twitchUsername , _twitchPassword ) ;
109+ var clientOptions = new ClientOptions
110+ {
111+ MessagesAllowedInPeriod = 750 ,
112+ ThrottlingPeriod = TimeSpan . FromSeconds ( 30 )
113+ } ;
114+ WebSocketClient customClient = new ( clientOptions ) ;
115+ client = new TwitchClient ( customClient ) ;
116+ client . Initialize ( credentials , _twitchChannel ) ;
117+ client . OnMessageReceived += Client_OnMessageReceived ;
118+ client . OnConnectionError += Client_OnConnectionError ;
119+ client . OnIncorrectLogin += Client_OnIncorrectLogin ;
120+ client . OnConnected += Client_OnConnected ;
121+ client . OnJoinedChannel += Client_OnJoinedChannel ;
122+ client . Connect ( ) ;
123+ } ) ;
115124 }
116125 catch ( Exception ex )
117126 {
@@ -174,6 +183,8 @@ private async Task<MainSettingsModel> GetSetTwitchCredentials()
174183 if ( HybridSupport . IsElectronActive )
175184 {
176185 string path = await Electron . App . GetPathAsync ( PathName . UserData ) ;
186+
187+ Console . WriteLine ( "Twitch: " + path ) ;
177188 _dataFields . Appfullpath = path + @"\Settings.json" ;
178189 return settingsHelper . GetSettings ( _dataFields . Appfullpath ) ;
179190 }
0 commit comments