/// <summary> /// Handles incoming <see cref="ChatMessagePacket"/>s. /// </summary> /// <param name="connectionId">Original connection ID</param> /// <param name="packet">Incoming packet</param> private void chatMessagePacketHandler(string connectionId, ChatMessagePacket packet) { ClientChatMessage message = new ClientChatMessage(packet.MessageId, packet.Uuid, packet.Message, packet.Timestamp.ToDateTime(), ChatMessageStatus.CONFIRMED); lock (messageHistoryLock) { // Store the message in chat history messageHistory.Add(message); } OnChatMessageReceived?.Invoke(this, new ClientChatMessageEventArgs(message)); }
/// <summary> /// Sends a message to the chat. /// </summary> /// <param name="message">Message</param> /// <exception cref="ArgumentException">Message cannot be null or empty</exception> public void Send(string message) { if (string.IsNullOrEmpty(message)) { throw new ArgumentException("Message cannot be null or empty", nameof(message)); } // Check if we aren't authenticated if (!authenticator.Authenticated) { RaiseLogEntry(new LogEventArgs("Cannot send chat message: Not authenticated")); return; } // Check if we aren't logged in if (!userManager.LoggedIn) { RaiseLogEntry(new LogEventArgs("Cannot send chat message: Not logged in")); return; } // Create a random message ID string messageId = Guid.NewGuid().ToString(); // Create and store a chat message locally ClientChatMessage localMessage = new ClientChatMessage(messageId, userManager.Uuid, message); lock (messageHistoryLock) { messageHistory.Add(localMessage); } // Create and pack the chat message packet ChatMessagePacket packet = new ChatMessagePacket { SessionId = authenticator.SessionId, Uuid = userManager.Uuid, MessageId = messageId, Message = message }; Any packedPacket = ProtobufPacketHelper.Pack(packet); // Send it on its way netClient.Send(MODULE_NAME, packedPacket.ToByteArray()); }
/// <summary> /// Attempts to retrieve the corresponding <see cref="ClientChatMessage"/> for the given message ID. /// Returns true if the attempt was successful, otherwise false. /// </summary> /// <param name="messageId">Message ID</param> /// <param name="message">Message output</param> /// <returns>Whether the message with the given ID was retrieved successfully</returns> public bool TryGetMessage(string messageId, out ClientChatMessage message) { message = null; lock (messageHistoryLock) { try { // Get the message corresponding with the given ID from history message = messageHistory.Single(m => m.MessageId == messageId); } catch (InvalidOperationException) { // A single message with the given ID doesn't exist, return a failure return(false); } } // Got what we came for, return a success return(true); }
public ClientChatMessageEventArgs(ClientChatMessage message) { this.Message = message; }