2023年6月13日14:30:12

master
zhangmeng 2023-06-13 14:30:33 +08:00
parent 81978c4f33
commit 2abdf60f37
9 changed files with 212 additions and 1 deletions

View File

@ -598,4 +598,8 @@ public class HomeController implements Serializable {
public void file_edit_menu_item(ActionEvent event) {
load_small_tools(16);
}
public void web_socket_client_menu_item(ActionEvent event) {
load_server_tools(8);
}
}

View File

@ -47,6 +47,7 @@ public class ServerToolsController {
private AnchorPane socket_client_nio;
private AnchorPane socket_server_aio;
private AnchorPane socket_server_nio;
private AnchorPane web_socket_client;
public static final String color_cell = "#f4f4f4";
@ -308,6 +309,12 @@ public class ServerToolsController {
}
socket_client_nio(flag);
}
if (newValue.getIndex() == 8){
if (web_socket_client != null){
flag = true;
}
web_socket_client(flag);
}
}
});
}
@ -448,6 +455,7 @@ public class ServerToolsController {
case Socket_server_aio -> new Image(ImagePath.path(ImagePath.ImagePathType.SPRING_SECURITY));
case Socket_server_nio -> new Image(ImagePath.path(ImagePath.ImagePathType.SPRING_SECURITY));
case Socket_client_nio -> new Image(ImagePath.path(ImagePath.ImagePathType.SPRING_SECURITY));
case Web_Socket_client -> new Image(ImagePath.path(ImagePath.ImagePathType.SPRING_SECURITY));
};
}
@ -683,8 +691,33 @@ public class ServerToolsController {
}
public void json_javabean_gen_menu_item(ActionEvent event) {
load_sql_tools(2);
}
public void sql_query_gen_menu_item(ActionEvent event) {
load_sql_tools(3);
}
public void web_socket_client_menu_item(ActionEvent event) {
boolean flag = false;
if (web_socket_client != null){
flag =true;
}
web_socket_client(flag);
}
private void web_socket_client(boolean flag) {
listView.getSelectionModel().select(8);
if (!flag){
try {
root = FXMLLoader.load(ResourcesUtils.getResource("websocket-client"));
} catch (IOException e) {
e.printStackTrace();
}
web_socket_client = root;
}else {
root = web_socket_client;
}
common_method();
}
}

View File

@ -0,0 +1,147 @@
package com.zhangmeng.tools.controller;
import com.zhangmeng.tools.utils.AlertUtils;
import javafx.application.Platform;
import javafx.collections.FXCollections;
import javafx.collections.ListChangeListener;
import javafx.collections.ObservableList;
import javafx.fxml.FXML;
import javafx.scene.control.Button;
import javafx.scene.control.TextArea;
import javafx.scene.control.TextField;
import lombok.extern.slf4j.Slf4j;
import org.java_websocket.WebSocket;
import org.java_websocket.client.WebSocketClient;
import org.java_websocket.drafts.Draft_6455;
import org.java_websocket.enums.ReadyState;
import org.java_websocket.handshake.ServerHandshake;
import java.net.URI;
/**
* @author :
* @version : 1.0
* @date : 2023-06-13 10:56
*/
@Slf4j
public class WebSocketClientController {
@FXML
public TextField ip;
@FXML
public TextField port;
@FXML
public Button conn;
@FXML
public Button send;
@FXML
public TextField content;
@FXML
public TextField params;
@FXML
private TextArea show_result;
private WebSocketClient webSocketClient;
public static ObservableList<String> message_list = FXCollections.observableArrayList();
@FXML
public void initialize() {
if (ip.getText().length() == 0){
ip.setText("localhost");
}
if (port.getText().length() == 0){
port.setText("3333");
}
if (params.getText().length() == 0){
params.setText("/websocketchart?userId=1&source=pc");
}
conn.setOnAction(event -> {
getConnection();
});
message_list.addListener((ListChangeListener<String>) c -> {
while (c.next()) {
if (c.wasAdded()) {
StringBuilder stringBuilder = new StringBuilder();
for (String s : message_list) {
stringBuilder.append(s);
}
receive(stringBuilder.toString());
}
}
});
send.setOnAction(event -> {
webSocketClient.send(content.getText());
});
}
public void receive(String msg) {
Platform.runLater(() -> {
this.show_result.setText(msg);
});
}
private void getConnection() {
if (webSocketClient == null) {
webSocketClient(ip.getText(), Integer.parseInt(port.getText()),params.getText());
}else {
if (!webSocketClient.isOpen()){
webSocketClient(ip.getText(), Integer.parseInt(port.getText()),params.getText());
}
}
}
public void webSocketClient(String socket_address, int socket_port,String params) {
String url = "ws://" + socket_address + ":" + socket_port+params;
log.info(url);
try {
webSocketClient = new WebSocketClient(new URI(url), new Draft_6455()) {
//连接服务端时触发
@Override
public void onOpen(ServerHandshake handshakedata) {
add_msg("websocket客户端和服务器连接成功!");
}
//收到服务端消息时触发
@Override
public void onMessage(String message) {
add_msg(message);
}
//和服务端断开连接时触发
@Override
public void onClose(int code, String reason, boolean remote) {
add_msg("websocket客户端退出连接");
}
//连接异常时触发
@Override
public void onError(Exception ex) {
add_msg("websocket客户端和服务器连接发生错误={" + ex.getMessage() + "}");
}
};
webSocketClient.connect();
} catch (Exception e) {
e.printStackTrace();
}
}
public static void add_msg(String msg) {
message_list.add(msg);
message_list.add(System.lineSeparator());
}
}

