Chat works.

This commit is contained in:
Jonas 2022-03-27 12:07:21 +02:00
parent 63a8e21f6c
commit d2c2ca1732
3 changed files with 9 additions and 6 deletions

View File

@ -10,13 +10,13 @@ public class JClientProtocolParser {
* @param c this Client(required so this method can access the Client's methods) * @param c this Client(required so this method can access the Client's methods)
*/ */
public static void parse(String msg, Client c) { public static void parse(String msg, Client c) {
String header = ""; //"header" is the first 5 characters. String header = ""; //"header" is the first 5 characters, i.e. the protocol part
try { try {
header = msg.substring(0, 5); header = msg.substring(0, 5);
} catch (IndexOutOfBoundsException e) { } catch (IndexOutOfBoundsException e) {
e.printStackTrace(); e.printStackTrace();
} }
System.out.println(header); //System.out.println(header); helpful for debugging
switch (header) { switch (header) {
case "SPING": case "SPING":
c.sendMsgToServer("PINGB"); c.sendMsgToServer("PINGB");
@ -24,6 +24,9 @@ public class JClientProtocolParser {
case "PINGB": case "PINGB":
c.clientPinger.setGotPingBack(true); c.clientPinger.setGotPingBack(true);
break; break;
case "CHATM":
System.out.println(msg.substring(6));
break;
default: default:
System.out.println("Received unknown command"); System.out.println("Received unknown command");
} }

View File

@ -102,9 +102,9 @@ public class ClientHandler implements Runnable {
for (ClientHandler client : connectedClients) { for (ClientHandler client : connectedClients) {
try { try {
if (!client.clientUserName.equals((clientUserName))) { if (!client.clientUserName.equals((clientUserName))) {
client.out.write(msg); client.out.write("CHATM:" + msg);
} else { } else {
client.out.write("Message: **" + msg + "** sent!"); client.out.write("CHATM:Message: **" + msg + "** sent!");
} }
client.out.newLine(); client.out.newLine();
client.out.flush(); client.out.flush();

View File

@ -11,13 +11,13 @@ public class JServerProtocolParser {
* @param h this ClientHandler (required so this method can access the ClientHandler's methods) * @param h this ClientHandler (required so this method can access the ClientHandler's methods)
*/ */
public static void parse(String msg, ClientHandler h) { public static void parse(String msg, ClientHandler h) {
String header = ""; //"header" is the first 5 characters. String header = ""; //"header" is the first 5 characters, i.e. the protocol part
try { try {
header = msg.substring(0, 5); header = msg.substring(0, 5);
} catch (IndexOutOfBoundsException e) { } catch (IndexOutOfBoundsException e) {
e.printStackTrace(); e.printStackTrace();
} }
System.out.println(header); //System.out.println(header); helpful for debugging
switch (header) { switch (header) {
case "CHATA": case "CHATA":
h.broadcastMessage(msg.substring(6)); h.broadcastMessage(msg.substring(6));