netty client 2023年2月27日14:45:16
parent
d21890a5f8
commit
8d8b61fbaf
|
|
@ -1,36 +1,20 @@
|
|||
package com.zhangmeng.tools.controller;
|
||||
|
||||
import com.zhangmeng.tools.netty.Message;
|
||||
import com.zhangmeng.tools.netty.NettyClient;
|
||||
import com.zhangmeng.tools.utils.AlertUtils;
|
||||
import com.zhangmeng.tools.utils.ExecutorUtils;
|
||||
import com.zhangmeng.tools.utils.Multithreading;
|
||||
import io.netty.bootstrap.Bootstrap;
|
||||
import io.netty.channel.*;
|
||||
import io.netty.channel.nio.NioEventLoopGroup;
|
||||
import io.netty.channel.socket.SocketChannel;
|
||||
import io.netty.channel.socket.nio.NioSocketChannel;
|
||||
import io.netty.handler.codec.string.StringEncoder;
|
||||
import javafx.application.Platform;
|
||||
import javafx.beans.property.SimpleObjectProperty;
|
||||
import javafx.beans.value.ChangeListener;
|
||||
import javafx.beans.value.ObservableValue;
|
||||
import javafx.collections.FXCollections;
|
||||
import javafx.collections.ListChangeListener;
|
||||
import javafx.collections.ObservableList;
|
||||
import javafx.fxml.FXML;
|
||||
import javafx.geometry.Pos;
|
||||
import javafx.scene.control.*;
|
||||
import javafx.scene.control.cell.TextFieldListCell;
|
||||
import javafx.scene.layout.HBox;
|
||||
import javafx.util.Callback;
|
||||
import javafx.util.StringConverter;
|
||||
import lombok.extern.slf4j.Slf4j;
|
||||
|
||||
import java.util.ArrayList;
|
||||
import java.util.Date;
|
||||
import java.util.List;
|
||||
import java.util.Map;
|
||||
|
||||
|
||||
/**
|
||||
* @author : 芊芊墨客
|
||||
|
|
@ -124,6 +108,8 @@ public class NettyClientController {
|
|||
@FXML
|
||||
public void initialize() {
|
||||
message_result_list.addListener((ListChangeListener<String>) c -> {
|
||||
|
||||
while (c.next()) {
|
||||
if (c.wasAdded()) {
|
||||
StringBuilder stringBuilder = new StringBuilder();
|
||||
for (String s : message_result_list) {
|
||||
|
|
@ -131,6 +117,7 @@ public class NettyClientController {
|
|||
}
|
||||
receive(stringBuilder.toString());
|
||||
}
|
||||
}
|
||||
});
|
||||
|
||||
param_list.setPlaceholder(new Label("没有数据"));
|
||||
|
|
@ -186,34 +173,35 @@ public class NettyClientController {
|
|||
|
||||
String msg = send_msg.getText();
|
||||
|
||||
Multithreading.execute(() -> {
|
||||
if (nettyClient == null){
|
||||
this.nettyClient = NettyClient.getInstance();
|
||||
this.nettyClient.setPort(socket_port);
|
||||
this.nettyClient.setIp(socket_address);
|
||||
StringBuilder params = new StringBuilder();
|
||||
for (Data data : list) {
|
||||
if (params.toString().equals("")){
|
||||
params.append(data.key).append("=").append(data.value);
|
||||
}else {
|
||||
params.append("&").append(data.key).append("=").append(data.value);
|
||||
}
|
||||
}
|
||||
this.nettyClient.setParams(params.toString());
|
||||
log.info("params:{}",params.toString());
|
||||
}
|
||||
|
||||
// 让客户端后台懒启动。避免先于服务端启动而引发异常
|
||||
if (!isStarted) {
|
||||
this.nettyClient.start();
|
||||
isStarted = true;
|
||||
}
|
||||
|
||||
Platform.runLater(() -> {
|
||||
this.nettyClient.send(msg);
|
||||
});
|
||||
// Multithreading.execute(() -> {
|
||||
// if (nettyClient == null){
|
||||
// this.nettyClient = NettyClient.getInstance();
|
||||
// this.nettyClient.setPort(socket_port);
|
||||
// this.nettyClient.setIp(socket_address);
|
||||
// StringBuilder params = new StringBuilder();
|
||||
// for (Data data : list) {
|
||||
// if (params.toString().equals("")){
|
||||
// params.append(data.key).append("=").append(data.value);
|
||||
// }else {
|
||||
// params.append("&").append(data.key).append("=").append(data.value);
|
||||
// }
|
||||
// }
|
||||
// this.nettyClient.setParams(params.toString());
|
||||
// log.info("params:{}",params.toString());
|
||||
// }
|
||||
//
|
||||
// // 让客户端后台懒启动。避免先于服务端启动而引发异常
|
||||
// if (!isStarted) {
|
||||
// this.nettyClient.start();
|
||||
// isStarted = true;
|
||||
// }
|
||||
//
|
||||
// Platform.runLater(() -> {
|
||||
// this.nettyClient.send(msg);
|
||||
// });
|
||||
// add_message(msg);
|
||||
// });
|
||||
add_message(msg);
|
||||
});
|
||||
|
||||
this.send_msg.requestFocus();
|
||||
this.send_msg.clear(); // TODO
|
||||
|
|
@ -223,7 +211,7 @@ public class NettyClientController {
|
|||
|
||||
public void receive(String msg) {
|
||||
Platform.runLater(() -> {
|
||||
this.result_show.appendText(msg);
|
||||
this.result_show.setText(msg);
|
||||
});
|
||||
|
||||
}
|
||||
|
|
|
|||
Loading…
Reference in New Issue