handshaking
This commit is contained in:
@@ -15,9 +15,16 @@ public class ClientConsole implements ClientListener {
|
||||
private Client client;
|
||||
private final Thread inputThread;
|
||||
private final Scanner scanner;
|
||||
private volatile boolean connected = false;
|
||||
|
||||
public static void main(String[] args) {
|
||||
new ClientConsole(new InetSocketAddress("localhost", 6665));
|
||||
ClientConsole console = new ClientConsole(new InetSocketAddress("localhost", 6665));
|
||||
try {
|
||||
console.joinThread();
|
||||
} catch (InterruptedException e) {
|
||||
e.printStackTrace();
|
||||
}
|
||||
System.out.println("End !");
|
||||
}
|
||||
|
||||
public ClientConsole(InetSocketAddress address) {
|
||||
@@ -34,7 +41,8 @@ public class ClientConsole implements ClientListener {
|
||||
|
||||
private String inputPseudo() {
|
||||
System.out.println("Enter your pseudo:");
|
||||
String pseudo = this.scanner.nextLine();
|
||||
String pseudo = "chatter";
|
||||
pseudo = this.scanner.nextLine();
|
||||
return pseudo;
|
||||
}
|
||||
|
||||
@@ -43,11 +51,24 @@ public class ClientConsole implements ClientListener {
|
||||
}
|
||||
|
||||
private void inputLoop() {
|
||||
//waiting to be connected
|
||||
try {
|
||||
Thread.sleep(2000);
|
||||
return;
|
||||
} catch (InterruptedException e) {
|
||||
if (!connected)
|
||||
return;
|
||||
}
|
||||
|
||||
// resets the interrupt
|
||||
Thread.interrupted();
|
||||
|
||||
while (!Thread.interrupted()) {
|
||||
String message = scanner.nextLine();
|
||||
String message = this.scanner.nextLine();
|
||||
if (Thread.interrupted())
|
||||
break;
|
||||
visitMessage(message);
|
||||
}
|
||||
this.scanner.close();
|
||||
}
|
||||
|
||||
public void joinThread() throws InterruptedException {
|
||||
@@ -88,23 +109,34 @@ public class ClientConsole implements ClientListener {
|
||||
}
|
||||
}
|
||||
|
||||
private void stop() {
|
||||
this.inputThread.interrupt();
|
||||
}
|
||||
|
||||
@Override
|
||||
public void handleDisconnect() {
|
||||
System.out.println("Disconnected !");
|
||||
this.inputThread.interrupt();
|
||||
stop();
|
||||
}
|
||||
|
||||
@Override
|
||||
public void handleConnexionError() {
|
||||
System.out.println("An error occured during the connexion !");
|
||||
this.connected = false;
|
||||
stop();
|
||||
}
|
||||
|
||||
@Override
|
||||
public void handleChatMessage(Instant time, String chatter, String content) {
|
||||
StringBuilder sb = new StringBuilder();
|
||||
String strTime = time.toString();
|
||||
sb.append("&y[");
|
||||
sb.append(strTime, 11, 19); // We only take the HH:MM:SS part
|
||||
sb.append("]&n");
|
||||
sb.append(" ");
|
||||
sb.append(chatter);
|
||||
sb.append(" : ");
|
||||
sb.append(content).append("&n"); // make the color back to normal at the end of every message
|
||||
sb.append("&y[")
|
||||
.append(strTime, 11, 19) // We only take the HH:MM:SS part
|
||||
.append("]&n")
|
||||
.append(" ")
|
||||
.append(chatter)
|
||||
.append(" : ")
|
||||
.append(content).append("&n"); // make the color back to normal at the end of every message
|
||||
System.out.println(ANSIColor.formatString(sb.toString()));
|
||||
}
|
||||
|
||||
@@ -125,4 +157,11 @@ public class ClientConsole implements ClientListener {
|
||||
System.out.println(response);
|
||||
}
|
||||
|
||||
@Override
|
||||
public void handleConnect() {
|
||||
System.out.println("Connected to server !");
|
||||
this.connected = true;
|
||||
this.inputThread.interrupt();
|
||||
}
|
||||
|
||||
}
|
||||
|
||||
Reference in New Issue
Block a user