/// <summary> /// Invite users into a new chatroom /// </summary> /// <param name="room"></param> /// <param name="userIdentifier"></param> /// <returns></returns> private async Task InviteUsersToRoom(RoomCreationRequest message, bool showRoomToAll) { string serialized = JsonConvert.SerializeObject(message, Global.SerializerSettings); if (showRoomToAll) { await SendMessageIntoRoom(_globalRoom, serialized); } else { await SendMessageIntoRoom(message.RequestedRoom, serialized); } }
/// <summary> /// Handles a chat message /// </summary> /// <param name="sender">GUID of the sender</param> /// <param name="targetedRoom">identifier of the targeted room</param> /// <param name="recipient">GUID of the recipient</param> /// <param name="messageContent">The message itself</param> /// <returns></returns> private async Task HandleChatMessage(string sender, int?targetedRoom, string recipient, string messageContent) { ChatRoom targetRoom; //if the targeted room exists, send the message into it if (targetedRoom.HasValue && _chatrooms.TryGetValue(targetedRoom.Value, out targetRoom)) { //check whether the sender is in the room and if a recipient is set, he should also be in the room if (targetRoom.ConnectedUsers.Contains(sender) && (recipient == null || targetRoom.ConnectedUsers.Contains(recipient))) { await SendMessageIntoRoom(targetRoom, messageContent); } } //if the targeted room is null and the recipient exists, it is a private message which room is not created yet else if (!targetedRoom.HasValue && recipient != null && _sockets.TryGetValue(recipient, out ChatPartner dummy)) { //check whether a room between them doesn't exist yet targetRoom = _chatrooms.Values.FirstOrDefault(x => x.IsPrivateRoom && x.ConnectedUsers.Contains(sender) && x.ConnectedUsers.Contains(recipient)); if (targetRoom == null) { targetRoom = CreateNewChatRoom(); targetRoom.IsPrivateRoom = true; targetRoom.ConnectedUsers.Add(sender); targetRoom.ConnectedUsers.Add(recipient); RoomCreationRequest request = new RoomCreationRequest() { RequestedRoom = targetRoom, SenderGuid = sender }; //inform the two users about the new room await InviteUsersToRoom(request, false); } await SendMessageIntoRoom(targetRoom, messageContent); } }
/// <summary> /// Handles the received message depending on its message type /// </summary> /// <param name="message"></param> /// <returns></returns> private async Task HandleMessage(string messageString, string socketId) { Message message = null; JObject jsonMessage = null; try { jsonMessage = JObject.Parse(messageString); message = JsonConvert.DeserializeObject <Message>(messageString); } catch { } //if it's a valid "Message", we will continue if (message != null) { //the user's identifier should always be saved in the senderGuid-property jsonMessage["senderGuid"] = socketId; switch (message.MessageType) { //as long as rooms are not implemented, we can broadcast the message to the whole server case MessageType.ChatMessage: ChatMessage chatMessage = jsonMessage.ToObject <ChatMessage>();; await HandleChatMessage(socketId, chatMessage.RoomIdentifier, chatMessage.Recipient, jsonMessage.ToString()); break; case MessageType.RoomCreationRequest: RoomCreationRequest roomRequest = jsonMessage.ToObject <RoomCreationRequest>(); //ensure that the sender is added to the room if (!roomRequest.RequestedRoom.ConnectedUsers.Any(x => x == roomRequest.SenderGuid)) { roomRequest.RequestedRoom.ConnectedUsers.Add(roomRequest.SenderGuid); } roomRequest.RequestedRoom = CreateNewChatRoom(roomRequest.RequestedRoom); await InviteUsersToRoom(roomRequest, true); break; case MessageType.NicknameRequest: NicknameRequest nameRequest = jsonMessage.ToObject <NicknameRequest>(); nameRequest.WasSuccessful = false; lock (_sockets) { if (IsNicknameAvailable(nameRequest.RequestedNickname)) { nameRequest.WasSuccessful = true; _sockets.FirstOrDefault(x => x.Key == socketId).Value.Nickname = nameRequest.RequestedNickname; } } if (nameRequest.WasSuccessful) { await SendMessageIntoRoom(_globalRoom, JsonConvert.SerializeObject(nameRequest, Global.SerializerSettings)); } break; case MessageType.RoomJoinRequest: RoomJoinRequest joinRequest = jsonMessage.ToObject <RoomJoinRequest>(); await HandleRoomJoinRequest(joinRequest); break; } } }