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