private void Server_OnConnectionRemoved(object sender, PacketEvents e) { if (!Usernames.ContainsKey(e.Sender.ClientId.ToString())) { return; } var notification = new ChatPacket { Username = "******", Message = "A user has left the chat", UserColor = Colors.Purple.ToString() }; var userPacket = new UserConnectionPacket { UserGuid = e.Sender.ClientId.ToString(), Username = Usernames[e.Sender.ClientId.ToString()], IsJoining = false }; if (Usernames.Keys.Contains(userPacket.UserGuid)) { Usernames.Remove(userPacket.UserGuid); } userPacket.Users = Usernames.Values.ToArray(); if (_server.Connections.Count != 0) { Task.Run(() => _server.SendObjectToClients(userPacket)).Wait(); Task.Run(() => _server.SendObjectToClients(notification)).Wait(); } WriteOutput("Client Disconnected: " + e.Sender.Socket.RemoteEndPoint.ToString()); }
private async Task <PersonalPacket> GetNewConnectionPacket(string username) { _listenTask = Task.Run(() => _client.Connect()); IsRunning = await _listenTask; var notifyServer = new UserConnectionPacket { Username = username, IsJoining = true, UserGuid = _client.ClientId.ToString() }; var personalPacket = new PersonalPacket { GuidId = _client.ClientId.ToString(), Package = notifyServer }; return(personalPacket); }