established connection between Client and Serveur, setting up basic interactions
This commit is contained in:
@@ -5,13 +5,21 @@ import java.net.DatagramSocket;
|
||||
import java.net.InetSocketAddress;
|
||||
import java.net.SocketException;
|
||||
|
||||
import network.protocol.packets.LoginPacket;
|
||||
import network.protocol.packets.SendChatMessagePacket;
|
||||
import network.protocol.packets.*;
|
||||
|
||||
public class Client {
|
||||
|
||||
private final ClientConnexion connexion;
|
||||
|
||||
public static void main(String[] args) {
|
||||
try {
|
||||
Client client = new Client(new InetSocketAddress("localhost", 6665));
|
||||
client.SendChatMessage("Hello");
|
||||
} catch (SocketException e) {
|
||||
e.printStackTrace();
|
||||
}
|
||||
}
|
||||
|
||||
public Client(InetSocketAddress serverAddress) throws SocketException {
|
||||
this.connexion = new ClientConnexion(new DatagramSocket(), serverAddress);
|
||||
login("Moi");
|
||||
@@ -32,4 +40,20 @@ public class Client {
|
||||
e.printStackTrace();
|
||||
}
|
||||
}
|
||||
|
||||
public void SendCreateRoom(String roomName) {
|
||||
try {
|
||||
this.connexion.sendPacket(new CreateRoomPacket(roomName));
|
||||
} catch (Exception e) {
|
||||
e.printStackTrace();
|
||||
}
|
||||
}
|
||||
|
||||
public void RequestRoomList() {
|
||||
try {
|
||||
this.connexion.sendPacket(new RequestRoomListPacket());
|
||||
} catch (Exception e) {
|
||||
e.printStackTrace();
|
||||
}
|
||||
}
|
||||
}
|
||||
|
||||
@@ -3,6 +3,7 @@ package client;
|
||||
import java.io.IOException;
|
||||
import java.net.DatagramSocket;
|
||||
import java.net.InetSocketAddress;
|
||||
import java.time.Instant;
|
||||
|
||||
import network.PacketHandler;
|
||||
import network.SocketReader;
|
||||
@@ -47,7 +48,14 @@ public class ClientConnexion implements PacketVisitor, PacketHandler{
|
||||
|
||||
@Override
|
||||
public void visitPacket(ChatMessagePacket packet) {
|
||||
throw new UnsupportedOperationException("Unimplemented method 'visitPacket'");
|
||||
StringBuilder sb = new StringBuilder();
|
||||
String time = packet.getTime().toString();
|
||||
sb.append(time, 11, 19); // We only take the HH:MM:SS part
|
||||
sb.append(" ");
|
||||
sb.append(packet.getChatter());
|
||||
sb.append(" : ");
|
||||
sb.append(packet.getContent());
|
||||
System.out.println(sb);
|
||||
}
|
||||
|
||||
@Override
|
||||
@@ -77,8 +85,12 @@ public class ClientConnexion implements PacketVisitor, PacketHandler{
|
||||
|
||||
@Override
|
||||
public void visitPacket(RoomListPacket packet) {
|
||||
System.out.println("Handled room list !");
|
||||
// System.out.println("Handled room list !");
|
||||
// throw new UnsupportedOperationException("Unimplemented method 'visitPacket'");
|
||||
System.out.println("Rooms :");
|
||||
for (String room : packet.getRoomNames()) {
|
||||
System.out.println("\t" + room);
|
||||
}
|
||||
}
|
||||
|
||||
@Override
|
||||
|
||||
Reference in New Issue
Block a user