Example #1
0
        internal void OnMessage(Guid clientId, MessageContainer container)
        {
            if (!_connections.TryGetValue(clientId, out WsConnection connection))
            {
                return;
            }

            switch (container.Identifier)
            {
            case nameof(ConnectionRequest):
                var connectionRequest  = ((JObject)container.Payload).ToObject(typeof(ConnectionRequest)) as ConnectionRequest;
                var connectionResponse = new ConnectionResponse
                {
                    Result    = ResultCode.Ok,
                    IsSuccess = true,
                };
                if (_connections.Values.Any(c => c.Username == connectionRequest.Username))
                {
                    string reason = $"User '{connectionRequest.Username}' is already logged in.";
                    connectionResponse.Result    = ResultCode.Failure;
                    connectionResponse.IsSuccess = false;
                    connectionResponse.Reason    = reason;
                    connection.Send(connectionResponse.GetContainer());
                    ErrorReceived?.Invoke(this, new ErrorReceivedEventArgs(reason, DateTime.Now));
                }
                else
                {
                    connection.Username            = connectionRequest.Username;
                    connectionResponse.ActiveUsers = _connections.Where(c => c.Value.Username != null).Select(u => u.Value.Username).ToList();
                    connection.Send(connectionResponse.GetContainer());
                    ConnectionStateChanged?.Invoke(this, new ConnectionStateChangedEventArgs(connection.Username, true, DateTime.Now));
                    ConnectionReceived?.Invoke(this, new ConnectionReceivedEventArgs(connection.Username, true, DateTime.Now));
                }
                break;

            case nameof(MessageRequest):
                var messageRequest = ((JObject)container.Payload).ToObject(typeof(MessageRequest)) as MessageRequest;
                MessageReceived?.Invoke(this, new MessageReceivedEventArgs(connection.Username, messageRequest.Target, messageRequest.Message, messageRequest.Groupname, DateTime.Now));
                break;

            case nameof(FiltrationRequest):
                var filtrationRequest = ((JObject)container.Payload).ToObject(typeof(FiltrationRequest)) as FiltrationRequest;
                FiltrationReceived?.Invoke(this, new FiltrationReceivedEventArgs(connection.Username, filtrationRequest.FirstDate, filtrationRequest.SecondDate, filtrationRequest.EventType));
                break;

            case nameof(CreateGroupRequest):
                var createGroupRequest = ((JObject)container.Payload).ToObject(typeof(CreateGroupRequest)) as CreateGroupRequest;
                createGroupRequest.UserList.Add(connection.Username);
                GroupCreated?.Invoke(this, new GroupCreatedEventArgs(createGroupRequest.Groupname, createGroupRequest.UserList));
                break;

            case nameof(LeaveGroupRequest):
                var leaveGroupRequest = ((JObject)container.Payload).ToObject(typeof(LeaveGroupRequest)) as LeaveGroupRequest;
                GroupLeaved?.Invoke(this, new GroupLeavedEventArgs(connection.Username, leaveGroupRequest.Groupname));
                break;
            }
        }
Example #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();
            }
        }