调整一下代码

main
lensfrex 2 years ago
parent 3fc7f6131a
commit 2f5c8d52d4
Signed by: lensfrex
GPG Key ID: 947ADABD8533C476
  1. 6
      exp3-network/src/main/java/net/lensfrex/URLConnectionResource.java
  2. 98
      exp3-network/src/main/java/net/lensfrex/socket/client/Client.java
  3. 4
      exp3-network/src/main/java/net/lensfrex/socket/client/Global.java
  4. 7
      exp3-network/src/main/java/net/lensfrex/socket/client/MainUI.java
  5. 19
      exp3-network/src/main/java/net/lensfrex/socket/common/Common.java
  6. 8
      exp3-network/src/main/java/net/lensfrex/socket/server/Server.java

@ -18,9 +18,9 @@ public class URLConnectionResource {
Set<Map.Entry<String, List<String>>> headers = urlConnection.getHeaderFields().entrySet();
for (Map.Entry<String, List<String>> entry : headers) {
String key = entry.getKey();
List<String> values = entry.getValue();
for (Map.Entry<String, List<String>> header : headers) {
String key = header.getKey();
List<String> values = header.getValue();
for (String value : values) {
System.out.printf("%s: %s%n", key, value);
}

@ -17,7 +17,6 @@ import net.lensfrex.socket.common.MessageData;
import java.io.IOException;
import java.io.InputStream;
import java.io.OutputStream;
import java.net.URL;
import java.nio.charset.StandardCharsets;
import java.util.Random;
@ -32,9 +31,9 @@ public class Client extends Application {
Scene scene = new Scene(new UI().buildUI(), 515, 509);
// 主题,不想要或者网络出错的话注释掉就好了
// scene.getStylesheets().add(new URL("https://www.lensfrex.net/light.css").toExternalForm());
scene.getStylesheets().add(new URL("https://www.lensfrex.net/light.css").toExternalForm());
// scene.getStylesheets().add(new URL("https://www.lensfrex.net/dark.css").toExternalForm());
scene.getStylesheets().add(new URL("http://127.0.0.1/light.css").toExternalForm());
// scene.getStylesheets().add(new URL("http://127.0.0.1/light.css").toExternalForm());
stage.setTitle("( ̄﹃ ̄)");
stage.setScene(scene);
@ -53,9 +52,11 @@ class UI {
private TextField serverTextField;
private TextArea messageTextArea;
private HBox userControlVBox;
private TextField usernameTextField;
private Button loginButton, logoutButton;
private HBox messageControlVBox;
private TextField messageTextField;
private Button sendButton;
@ -109,16 +110,10 @@ class UI {
loginButton = new Button("登录");
loginButton.setOnAction(e -> {
try {
this.auth(Constant.OPCODE_LOGIN);
logoutButton.setDisable(false);
loginButton.setDisable(true);
serverTextField.setDisable(true);
usernameTextField.setDisable(true);
messageControlVBox.setDisable(false);
this.changeLoginStatus(true);
} catch (Exception ex) {
ex.printStackTrace();
messageTextArea.textProperty().set("登录服务器时发生错误:" + ex);
messageTextArea.textProperty().set("请求服务器时发生错误:" + ex);
}
});
@ -126,16 +121,10 @@ class UI {
logoutButton.setDisable(true);
logoutButton.setOnAction(e -> {
try {
this.auth(Constant.OPCODE_LOGOUT);
logoutButton.setDisable(true);
loginButton.setDisable(false);
serverTextField.setDisable(false);
usernameTextField.setDisable(false);
messageControlVBox.setDisable(true);
this.changeLoginStatus(false);
} catch (Exception ex) {
ex.printStackTrace();
messageTextArea.textProperty().set("登录服务器时发生错误:" + ex);
messageTextArea.textProperty().set(messageTextArea.getText() + "请求服务器时发生错误:" + ex);
}
});
@ -144,29 +133,21 @@ class UI {
return userControlVBox;
}
private void auth(byte[] opcode) throws Exception {
String[] address = serverTextField.getText().split(":");
int port = Integer.parseInt(address[1]);
ClientConnection connection = new ClientConnection(address[0], port);
private void changeLoginStatus(boolean login) throws IOException {
byte[] data = usernameTextField.getText().getBytes(StandardCharsets.UTF_8);
OutputStream outputStream = connection.getOutputStream();
Common.send(data, opcode, outputStream);
connection.finishOutput();
InputStream inputStream = connection.getInputStream();
byte[] header = new byte[4];
inputStream.read(header);
DataPack responseDataPack = this.sendData(data, login ? Constant.OPCODE_LOGIN : Constant.OPCODE_LOGOUT);
this.displayResponse(responseDataPack);
DataPack dataPack = Common.readDataPack(inputStream);
MessageData messageData = MessageData.parse(dataPack.getData());
messageTextArea.textProperty().set(messageTextArea.getText() +
String.format("\n服务器[%s]响应:%s", messageData.getIdText(), messageData.getMessageText()));
connection.finishInput();
this.switchStatus(login);
}
connection.disconnect();
private void switchStatus(boolean online) {
logoutButton.setDisable(!online);
loginButton.setDisable(online);
serverTextField.setDisable(online);
usernameTextField.setDisable(online);
messageControlVBox.setDisable(!online);
}
private HBox messageControlPane() {
@ -182,14 +163,33 @@ class UI {
sendButton = new Button("发送");
sendButton.setOnAction(e -> {
try {
MessageData messageData = new MessageData(usernameTextField.getText(), messageTextField.getText());
DataPack responseDataPack = this.sendData(messageData.toBytes(), Constant.OPCODE_SEND_MESSAGE);
this.displayResponse(responseDataPack);
} catch (Exception ex) {
ex.printStackTrace();
messageTextArea.textProperty().set(messageTextArea.getText() + "请求服务器时发生错误:" + ex);
}
});
messageControlVBox.getChildren().addAll(usernameLabel, messageTextField, sendButton);
return messageControlVBox;
}
private ClientConnection createConnection() throws IOException {
String[] address = serverTextField.getText().split(":");
int port = Integer.parseInt(address[1]);
try {
ClientConnection connection = new ClientConnection(address[0], port);
MessageData messageData = new MessageData(usernameTextField.getText(), messageTextField.getText());
return new ClientConnection(address[0], port);
}
private DataPack sendData(byte[] data, byte[] opcode) throws IOException {
ClientConnection connection = this.createConnection();
Common.send(messageData.toBytes(), Constant.OPCODE_SEND_MESSAGE, connection.getOutputStream());
Common.write(data, opcode, connection.getOutputStream());
connection.finishOutput();
InputStream inputStream = connection.getInputStream();
@ -197,20 +197,16 @@ class UI {
inputStream.read(header);
DataPack dataPack = Common.readDataPack(inputStream);
MessageData responseData = MessageData.parse(dataPack.getData());
messageTextArea.textProperty().set(messageTextArea.getText() +
String.format("\n服务器[%s]响应:%s", responseData.getIdText(), responseData.getMessageText()));
connection.finishInput();
connection.disconnect();
} catch (IOException ex) {
ex.printStackTrace();
messageTextArea.textProperty().set("登录服务器时发生错误:" + ex);
return dataPack;
}
});
messageControlVBox.getChildren().addAll(usernameLabel, messageTextField, sendButton);
private void displayResponse(DataPack dataPack) {
MessageData messageData = MessageData.parse(dataPack.getData());
return messageControlVBox;
messageTextArea.textProperty().set(messageTextArea.getText() +
String.format("\n服务器[%s]响应:%s", messageData.getIdText(), messageData.getMessageText()));
}
}

@ -1,4 +0,0 @@
package net.lensfrex.socket.client;
public class Global {
}

@ -1,7 +0,0 @@
package net.lensfrex.socket.client;
import javafx.scene.layout.Pane;
public class MainUI extends Pane {
}

@ -3,7 +3,6 @@ package net.lensfrex.socket.common;
import java.io.IOException;
import java.io.InputStream;
import java.io.OutputStream;
import java.nio.charset.StandardCharsets;
public class Common {
public static DataPack readDataPack(InputStream inputStream) {
@ -14,27 +13,27 @@ public class Common {
return DataPack.parse(dataPackBytes);
}
public static int sendMessage(String id, String message, OutputStream outputStream) throws IOException {
public static int writeMessage(String id, String message, OutputStream outputStream) throws IOException {
MessageData messageData = new MessageData(id, message);
return sendMessage(messageData, outputStream);
return writeMessage(messageData, outputStream);
}
public static int sendMessage(MessageData message, OutputStream outputStream) throws IOException {
return sendMessage(message.toBytes(), outputStream);
public static int writeMessage(MessageData message, OutputStream outputStream) throws IOException {
return writeMessage(message.toBytes(), outputStream);
}
public static int sendMessage(byte[] data, OutputStream outputStream) throws IOException {
return send(data, Constant.OPCODE_SEND_MESSAGE, outputStream);
public static int writeMessage(byte[] data, OutputStream outputStream) throws IOException {
return write(data, Constant.OPCODE_SEND_MESSAGE, outputStream);
}
public static int send(byte[] data, byte[] opcode, OutputStream outputStream) throws IOException {
public static int write(byte[] data, byte[] opcode, OutputStream outputStream) throws IOException {
DataPack dataPack = new DataPack(opcode, data);
return send(dataPack, outputStream);
return write(dataPack, outputStream);
}
public static int send(DataPack dataPack, OutputStream outputStream) throws IOException {
public static int write(DataPack dataPack, OutputStream outputStream) throws IOException {
outputStream.write(Constant.MAGIC_HEADER);
outputStream.write(dataPack.toBytes());

@ -63,17 +63,17 @@ class ServerThread extends Thread {
if (Arrays.equals(Constant.OPCODE_LOGIN, opcode)) {
this.login(new String(dataPack.getData()));
Common.sendMessage(SERVER_ID, Constant.MESSAGE_OK, outputStream);
Common.writeMessage(SERVER_ID, Constant.MESSAGE_OK, outputStream);
} else if (Arrays.equals(Constant.OPCODE_LOGOUT, opcode)) {
this.logout(new String(dataPack.getData()));
Common.sendMessage(SERVER_ID, Constant.MESSAGE_OK, outputStream);
Common.writeMessage(SERVER_ID, Constant.MESSAGE_OK, outputStream);
} else if (Arrays.equals(Constant.OPCODE_REQUEST_MESSAGE, opcode)) {
this.sendRandomMessage(outputStream);
} else if (Arrays.equals(Constant.OPCODE_SEND_MESSAGE, opcode)) {
String receivedMessage = this.receiveMessage(dataPack);
String responseMessage = String.format("Receive message '%s'", receivedMessage);
Common.sendMessage(SERVER_ID, responseMessage, outputStream);
Common.writeMessage(SERVER_ID, responseMessage, outputStream);
}
}
@ -94,7 +94,7 @@ class ServerThread extends Thread {
}
private void sendMessage(String messageText, OutputStream outputStream) throws IOException {
Common.sendMessage(SERVER_ID, messageText, outputStream);
Common.writeMessage(SERVER_ID, messageText, outputStream);
}
private String receiveMessage(DataPack dataPack) {

Loading…
Cancel
Save