|
|
|
@ -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())); |
|
|
|
|
} |
|
|
|
|
} |