|
|
@ -17,7 +17,6 @@ import net.lensfrex.socket.common.MessageData; |
|
|
|
|
|
|
|
|
|
|
|
import java.io.IOException; |
|
|
|
import java.io.IOException; |
|
|
|
import java.io.InputStream; |
|
|
|
import java.io.InputStream; |
|
|
|
import java.io.OutputStream; |
|
|
|
|
|
|
|
import java.net.URL; |
|
|
|
import java.net.URL; |
|
|
|
import java.nio.charset.StandardCharsets; |
|
|
|
import java.nio.charset.StandardCharsets; |
|
|
|
import java.util.Random; |
|
|
|
import java.util.Random; |
|
|
@ -32,9 +31,9 @@ public class Client extends Application { |
|
|
|
Scene scene = new Scene(new UI().buildUI(), 515, 509); |
|
|
|
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("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.setTitle("( ̄﹃ ̄)"); |
|
|
|
stage.setScene(scene); |
|
|
|
stage.setScene(scene); |
|
|
@ -53,9 +52,11 @@ class UI { |
|
|
|
private TextField serverTextField; |
|
|
|
private TextField serverTextField; |
|
|
|
|
|
|
|
|
|
|
|
private TextArea messageTextArea; |
|
|
|
private TextArea messageTextArea; |
|
|
|
|
|
|
|
|
|
|
|
private HBox userControlVBox; |
|
|
|
private HBox userControlVBox; |
|
|
|
private TextField usernameTextField; |
|
|
|
private TextField usernameTextField; |
|
|
|
private Button loginButton, logoutButton; |
|
|
|
private Button loginButton, logoutButton; |
|
|
|
|
|
|
|
|
|
|
|
private HBox messageControlVBox; |
|
|
|
private HBox messageControlVBox; |
|
|
|
private TextField messageTextField; |
|
|
|
private TextField messageTextField; |
|
|
|
private Button sendButton; |
|
|
|
private Button sendButton; |
|
|
@ -109,16 +110,10 @@ class UI { |
|
|
|
loginButton = new Button("登录"); |
|
|
|
loginButton = new Button("登录"); |
|
|
|
loginButton.setOnAction(e -> { |
|
|
|
loginButton.setOnAction(e -> { |
|
|
|
try { |
|
|
|
try { |
|
|
|
this.auth(Constant.OPCODE_LOGIN); |
|
|
|
this.changeLoginStatus(true); |
|
|
|
|
|
|
|
|
|
|
|
logoutButton.setDisable(false); |
|
|
|
|
|
|
|
loginButton.setDisable(true); |
|
|
|
|
|
|
|
serverTextField.setDisable(true); |
|
|
|
|
|
|
|
usernameTextField.setDisable(true); |
|
|
|
|
|
|
|
messageControlVBox.setDisable(false); |
|
|
|
|
|
|
|
} catch (Exception ex) { |
|
|
|
} catch (Exception ex) { |
|
|
|
ex.printStackTrace(); |
|
|
|
ex.printStackTrace(); |
|
|
|
messageTextArea.textProperty().set("登录服务器时发生错误:" + ex); |
|
|
|
messageTextArea.textProperty().set("请求服务器时发生错误:" + ex); |
|
|
|
} |
|
|
|
} |
|
|
|
}); |
|
|
|
}); |
|
|
|
|
|
|
|
|
|
|
@ -126,16 +121,10 @@ class UI { |
|
|
|
logoutButton.setDisable(true); |
|
|
|
logoutButton.setDisable(true); |
|
|
|
logoutButton.setOnAction(e -> { |
|
|
|
logoutButton.setOnAction(e -> { |
|
|
|
try { |
|
|
|
try { |
|
|
|
this.auth(Constant.OPCODE_LOGOUT); |
|
|
|
this.changeLoginStatus(false); |
|
|
|
|
|
|
|
|
|
|
|
logoutButton.setDisable(true); |
|
|
|
|
|
|
|
loginButton.setDisable(false); |
|
|
|
|
|
|
|
serverTextField.setDisable(false); |
|
|
|
|
|
|
|
usernameTextField.setDisable(false); |
|
|
|
|
|
|
|
messageControlVBox.setDisable(true); |
|
|
|
|
|
|
|
} catch (Exception ex) { |
|
|
|
} catch (Exception ex) { |
|
|
|
ex.printStackTrace(); |
|
|
|
ex.printStackTrace(); |
|
|
|
messageTextArea.textProperty().set("登录服务器时发生错误:" + ex); |
|
|
|
messageTextArea.textProperty().set(messageTextArea.getText() + "请求服务器时发生错误:" + ex); |
|
|
|
} |
|
|
|
} |
|
|
|
}); |
|
|
|
}); |
|
|
|
|
|
|
|
|
|
|
@ -144,29 +133,21 @@ class UI { |
|
|
|
return userControlVBox; |
|
|
|
return userControlVBox; |
|
|
|
} |
|
|
|
} |
|
|
|
|
|
|
|
|
|
|
|
private void auth(byte[] opcode) throws Exception { |
|
|
|
private void changeLoginStatus(boolean login) throws IOException { |
|
|
|
String[] address = serverTextField.getText().split(":"); |
|
|
|
|
|
|
|
int port = Integer.parseInt(address[1]); |
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
ClientConnection connection = new ClientConnection(address[0], port); |
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
byte[] data = usernameTextField.getText().getBytes(StandardCharsets.UTF_8); |
|
|
|
byte[] data = usernameTextField.getText().getBytes(StandardCharsets.UTF_8); |
|
|
|
OutputStream outputStream = connection.getOutputStream(); |
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
Common.send(data, opcode, outputStream); |
|
|
|
|
|
|
|
connection.finishOutput(); |
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
InputStream inputStream = connection.getInputStream(); |
|
|
|
DataPack responseDataPack = this.sendData(data, login ? Constant.OPCODE_LOGIN : Constant.OPCODE_LOGOUT); |
|
|
|
byte[] header = new byte[4]; |
|
|
|
this.displayResponse(responseDataPack); |
|
|
|
inputStream.read(header); |
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
DataPack dataPack = Common.readDataPack(inputStream); |
|
|
|
this.switchStatus(login); |
|
|
|
MessageData messageData = MessageData.parse(dataPack.getData()); |
|
|
|
} |
|
|
|
messageTextArea.textProperty().set(messageTextArea.getText() + |
|
|
|
|
|
|
|
String.format("\n服务器[%s]响应:%s", messageData.getIdText(), messageData.getMessageText())); |
|
|
|
|
|
|
|
connection.finishInput(); |
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
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() { |
|
|
|
private HBox messageControlPane() { |
|
|
@ -182,14 +163,33 @@ class UI { |
|
|
|
|
|
|
|
|
|
|
|
sendButton = new Button("发送"); |
|
|
|
sendButton = new Button("发送"); |
|
|
|
sendButton.setOnAction(e -> { |
|
|
|
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(":"); |
|
|
|
String[] address = serverTextField.getText().split(":"); |
|
|
|
int port = Integer.parseInt(address[1]); |
|
|
|
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(); |
|
|
|
connection.finishOutput(); |
|
|
|
|
|
|
|
|
|
|
|
InputStream inputStream = connection.getInputStream(); |
|
|
|
InputStream inputStream = connection.getInputStream(); |
|
|
@ -197,20 +197,16 @@ class UI { |
|
|
|
inputStream.read(header); |
|
|
|
inputStream.read(header); |
|
|
|
|
|
|
|
|
|
|
|
DataPack dataPack = Common.readDataPack(inputStream); |
|
|
|
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.finishInput(); |
|
|
|
|
|
|
|
|
|
|
|
connection.disconnect(); |
|
|
|
connection.disconnect(); |
|
|
|
} catch (IOException ex) { |
|
|
|
return dataPack; |
|
|
|
ex.printStackTrace(); |
|
|
|
|
|
|
|
messageTextArea.textProperty().set("登录服务器时发生错误:" + ex); |
|
|
|
|
|
|
|
} |
|
|
|
} |
|
|
|
}); |
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
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())); |
|
|
|
} |
|
|
|
} |
|
|
|
} |
|
|
|
} |