diff --git a/.gitignore b/.gitignore index 89ad0e0..24012de 100644 --- a/.gitignore +++ b/.gitignore @@ -233,4 +233,6 @@ $RECYCLE.BIN/ *.msp # Windows shortcuts -*.lnk \ No newline at end of file +*.lnk + +.idea/* \ No newline at end of file diff --git a/TwitchLib.PubSub/Interfaces/ITwitchPubSub.cs b/TwitchLib.PubSub/Interfaces/ITwitchPubSub.cs index 009a4ee..0390295 100644 --- a/TwitchLib.PubSub/Interfaces/ITwitchPubSub.cs +++ b/TwitchLib.PubSub/Interfaces/ITwitchPubSub.cs @@ -1,4 +1,5 @@ using System; +using System.Threading.Tasks; using TwitchLib.PubSub.Events; namespace TwitchLib.PubSub.Interfaces @@ -169,10 +170,22 @@ public interface ITwitchPubSub /// Connects this instance. /// void Connect(); + + /// + /// Connects this instance. + /// + Task ConnectAsync(); + /// /// Disconnects this instance. /// void Disconnect(); + + /// + /// Disconnects this instance. + /// + Task DisconnectAsync(); + /// /// Listens to bits events. /// @@ -248,6 +261,14 @@ public interface ITwitchPubSub /// The oauth. /// if set to true [unlisten]. void SendTopics(string oauth = null, bool unlisten = false); + + /// + /// Sends the topics. + /// + /// The oauth. + /// if set to true [unlisten]. + Task SendTopicsAsync(string oauth = null, bool unlisten = false); + /// /// Tests the message parser. /// diff --git a/TwitchLib.PubSub/TwitchLib.PubSub.csproj b/TwitchLib.PubSub/TwitchLib.PubSub.csproj index b2d349b..67f3972 100644 --- a/TwitchLib.PubSub/TwitchLib.PubSub.csproj +++ b/TwitchLib.PubSub/TwitchLib.PubSub.csproj @@ -3,7 +3,7 @@ netstandard2.0 TwitchLib.PubSub - 3.2.6 + 4.0.0 $(VersionSuffix) PubSub component of TwitchLib. This component allows you to access Twitch's PubSub event system and subscribe/unsubscribe from topics. true @@ -18,13 +18,13 @@ Git twitch library irc chat c# csharp api events pubsub net standard 2.0 en-US - 3.2.6 - 3.2.6 + 4.0.0 + 4.0.0 True - + diff --git a/TwitchLib.PubSub/TwitchPubSub.cs b/TwitchLib.PubSub/TwitchPubSub.cs index e82c85a..a019d80 100644 --- a/TwitchLib.PubSub/TwitchPubSub.cs +++ b/TwitchLib.PubSub/TwitchPubSub.cs @@ -4,6 +4,7 @@ using System.Collections.Generic; using System.Linq; using System.Threading; +using System.Threading.Tasks; using System.Timers; using TwitchLib.Communication.Clients; using TwitchLib.Communication.Enums; @@ -288,7 +289,7 @@ public TwitchPubSub(ILogger logger = null) { _logger = logger; - var options = new ClientOptions { ClientType = ClientType.PubSub }; + var options = new ClientOptions(clientType: ClientType.PubSub); _socket = new WebSocketClient(options); _socket.OnConnected += Socket_OnConnected; @@ -297,7 +298,7 @@ public TwitchPubSub(ILogger logger = null) _socket.OnDisconnected += Socket_OnDisconnected; _pongTimer.Interval = 15000; //15 seconds, we should get a pong back within 10 seconds. - _pongTimer.Elapsed += PongTimerTick; + _pongTimer.Elapsed += PongTimerTickAsync; } /// @@ -345,7 +346,7 @@ private void Socket_OnConnected(object sender, EventArgs e) { _logger?.LogInformation("PubSub Websocket connection established"); _pingTimer.Interval = 180000; - _pingTimer.Elapsed += PingTimerTick; + _pingTimer.Elapsed += PingTimerTickAsync; _pingTimer.Start(); OnPubSubServiceConnected?.Invoke(this, null); } @@ -355,7 +356,7 @@ private void Socket_OnConnected(object sender, EventArgs e) /// /// The sender. /// The instance containing the event data. - private void PingTimerTick(object sender, ElapsedEventArgs e) + private async void PingTimerTickAsync(object sender, ElapsedEventArgs e) { //Reset pong state. _pongReceived = false; @@ -364,7 +365,8 @@ private void PingTimerTick(object sender, ElapsedEventArgs e) var data = new JObject( new JProperty("type", "PING") ); - _socket.Send(data.ToString()); + + await _socket.SendAsync(data.ToString()); //Start pong timer. _pongTimer.Start(); @@ -375,7 +377,7 @@ private void PingTimerTick(object sender, ElapsedEventArgs e) /// /// The sender. /// The instance containing the event data. - private void PongTimerTick(object sender, ElapsedEventArgs e) + private async void PongTimerTickAsync(object sender, ElapsedEventArgs e) { //Stop the pong timer. _pongTimer.Stop(); @@ -388,7 +390,7 @@ private void PongTimerTick(object sender, ElapsedEventArgs e) else { //Otherwise we're disconnected so close the socket. - _socket.Close(); + await _socket.CloseAsync(); } } @@ -680,7 +682,11 @@ private void ParseMessage(string message) case "pong": _pongReceived = true; return; - case "reconnect": _socket.Close(); break; + case "reconnect": + // This does not fit here. This method parses message, it shouldn't do any action. + // TODO: Fire event to trigger socket close + _socket.CloseAsync().GetAwaiter().GetResult(); + break; } UnaccountedFor(message); } @@ -720,13 +726,19 @@ private void ListenToTopics(params string[] topics) } } + /// + public void SendTopics(string oauth = null, bool unlisten = false) + { + SendTopicsAsync(oauth, unlisten).GetAwaiter().GetResult(); + } + /// /// /// Sends the topics. /// /// The oauth. /// if set to true [unlisten]. - public void SendTopics(string oauth = null, bool unlisten = false) + public async Task SendTopicsAsync(string oauth = null, bool unlisten = false) { if (oauth != null && oauth.Contains("oauth:")) { @@ -764,7 +776,7 @@ public void SendTopics(string oauth = null, bool unlisten = false) ((JObject)jsonData.SelectToken("data"))?.Add(new JProperty("auth_token", oauth)); } - _socket.Send(jsonData.ToString()); + await _socket.SendAsync(jsonData.ToString()); _topicList.Clear(); } @@ -968,16 +980,25 @@ public void ListenToPredictions(string channelTwitchId) /// public void Connect() { - _socket.Open(); + ConnectAsync().GetAwaiter().GetResult(); + } + + /// + public async Task ConnectAsync() + { + await _socket.OpenAsync(); } /// - /// - /// What do you think it does? :) - /// public void Disconnect() { - _socket.Close(); + DisconnectAsync().GetAwaiter().GetResult(); + } + + /// + public async Task DisconnectAsync() + { + await _socket.CloseAsync(); } ///