Basics of client server communication and drawing of gamestates finished
This commit is contained in:
@@ -2,6 +2,8 @@ package networking;
|
||||
|
||||
import logging.ClientLogger;
|
||||
import logging.LogType;
|
||||
|
||||
import javax.swing.*;
|
||||
import java.io.DataInputStream;
|
||||
import java.io.DataOutputStream;
|
||||
import java.io.IOException;
|
||||
@@ -37,8 +39,9 @@ public class Client {
|
||||
out.writeInt(165313125);
|
||||
out.flush();
|
||||
success = in.readInt() == 200;
|
||||
if (success){
|
||||
if (success) {
|
||||
out.writeUTF(name);
|
||||
out.flush();
|
||||
serverName = in.readUTF();
|
||||
clientLogger.printLog("You successfully connected to me", serverName, success, LogType.Log);
|
||||
} else {
|
||||
@@ -55,18 +58,37 @@ public class Client {
|
||||
out.writeUTF(message);
|
||||
out.flush();
|
||||
success = in.readInt() == 200;
|
||||
clientLogger.printLog(String.format("Sent the message: %s", message), serverName, success, LogType.Log);
|
||||
} catch (IOException e) {
|
||||
e.printStackTrace();
|
||||
}
|
||||
}
|
||||
|
||||
public String getGameState(){
|
||||
public String getResponse() {
|
||||
try {
|
||||
out.writeUTF("gamestate");
|
||||
return in.readUTF();
|
||||
String message = in.readUTF();
|
||||
clientLogger.printLog(String.format("Message recieved: %s", message), serverName, success, LogType.Log);
|
||||
return message;
|
||||
} catch (IOException e) {
|
||||
e.printStackTrace();
|
||||
}
|
||||
return "";
|
||||
}
|
||||
|
||||
public String getGameState() {
|
||||
this.sendToServer("gameState");
|
||||
String gameState = this.getResponse();
|
||||
return gameState;
|
||||
}
|
||||
|
||||
public void exitProcess(){
|
||||
try {
|
||||
out.writeUTF("exit()");
|
||||
out.flush();
|
||||
success = in.readInt()==200;
|
||||
clientLogger.printLog("Closing connection to server", serverName, success, LogType.Log);
|
||||
} catch (IOException e) {
|
||||
e.printStackTrace();
|
||||
return "";
|
||||
}
|
||||
}
|
||||
|
||||
@@ -74,6 +96,8 @@ public class Client {
|
||||
return success;
|
||||
}
|
||||
|
||||
public String getName(){return name;}
|
||||
public String getName() {
|
||||
return name;
|
||||
}
|
||||
|
||||
}
|
||||
Reference in New Issue
Block a user