Exemplo n.º 1
0
        private void PersonalMessage(WsConnection connection, MessageContainer container)
        {
            var messageRequest = ((JObject)container.Payload).ToObject(typeof(MessageRequest)) as MessageRequest;

            if (!(((JObject)messageRequest?.MsgContainer)?.ToObject(typeof(TextMsgRequest)) is TextMsgRequest textMsgContainer))
            {
                return;
            }

            var serverOkPayload = new ServerOkMsgResponse(textMsgContainer.From, textMsgContainer.To, textMsgContainer.Date);
            MessageContainer serverOkContainer = serverOkPayload.GetContainer();

            _msgDataBase.Create(
                new MessageDto
            {
                Date       = textMsgContainer.Date,
                Message    = textMsgContainer.Message,
                ClientFrom = textMsgContainer.From,
                ClientTo   = textMsgContainer.To,
                MsgStatus  = Resources.ServerOkStatus
            });

            if (textMsgContainer.To == Resources.CommonChat)
            {
                BroadcastSend(container, textMsgContainer.From);
            }
            else
            {
                SendMessage(serverOkContainer, textMsgContainer.From);
                SendMessage(container, textMsgContainer.To);
            }

            UpdateLastClientActivity(textMsgContainer.From);
            SendUpdatedDataBaseMsgResponse();
        }
Exemplo n.º 2
0
        private void ClientRegistration(WsConnection connection, MessageContainer container)
        {
            var connectionRequest  = ((JObject)container.Payload).ToObject(typeof(ConnectionRequest)) as ConnectionRequest;
            var connectionResponse = new ConnectionResponse
            {
                Result = ResultCodes.Ok
            };

            if (connectionRequest == null)
            {
                return;
            }

            if (_connections.Values.Any(item => item.Login == connectionRequest.Login))
            {
                connectionResponse.Result = ResultCodes.Failure;
                connectionResponse.Reason = $"Client named '{connectionRequest.Login}' is already connected.";
                connection.Send(connectionResponse.GetContainer());
            }
            else
            {
                connection.Login = connectionRequest.Login;
                _connections.TryAdd(connection.Login, connection);

                connection.Send(connectionResponse.GetContainer());
                _clientsNotActiveList.Remove(connection.Login);

                SendUpdatedClientsList(new UpdatedClientsResponse(_connections.Keys, GetNotActiveClientsList()));
                _clientsActivity.TryAdd(connection.Login, new ClientActivity(connection.Login));

                _eventDataBase.Create(
                    new EventDto
                {
                    Date    = DateTime.Now,
                    Message = $"Client {connection.Login} connected"
                });

                List <string> dataBaseListLoginsString = GetDataBaseClientsListToString();

                if (!dataBaseListLoginsString.Contains(connection.Login))
                {
                    _clientDataBase.Create(
                        new ClientDto
                    {
                        Login = connection.Login
                    });
                }

                SendUpdatedDataBaseClientsResponse();
                SendUpdatedDataBaseMsgResponse();
                SendUpdatedDataBaseEventsResponse();
            }
        }
Exemplo n.º 3
0
        internal void HandleMessage(WsConnection connection, MessageContainer container)
        {
            switch (container.Identifier)
            {
            case MsgType.ClientRegistration:
                ClientRegistration(connection, container);
                break;

            case MsgType.PersonalMessage:
                PersonalMessage(connection, container);
                break;

            case MsgType.ClientOk:
                ClientOk(container);
                break;
            }
        }