Skip to content

Commit

Permalink
feat: 重构发送请求方法,优化代码结构
Browse files Browse the repository at this point in the history
  • Loading branch information
shulng committed Sep 20, 2024
1 parent 1b3dc55 commit dbe2ae6
Showing 1 changed file with 16 additions and 14 deletions.
30 changes: 16 additions & 14 deletions src/main/java/cc/baka9/catseedlogin/bungee/Communication.java
Original file line number Diff line number Diff line change
@@ -1,9 +1,9 @@
package cc.baka9.catseedlogin.bungee;

import java.io.BufferedWriter;
import java.net.Socket;
import java.io.IOException;
import java.io.BufferedWriter;
import java.io.OutputStreamWriter;
import java.net.Socket;

import cc.baka9.catseedlogin.util.CommunicationAuth;

Expand All @@ -15,27 +15,29 @@ public class Communication {
private static final int PORT = Config.Port;

public static int sendConnectRequest(String playerName) {
try (Socket socket = new Socket(HOST, PORT);
BufferedWriter writer = new BufferedWriter(new OutputStreamWriter(socket.getOutputStream()))) {
writeMessage(writer, "Connect", playerName);
return socket.getInputStream().read();
} catch (IOException e) {
return 0;
}
return sendRequest("Connect", playerName);
}

public static void sendKeepLoggedInRequest(String playerName) {
long currentTime = System.currentTimeMillis();
String time = String.valueOf(currentTime);
String sign = CommunicationAuth.encryption(playerName, time, Config.AuthKey);
sendRequest("KeepLoggedIn", playerName, time, sign);
}

private static int sendRequest(String requestType, String... messages) {
try (Socket socket = new Socket(HOST, PORT);
BufferedWriter writer = new BufferedWriter(new OutputStreamWriter(socket.getOutputStream()))) {
long currentTime = System.currentTimeMillis();
String time = String.valueOf(currentTime);
String sign = CommunicationAuth.encryption(playerName, time, Config.AuthKey);
writeMessage(writer, "KeepLoggedIn", playerName, time, sign);
writeMessage(writer, requestType, messages);
return socket.getInputStream().read();
} catch (IOException e) {
return 0;
}
}

private static void writeMessage(BufferedWriter writer, String... messages) throws IOException {
private static void writeMessage(BufferedWriter writer, String requestType, String... messages) throws IOException {
writer.write(requestType);
writer.newLine();
for (String message : messages) {
writer.write(message);
writer.newLine();
Expand Down

0 comments on commit dbe2ae6

Please sign in to comment.