public async Task <bool> SendWhisper(int channelId, string targetUserName, string message) { ChatConnector connector = GetChatConnector(channelId); if (connector == null) { return(false); } return(await connector.SendWhisper(targetUserName, message)); }
public async Task <bool> SendChatMessage(int channelId, string message) { ChatConnector connector = GetChatConnector(channelId); if (connector == null) { return(false); } return(await connector.SendChatMessage(message)); }
private void Chat_OnChatWsStateChanged(ChatConnector sender, ChatState newState, bool wasError) { // If the socket closed removed the channel. if (newState == ChatState.Disconnected) { int channelId = sender.GetChannelId(); var _ignored = Task.Run(async() => { Logger.Error($"Chat ws disconnected due to ws error for channel {channelId}"); await RemoveChannel(channelId, "websocket error."); }).ConfigureAwait(false); } }
private async void WorkerThread() { while (true) { // Get a channel id to work on. int channelId = -1; lock (m_channelProcessQueue) { foreach (KeyValuePair <int, bool> entry in m_channelProcessQueue) { // Skip already processing channel ids. if (entry.Value) { continue; } // Claim this channel id. channelId = entry.Key; m_channelProcessQueue[channelId] = true; break; } } if (channelId == -1) { // No work to do, sleep. Thread.Sleep(100); continue; } // We have a channel id we should connect, try to do it. ChatConnector conn = new ChatConnector(m_chatBotUserId, channelId, this); bool added = false; if ((await conn.Connect())) { // We succeeded, add the connection to the channel. lock (m_channelMap) { if (m_channelMap.ContainsKey(channelId)) { m_channelMap[channelId].Chat = conn; conn.OnChatWsStateChanged += Chat_OnChatWsStateChanged; added = true; } } } // If we failed to connect or didn't add the chat, disconnect. if (!added) { await conn.Disconnect(); } else { OnChatConnectionChanged(channelId, ChatConnectionState.Connected); } // Remove the channel from the processing map. lock (m_channelProcessQueue) { m_channelProcessQueue.Remove(channelId); } } }