2023年2月25日17:47:13

master
zhangmeng 2023-02-25 18:11:01 +08:00
parent 46485cc46e
commit 7321e83f27
1 changed files with 12 additions and 12 deletions

View File

@ -58,7 +58,7 @@ public class NettyClientController {
@FXML
private Button connect;
private final SimpleObjectProperty<ChannelHandlerContext> client = new SimpleObjectProperty<>();
private final SimpleObjectProperty<Channel> client = new SimpleObjectProperty<>();
private ObservableList<Data> list = FXCollections.observableArrayList();
@ -98,8 +98,6 @@ public class NettyClientController {
@FXML
public void initialize() {
param_list.setPlaceholder(new Label("没有数据"));
param_list.setEditable(true);
param_list.setCellFactory(new Callback<>() {
@ -168,18 +166,13 @@ public class NettyClientController {
}
send.setOnAction(event -> {
ChannelHandlerContext channel = client.getValue();
Channel channel = client.getValue();
if (channel == null){
AlertUtils.alert_warning("请连接至服务器再试!");
return;
}
channel.writeAndFlush(result_show.getText());
});
if (client.getValue() == null){
new Thread(()->{
netty_client(url_address.getText(),Integer.parseInt(port.getText()));
}).start();
}
}
private void netty_client(String socket_address,int socket_port) {
@ -196,23 +189,30 @@ public class NettyClientController {
// 接收响应消息
@Override
public void channelRead(ChannelHandlerContext ctx, Object msg) throws Exception {
log.debug("msg: {}", msg);
log.debug("msg: {}...............", msg);
}
// 在连接建立后触发 active 事件
@Override
public void channelActive(ChannelHandlerContext ctx) throws Exception {
NettyClientController.this.client.setValue(ctx);
log.info("channelActive.............");
}
@Override
public void exceptionCaught(ChannelHandlerContext ctx, Throwable cause) throws Exception {
log.info("exceptionCaught");
log.info("exceptionCaught...................");
}
@Override
public void channelInactive(ChannelHandlerContext ctx) throws Exception {
log.info("channelInactive...........");
}
});
}
});
Channel channel = bootstrap.connect(socket_address, socket_port).sync().channel();
client.setValue(channel);
channel.writeAndFlush("1111111111");
channel.closeFuture().sync();
} catch (Exception e) {
log.error("client error", e);