add disconnect

This commit is contained in:
2025-03-01 13:00:58 +01:00
parent 07ad2ba05e
commit 63ec7b3aaa
6 changed files with 86 additions and 27 deletions

View File

@@ -10,6 +10,7 @@ import network.protocol.Packet;
import network.protocol.PacketVisitor;
import network.protocol.packets.ChatMessagePacket;
import network.protocol.packets.CreateRoomPacket;
import network.protocol.packets.DisconnectPacket;
import network.protocol.packets.JoinRoomPacket;
import network.protocol.packets.LeaveRoomPacket;
import network.protocol.packets.LoginPacket;
@@ -41,15 +42,10 @@ public class ServerConnexion implements PacketVisitor {
this.writer.sendPacket(packet, clientAddress);
} catch (IOException e) {
e.printStackTrace();
this.server.removeConnexion(this);
}
}
@Override
public void visitPacket(ChatMessagePacket packet) {
// I'm never supposed to receive this from the client
throw new UnsupportedOperationException("Unimplemented method 'visitPacket'");
}
@Override
public void visitPacket(CreateRoomPacket packet) {
try {
@@ -97,12 +93,6 @@ public class ServerConnexion implements PacketVisitor {
sendPacket(new RoomListPacket(server.getRoomNames()));
}
@Override
public void visitPacket(RoomListPacket packet) {
// I'm never supposed to receive this from the client
throw new UnsupportedOperationException("Unimplemented method 'visitPacket'");
}
@Override
public void visitPacket(SendChatMessagePacket packet) {
try {
@@ -112,6 +102,28 @@ public class ServerConnexion implements PacketVisitor {
}
}
@Override
public void visitPacket(DisconnectPacket packet) {
this.onDisconnect();
}
private void onDisconnect() {
this.server.removeConnexion(this);
System.out.println("[Server] Chatter " + chatterName + " disconnected !");
}
@Override
public void visitPacket(RoomListPacket packet) {
// I'm never supposed to receive this from the client
throw new UnsupportedOperationException("Unimplemented method 'visitPacket'");
}
@Override
public void visitPacket(ChatMessagePacket packet) {
// I'm never supposed to receive this from the client
throw new UnsupportedOperationException("Unimplemented method 'visitPacket'");
}
@Override
public void visitPacket(ServerResponsePacket packet) {
// I'm never supposed to receive this from the client