Relocated message formating from server to client
This commit is contained in:
parent
2f7db70df7
commit
ced0f0aa9e
@ -2,10 +2,12 @@ package ch.unibas.dmi.dbis.cs108.multiplayer.client;
|
|||||||
|
|
||||||
import ch.unibas.dmi.dbis.cs108.multiplayer.helpers.ClientPinger;
|
import ch.unibas.dmi.dbis.cs108.multiplayer.helpers.ClientPinger;
|
||||||
|
|
||||||
|
import ch.unibas.dmi.dbis.cs108.multiplayer.server.MessageFormatter;
|
||||||
import java.net.Socket;
|
import java.net.Socket;
|
||||||
import java.io.*;
|
import java.io.*;
|
||||||
import java.net.UnknownHostException;
|
import java.net.UnknownHostException;
|
||||||
import java.util.Scanner;
|
import java.util.Scanner;
|
||||||
|
import org.apache.logging.log4j.message.Message;
|
||||||
|
|
||||||
public class Client {
|
public class Client {
|
||||||
|
|
||||||
@ -39,7 +41,8 @@ public class Client {
|
|||||||
Scanner sc = new Scanner(System.in);
|
Scanner sc = new Scanner(System.in);
|
||||||
while (socket.isConnected()) {
|
while (socket.isConnected()) {
|
||||||
String msg = sc.nextLine();
|
String msg = sc.nextLine();
|
||||||
out.write(msg);
|
String formattedMSG = MessageFormatter.formatMsg(msg);
|
||||||
|
out.write(formattedMSG);
|
||||||
out.newLine();
|
out.newLine();
|
||||||
out.flush();
|
out.flush();
|
||||||
}
|
}
|
||||||
|
|||||||
@ -11,16 +11,15 @@ public class JServerProtocolParser {
|
|||||||
*/
|
*/
|
||||||
public static void parse(String msg, ClientHandler h) {
|
public static void parse(String msg, ClientHandler h) {
|
||||||
String header = ""; //"header" is the first 5 characters, i.e. the protocol part
|
String header = ""; //"header" is the first 5 characters, i.e. the protocol part
|
||||||
String formattedMSG = MessageFormatter.formatMsg(msg);
|
try {
|
||||||
try {
|
header = msg.substring(0, 5);
|
||||||
header = formattedMSG.substring(0, 5);
|
|
||||||
} catch (IndexOutOfBoundsException e) {
|
} catch (IndexOutOfBoundsException e) {
|
||||||
System.out.println("Received unknown command");
|
System.out.println("Received unknown command");
|
||||||
}
|
}
|
||||||
//System.out.println(header); helpful for debugging
|
//System.out.println(header); helpful for debugging
|
||||||
switch (header) {
|
switch (header) {
|
||||||
case "CHATA":
|
case "CHATA":
|
||||||
h.broadcastMessage(formattedMSG.substring(6));
|
h.broadcastMessage(msg.substring(6));
|
||||||
break;
|
break;
|
||||||
case "CPING":
|
case "CPING":
|
||||||
h.sendMsgToClient("PINGB");
|
h.sendMsgToClient("PINGB");
|
||||||
|
|||||||
@ -30,6 +30,8 @@ public class MessageFormatter {
|
|||||||
stringBuilder.append("QUITS");
|
stringBuilder.append("QUITS");
|
||||||
s = msg.substring(2);
|
s = msg.substring(2);
|
||||||
break;
|
break;
|
||||||
|
case "/n":
|
||||||
|
stringBuilder.append("NAMEC");
|
||||||
default:
|
default:
|
||||||
s = msg;
|
s = msg;
|
||||||
}
|
}
|
||||||
|
|||||||
Reference in New Issue
Block a user