56 lines
1.6 KiB
Java
56 lines
1.6 KiB
Java
package client;
|
|
|
|
import java.net.DatagramSocket;
|
|
import java.net.InetSocketAddress;
|
|
import java.net.SocketException;
|
|
|
|
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;
|
|
import network.protocol.packets.RequestRoomListPacket;
|
|
import network.protocol.packets.SendChatMessagePacket;
|
|
|
|
public class Client {
|
|
|
|
private final ClientConnexion connexion;
|
|
private final ClientListener callback;
|
|
|
|
public Client(InetSocketAddress serverAddress, ClientListener callback, String pseudo) throws SocketException {
|
|
this.connexion = new ClientConnexion(new DatagramSocket(), serverAddress, callback);
|
|
this.callback = callback;
|
|
login(pseudo);
|
|
}
|
|
|
|
public void close() {
|
|
this.connexion.sendPacket(new DisconnectPacket("Leaving"));
|
|
this.connexion.close();
|
|
this.callback.handleDisconnect();
|
|
}
|
|
|
|
private void login(String pseudo) {
|
|
this.connexion.sendPacket(new LoginPacket(pseudo));
|
|
}
|
|
|
|
public void SendChatMessage(String message) {
|
|
this.connexion.sendPacket(new SendChatMessagePacket(message));
|
|
}
|
|
|
|
public void SendCreateRoom(String roomName) {
|
|
this.connexion.sendPacket(new CreateRoomPacket(roomName));
|
|
}
|
|
|
|
public void SendJoinRoom(String roomName) {
|
|
this.connexion.sendPacket(new JoinRoomPacket(roomName));
|
|
}
|
|
|
|
public void SendLeaveRoom() {
|
|
this.connexion.sendPacket(new LeaveRoomPacket());
|
|
}
|
|
|
|
public void RequestRoomList() {
|
|
this.connexion.sendPacket(new RequestRoomListPacket());
|
|
}
|
|
}
|