cli for Client
This commit is contained in:
@@ -11,11 +11,12 @@ public class ChatApp {
|
||||
|
||||
client.SendCreateRoom("Room1");
|
||||
client.RequestRoomList();
|
||||
client.SendChatMessage("Hello");
|
||||
|
||||
Scanner scanner = new Scanner(System.in);
|
||||
while (true) {
|
||||
Scanner scan = new Scanner(System.in);
|
||||
String message = scan.nextLine();
|
||||
client.SendChatMessage(message);
|
||||
String message = scanner.nextLine();
|
||||
client.visitMessage(message);
|
||||
}
|
||||
}
|
||||
}
|
||||
|
||||
@@ -4,6 +4,8 @@ import java.io.IOException;
|
||||
import java.net.DatagramSocket;
|
||||
import java.net.InetSocketAddress;
|
||||
import java.net.SocketException;
|
||||
import java.util.Objects;
|
||||
import java.util.Scanner;
|
||||
|
||||
import network.protocol.packets.*;
|
||||
|
||||
@@ -14,7 +16,11 @@ public class Client {
|
||||
public static void main(String[] args) {
|
||||
try {
|
||||
Client client = new Client(new InetSocketAddress("localhost", 6665));
|
||||
client.SendChatMessage("Hello");
|
||||
Scanner scanner = new Scanner(System.in);
|
||||
while(true) {
|
||||
String message = scanner.nextLine();
|
||||
client.visitMessage(message);
|
||||
}
|
||||
} catch (SocketException e) {
|
||||
e.printStackTrace();
|
||||
}
|
||||
@@ -25,6 +31,30 @@ public class Client {
|
||||
login("Moi");
|
||||
}
|
||||
|
||||
public void visitMessage(String message){
|
||||
try {
|
||||
if(message.startsWith("/")){
|
||||
if(message.startsWith("/createRoom")) {
|
||||
String roomName = message.substring(12).trim();
|
||||
SendCreateRoom(roomName);
|
||||
} else if(message.startsWith("/listRooms")) {
|
||||
RequestRoomList();
|
||||
} else if(message.startsWith("/joinRoom")) {
|
||||
String roomName = message.substring(10).trim();
|
||||
SendJoinRoom(roomName);
|
||||
} else if(message.startsWith("/leaveRoom")) {
|
||||
SendLeaveRoom();
|
||||
} else {
|
||||
System.out.println("Unknown command");
|
||||
}
|
||||
} else {
|
||||
SendChatMessage(message);
|
||||
}
|
||||
} catch (Exception e) {
|
||||
e.printStackTrace();
|
||||
}
|
||||
}
|
||||
|
||||
private void login(String pseudo) {
|
||||
try {
|
||||
this.connexion.sendPacket(new LoginPacket(pseudo));
|
||||
@@ -49,6 +79,22 @@ public class Client {
|
||||
}
|
||||
}
|
||||
|
||||
public void SendJoinRoom(String roomName) {
|
||||
try {
|
||||
this.connexion.sendPacket(new JoinRoomPacket(roomName));
|
||||
} catch (Exception e) {
|
||||
e.printStackTrace();
|
||||
}
|
||||
}
|
||||
|
||||
public void SendLeaveRoom() {
|
||||
try {
|
||||
this.connexion.sendPacket(new LeaveRoomPacket());
|
||||
} catch (Exception e) {
|
||||
e.printStackTrace();
|
||||
}
|
||||
}
|
||||
|
||||
public void RequestRoomList() {
|
||||
try {
|
||||
this.connexion.sendPacket(new RequestRoomListPacket());
|
||||
|
||||
@@ -9,7 +9,7 @@ public class ServerResponsePacket extends Packet {
|
||||
static private final long serialVersionUID = Packets.Login.ordinal();
|
||||
|
||||
public static enum Response {
|
||||
AuthSuccess, AuthError, RoomCreated, RoomNotCreated, RoomJoined, RoomNotJoined, NotInRoom, RoomLeft, RoomNotLeft, messageSent, messageNotSent;
|
||||
AuthSuccess, AuthError, RoomCreated, RoomNotCreated, RoomJoined, RoomNotJoined, NotInRoom, RoomLeft, RoomNotLeft, MessageSent, MessageNotSent;
|
||||
};
|
||||
|
||||
private final Response response;
|
||||
|
||||
@@ -107,9 +107,9 @@ public class ServerConnexion implements PacketVisitor {
|
||||
public void visitPacket(SendChatMessagePacket packet) {
|
||||
try {
|
||||
server.sendToRoom(this, packet);
|
||||
sendPacket(new ServerResponsePacket(Response.messageSent));
|
||||
sendPacket(new ServerResponsePacket(Response.MessageSent));
|
||||
} catch (SocketException e) {
|
||||
sendPacket(new ServerResponsePacket(Response.messageNotSent));
|
||||
sendPacket(new ServerResponsePacket(Response.MessageNotSent));
|
||||
}
|
||||
}
|
||||
|
||||
|
||||
Reference in New Issue
Block a user