View File

@ -298,6 +298,7 @@ public class ResourcesUtils {
Socket_server_aio("aio socket server",5),
Socket_server_nio("nio socket server",6),
Socket_client_nio("nio socket client",7),
Web_Socket_client("web socket client",8),
;
ServerTools(String title, int index) {

View File

@ -89,6 +89,7 @@
<MenuItem mnemonicParsing="false" text="socket-server-aio 服务端工具" onAction="#socket_server_aio_menu_item"/>
<MenuItem mnemonicParsing="false" text="socket-server-nio 服务端工具" onAction="#socket_server_nio_menu_item"/>
<MenuItem mnemonicParsing="false" text="socket-client-nio 客户端工具" onAction="#socket_client_nio_menu_item"/>
<MenuItem mnemonicParsing="false" text="web-socket-client 客户端工具" onAction="#web_socket_client_menu_item"/>
</items>
</Menu>

View File

@ -11,7 +11,7 @@
<children>
<ImageView id="bg_iv" fx:id="bg_iv" fitHeight="649.0" fitWidth="1156.0" layoutX="1.0" pickOnBounds="true" AnchorPane.bottomAnchor="0.0" AnchorPane.leftAnchor="0.0" AnchorPane.rightAnchor="0.0" AnchorPane.topAnchor="0.0">
<image>
<Image url="@../static/bgimg/2041075.jpg" />
<Image url="@../static/bgimg/bg_3.jpg" />
</image>
</ImageView>
<Label id="load_info_show" fx:id="load_info_show" layoutX="1052.0" layoutY="595.0" text="加载中......" textFill="#26852f" AnchorPane.bottomAnchor="30.0" AnchorPane.rightAnchor="24.0">

View File

@ -87,6 +87,7 @@
<MenuItem mnemonicParsing="false" text="socket-server-aio 服务端工具" onAction="#socket_server_aio_menu_item"/>
<MenuItem mnemonicParsing="false" text="socket-server-nio 服务端工具" onAction="#socket_server_nio_menu_item"/>
<MenuItem mnemonicParsing="false" text="socket-client-nio 客户端工具" onAction="#socket_client_nio_menu_item"/>
<MenuItem mnemonicParsing="false" text="web-socket-client 客户端工具" onAction="#web_socket_client_menu_item"/>
</items>
</Menu>
</menus>

View File

@ -0,0 +1,24 @@
<?xml version="1.0" encoding="UTF-8"?>
<?import javafx.scene.control.Button?>
<?import javafx.scene.control.Label?>
<?import javafx.scene.control.TextArea?>
<?import javafx.scene.control.TextField?>
<?import javafx.scene.layout.AnchorPane?>
<AnchorPane prefHeight="649.0" prefWidth="1200.0" xmlns="http://javafx.com/javafx/19" xmlns:fx="http://javafx.com/fxml/1" fx:controller="com.zhangmeng.tools.controller.WebSocketClientController">
<children>
<TextField fx:id="ip" layoutX="243.0" layoutY="60.0" prefHeight="25.0" prefWidth="198.0" AnchorPane.topAnchor="60.0" />
<Label layoutX="176.0" layoutY="64.0" text="ip:" AnchorPane.topAnchor="64.0" />
<TextArea fx:id="show_result" layoutX="243.0" layoutY="131.0" prefHeight="316.0" prefWidth="845.0" AnchorPane.bottomAnchor="202.0" AnchorPane.leftAnchor="243.0" AnchorPane.rightAnchor="112.0" AnchorPane.topAnchor="131.0" />
<TextField fx:id="content" layoutX="243.0" layoutY="493.0" prefHeight="25.0" prefWidth="845.0" AnchorPane.bottomAnchor="131.0" />
<Label layoutX="169.0" layoutY="497.0" text="发送内容:" AnchorPane.bottomAnchor="135.0" />
<Button fx:id="send" layoutX="1103.0" layoutY="493.0" mnemonicParsing="false" text="发送" AnchorPane.bottomAnchor="131.0" />
<Label layoutX="169.0" layoutY="133.0" text="内容:" />
<Label layoutX="487.0" layoutY="64.0" text="port:" />
<TextField fx:id="port" layoutX="539.0" layoutY="60.0" />
<Button fx:id="conn" layoutX="1042.0" layoutY="60.0" mnemonicParsing="false" text="连接" />
<Label layoutX="726.0" layoutY="64.0" text="参数:" />
<TextField fx:id="params" layoutX="770.0" layoutY="60.0" prefHeight="25.0" prefWidth="251.0" />
</children>
</AnchorPane>

Binary file not shown.

After

Width:  |  Height:  |  Size: 599 KiB