/// <summary> /// Called when a client establishes a connection with the server. /// </summary> /// <param name="state">The client's socket state.</param> private void ClientConnectionEstablished(SocketState state) { // Add a new client communicator. var communicator = new ClientCommunicator(this, state); _clients.Add(communicator.Id, communicator); // Create a ship when the client sends their nickname. communicator.NicknameReceived += nickname => { var ship = new Ship(communicator.Id, nickname); _world.PutComponent(ship); }; // Handle the case where the client disconnects. communicator.Disconnected += () => { // Remove the client's ship. _world.GetComponent <Ship>(communicator.Id).Health = 0; // Remove the client communicator. _clients.Remove(communicator.Id); // Notify listeners. ClientDisconnected?.Invoke(); }; // Start the listening process. communicator.BeginListeningAsync(); // Notify listeners of a newly connected client. ClientConnected?.Invoke(); }
/// <summary> /// Called when a client establishes a connection with the server. /// </summary> /// <param name="state">The client's socket state.</param> private void ClientConnectionEstablished(SocketState state) { // Add a new client communicator. var communicator = new ClientCommunicator(this, state); _clients.Add(communicator.Id, communicator); // Handle the case where the client disconnects. communicator.Disconnected += () => { // Remove the client communicator. _clients.Remove(communicator.Id); // Notify listeners. ClientDisconnected?.Invoke(); }; // Start the listening process. communicator.BeginListeningAsync(); // Notify listeners of a newly connected client. ClientConnected?.Invoke(); }