private async Task ProcessMessage(IMessage message)
        {
            switch (message.MessageType)
            {
            case MessageType.Chat:
                var newMsg = message as ChatMessage;
                System.Console.WriteLine("Chat message read.");
                await _clientWriter.WriteMessageToClient(newMsg.UserToId, newMsg);

                break;

            case MessageType.UserLogoff:
                var msg = message as UserLogoffMessage;
                _clientDisconnector.UserDisconnected(msg.UsersId);
                _clientWriter.WriteMessageToAllClients(new UserOfflineMessage {
                    UsersId = msg.UsersId
                });
                break;

            case MessageType.Typing:
                var typingMessage = message as UserTypingMessage;
                await _clientWriter.WriteMessageToClient(typingMessage.UserTypingToId, message);

                break;

            case MessageType.Image:
                var imageMessage = message as ImageMessage;
                await _clientWriter.WriteMessageToClient(imageMessage.UserToId, message);

                break;
            }
        }
Ejemplo n.º 2
0
        private async Task <bool> ProcessMessage(IMessage message, NetworkStream stream)
        {
            if (message is ConnectRequestMessage)
            {
                var connectRequestMessage = message as ConnectRequestMessage;

                var newMsg = new NewUserOnlineMessage()
                {
                    UserId = connectRequestMessage.UserId
                };

                _clientWriter.WriteMessageToAllClients(newMsg);

                var existingConn = _clientsHolder.ClientConnections.FirstOrDefault(c => c.UserId == newMsg.UserId);

                if (existingConn != null)
                {
                    _clientDisconnector.UserDisconnected((ushort)existingConn.UserId);
                }

                var connection = new ClientConnection(connectRequestMessage.UserId, stream);

                await NotifyNewUserOfOtherConnections(connection);

                _clientsHolder.ClientConnections.Add(connection);

                System.Console.WriteLine("Client joined with ID: " + connectRequestMessage.UserId);

                return(true);
            }
            return(false);
        }
Ejemplo n.º 3
0
        public void UserDisconnected(ushort userId)
        {
            var message = new UserOfflineMessage();

            message.UsersId = userId;

            RemoveUser(userId);

            _clientWriter.WriteMessageToAllClients(message);
        }