quick fix & server timeout after 10sec

This commit is contained in:
ayunami2000 2022-07-08 10:34:00 -04:00
parent 6336ee7fa8
commit 5e5e54dde1
4 changed files with 9 additions and 5 deletions

View File

@ -24,9 +24,6 @@ public class ChatHandler {
try {
int destServer = Integer.parseInt(args);
client.server = Math.max(0, Math.min(Main.servers.size() - 1, destServer));
try {
client.socket.close();
} catch (IOException ignored) {}
} catch (NumberFormatException e) {
//not a number
client.conn.send(new byte[] { 3, 0, 29, 0, (byte) 167, 0, 57, 0, 84, 0, 104, 0, 97, 0, 116, 0, 32, 0, 105, 0, 115, 0, 32, 0, 110, 0, 111, 0, 116, 0, 32, 0, 97, 0, 32, 0, 118, 0, 97, 0, 108, 0, 105, 0, 100, 0, 32, 0, 110, 0, 117, 0, 109, 0, 98, 0, 101, 0, 114, 0, 33 });

View File

@ -282,7 +282,6 @@ public class Main {
try {
int destServer = Integer.parseInt(pieces[2]);
targetUser.server = Math.max(0, Math.min(servers.size() - 1, destServer));
targetUser.socket.close();
printMsg("Successfully send user " + targetUser + " to server " + destServer + "!");
} catch (NumberFormatException e) {
printMsg("That is not a valid number!");

View File

@ -7,6 +7,7 @@ import org.java_websocket.server.WebSocketServer;
import java.io.IOException;
import java.net.InetSocketAddress;
import java.net.Socket;
import java.net.SocketTimeoutException;
import java.nio.ByteBuffer;
import java.time.Instant;
import java.util.*;
@ -125,7 +126,14 @@ public class WebSocketProxy extends WebSocketServer {
selfClient.hasLoginHappened = false;
if (!selfClient.firstTime) Main.printMsg("Player " + selfClient + " joined server " + selfClient.server + "!");
ServerItem chosenServer = Main.servers.get(currServer);
Socket selfSocket = new Socket(chosenServer.host, chosenServer.port);
Socket selfSocket = new Socket();
try {
// todo: pregenerate InetSocketAddresses
selfSocket.connect(new InetSocketAddress(chosenServer.host, chosenServer.port), 10000);
} catch (SocketTimeoutException e) {
conn.close();
return;
}
selfClient.setSocket(selfSocket);
if (!selfClient.firstTime) sendToServer(selfClient.handshake, selfClient);
if (selfClient.firstTime) {