/// <summary> /// Processes the received text packet. /// </summary> /// <param name="packetMessage">The receive text packet</param> /// <returns>An awaitable task</returns> protected override Task ProcessReceivedPacket(string packetMessage) { if (!string.IsNullOrEmpty(packetMessage)) { ClientResponsePacketModel packet = new ClientResponsePacketModel(packetMessage); if (packet.IsReplyEvent) { if (this.replyIDListeners.ContainsKey(packet.NormalRef)) { this.replyIDListeners[packet.NormalRef] = packet; } } if (this.chatSubscriptions.Contains(packet.Topic)) { ChatMessagePacketModel message = new ChatMessagePacketModel(packetMessage); this.OnChatMessageReceived?.Invoke(this, message); } else if (this.channelSubscriptions.Contains(packet.Topic)) { ChannelUpdatePacketModel channelUpdate = new ChannelUpdatePacketModel(packetMessage); this.OnChannelUpdated?.Invoke(this, channelUpdate); } else if (this.followSubscriptions.Contains(packet.Topic)) { FollowPacketModel follow = new FollowPacketModel(packetMessage); this.OnFollowOccurred?.Invoke(this, follow); } } return(Task.FromResult(0)); }
/// <summary> /// Unbans a user from the specified channel. /// </summary> /// <param name="channelID">The ID of the channel to send the message to</param> /// <param name="userID">The ID of the user to unban</param> /// <returns>Whether the message was successful</returns> public async Task <bool> UnbanUser(string channelID, string userID) { Validator.ValidateString(channelID, "channelID"); Validator.ValidateString(userID, "userID"); ClientResponsePacketModel response = await this.SendAndListen(new ChatUnbanUserPacketModel(channelID, userID)); return(response != null && response.IsPayloadStatusOk); }
/// <summary> /// Deletes the specified message from the specified channel's chat. /// </summary> /// <param name="channelID">The ID of the channel to delete the message from</param> /// <param name="messageID">The ID of the message to delete</param> /// <returns>Whether the message was successful</returns> public async Task <bool> DeleteMessage(string channelID, string messageID) { Validator.ValidateString(channelID, "channelID"); Validator.ValidateString(messageID, "messageID"); ClientResponsePacketModel response = await this.SendAndListen(new ChatDeleteMessagePacketModel(channelID, messageID)); return(response != null && response.IsPayloadStatusOk); }
/// <summary> /// Connects to the web socket servers. /// </summary> /// <returns>Whether the connection was successful</returns> public async Task <bool> Connect() { if (await this.Connect(this.connectionUrl)) { ClientResponsePacketModel response = await this.SendAndListen(new ClientConnectPacketModel()); if (response != null && response.IsPayloadStatusOk) { this.backgroundPingCancellationTokenSource = new CancellationTokenSource(); #pragma warning disable CS4014 // Because this call is not awaited, execution of the current method continues before the call is completed Task.Run(() => this.BackgroundPing(this.backgroundPingCancellationTokenSource.Token), this.backgroundPingCancellationTokenSource.Token); #pragma warning restore CS4014 // Because this call is not awaited, execution of the current method continues before the call is completed return(true); } } return(false); }
private async Task <ClientResponsePacketModel> SendAndListen(ClientPacketModelBase packet) { ClientResponsePacketModel replyPacket = null; this.replyIDListeners[packet.NormalRef] = null; await this.Send(packet); await this.WaitForSuccess(() => { if (this.replyIDListeners.ContainsKey(packet.NormalRef) && this.replyIDListeners[packet.NormalRef] != null) { replyPacket = this.replyIDListeners[packet.NormalRef]; return(true); } return(false); }, secondsToWait : 5); this.replyIDListeners.Remove(packet.NormalRef); return(replyPacket); }
/// <summary> /// Joins the specified channel's chat. /// </summary> /// <param name="channelID">The ID of the channel</param> /// <returns>Whether the connection was successful</returns> public async Task <bool> JoinChannelChat(string channelID) { Validator.ValidateString(channelID, "channelID"); // Join Chat ClientResponsePacketModel chatJoinResponse = await this.SendAndListen(new ChatJoinPacketModel(channelID)); if (chatJoinResponse == null || !chatJoinResponse.IsPayloadStatusOk) { return(false); } JToken chatJoinSubscription = chatJoinResponse.Payload.SelectToken("response.subscriptionId"); if (chatJoinSubscription == null) { return(false); } this.chatSubscriptions.Add(chatJoinSubscription.ToString()); return(true); }
/// <summary> /// Joins the specified channel's events. /// </summary> /// <param name="channelID">The ID of the channel</param> /// <param name="streamerID">The ID of the streamer user</param> /// <returns>Whether the connection was successful</returns> public async Task <bool> JoinChannelEvents(string channelID, string streamerID) { Validator.ValidateString(channelID, "channelID"); // Join Channel Events ClientResponsePacketModel channelJoinResponse = await this.SendAndListen(new ChannelJoinPacketModel(channelID)); if (channelJoinResponse == null || !channelJoinResponse.IsPayloadStatusOk) { return(false); } JToken channelJoinSubscription = channelJoinResponse.Payload.SelectToken("response.subscriptionId"); if (channelJoinSubscription == null) { return(false); } this.channelSubscriptions.Add(channelJoinSubscription.ToString()); // Join Follow Events ClientResponsePacketModel followJoinResponse = await this.SendAndListen(new FollowsJoinPacketModel(streamerID)); if (followJoinResponse == null || !followJoinResponse.IsPayloadStatusOk) { return(false); } JToken followJoinSubscription = followJoinResponse.Payload.SelectToken("response.subscriptionId"); if (followJoinSubscription == null) { return(false); } this.followSubscriptions.Add(followJoinSubscription.ToString()); return(true); }
/// <summary> /// Sends a heartbeat to the web socket servers. /// </summary> /// <returns>An awaitable Task</returns> public async Task <bool> SendHeartbeat() { ClientResponsePacketModel response = await this.SendAndListen(new ClientHeartbeatPacketModel()); return(response != null && response.IsPayloadStatusOk); }