internal void HandleMessage(Guid clientId, Container container) { if (!_connections.TryGetValue(clientId, out WsConnection connection)) { return; } _connections.RefreshLifeClient(clientId); switch (container.Identifier) { case DispatchType.Login: if (((JObject)container.Payload).ToObject(typeof(LoginRequestContainer)) is LoginRequestContainer loginRequest) { LoginResponseContainer loginResponse; bool isEnter = true; DispatchType stage; if (UserOnlineList.Contains(loginRequest.Content)) { loginResponse = new LoginResponseContainer( new Response(ResponseType.Failure, $"Client with name '{loginRequest.Content}' yet connect."), null, null, null, null); connection.Send(loginResponse.GetContainer()); connection.Login = $"pseudo-{loginRequest.Content}"; stage = DispatchType.Connection; } else { isEnter = UserOfflineList.Contains(loginRequest.Content); loginResponse = new LoginResponseContainer(new Response(ResponseType.Ok, "Connected"), null, null, null, null); connection.Login = loginRequest.Content; stage = DispatchType.Login; } var eventLogMessage = new EventLogMessage { IsSuccessfully = loginResponse.Content.Result == ResponseType.Ok == isEnter, SenderName = _name, Text = loginResponse.Content.Reason, Time = DateTime.Now, Type = stage }; ConnectionStateChanged?.Invoke(this, new ConnectionStateChangedEventArgs(connection.Login, true, eventLogMessage)); } break; case DispatchType.Message: MessageReceivedEventArgs message = MessageSorter.GetSortedMessage((JObject)container.Payload); MessageReceived?.Invoke(this, message); Send(MessageSorter.GetSortedMessage(message.Author, message.Message, InterfaceType.WebSocket, message.Agenda), message.Agenda); break; } }
private async void SendLoginInitAsync(ConnectionStateChangedEventArgs eventArgs) { await Task.Delay(1000); List <GeneralMessage> generalMessage = await _dataBaseManager.GetGeneralMessageAsync(); List <PrivateMessage> privateMessages = await _dataBaseManager.GetPrivateMessageAsync(); List <Event> events = await _dataBaseManager.GetEventAsync(); await Task.Run( () => { User user = _dataBaseManager.UserList.Find(u => u.Name == eventArgs.ClientName); var lrc = new LoginResponseContainer( new Response(ResponseType.Ok, eventArgs.EventLog.Text), Collector.CollectGeneralChannel(user.Id, generalMessage), Collector.CollectOnlineChannel(user, CheckUserList(_wsServer.UserOnlineList, _tcpServer.UserOnlineList), privateMessages), Collector.CollectOfflineChannel(user, CheckUserList(_wsServer.UserOfflineList, _tcpServer.UserOfflineList), privateMessages), Collector.CollectEventLog(user, events)); _wsServer.Send(lrc, new PrivateAgenda(eventArgs.ClientName)); _tcpServer.Send(lrc, new PrivateAgenda(eventArgs.ClientName)); }); }
internal void HandlePacket(IPEndPoint remoteEndpoint, byte[] packet) { string serializedMessages = GetStringPacket(packet); var container = JsonConvert.DeserializeObject <Container>(serializedMessages); if (!_connections.TryGetValue(remoteEndpoint, out TcpConnection connection)) { return; } _connections.RefreshLifeClient(remoteEndpoint); switch (container.Identifier) { case DispatchType.Login: if (((JObject)container.Payload).ToObject(typeof(LoginRequestContainer)) is LoginRequestContainer loginRequest) { LoginResponseContainer loginResponse; bool isEnter = true; DispatchType stage; if (UserOnlineList.Contains(loginRequest.Content)) { loginResponse = new LoginResponseContainer( new Response(ResponseType.Failure, $"Client with name '{loginRequest.Content}' yet connect."), null, null, null, null); connection.Send(ConvertToBytes(loginResponse.GetContainer())); connection.Login = $"pseudo-{loginRequest.Content}"; stage = DispatchType.Connection; } else { isEnter = UserOfflineList.Contains(loginRequest.Content); loginResponse = new LoginResponseContainer(new Response(ResponseType.Ok, "Connected"), null, null, null, null); connection.Login = loginRequest.Content; stage = DispatchType.Login; } var eventLogMessage = new EventLogMessage { IsSuccessfully = loginResponse.Content.Result == ResponseType.Ok == isEnter, SenderName = _name, Text = loginResponse.Content.Reason, Time = DateTime.Now, Type = stage }; ConnectionStateChanged?.Invoke(this, new ConnectionStateChangedEventArgs(connection.Login, true, eventLogMessage)); } break; case DispatchType.Message: MessageReceivedEventArgs message = MessageSorter.GetSortedMessage((JObject)container.Payload); MessageReceived?.Invoke(this, message); Send(MessageSorter.GetSortedMessage(message.Author, message.Message, InterfaceType.Tcp, message.Agenda), message.Agenda); break; } }