From 8a340545d76af29dd85d4be2812353294d236011 Mon Sep 17 00:00:00 2001 From: whycq <10027870+whycq@user.noreply.gitee.com> Date: 星期三, 14 八月 2024 12:36:51 +0800 Subject: [PATCH] # --- app/src/main/java/com/example/agvcontroller/socket/NettyServerHandler.java | 27 ++++++++++++++------------- 1 files changed, 14 insertions(+), 13 deletions(-) diff --git a/app/src/main/java/com/example/agvcontroller/socket/NettyServerHandler.java b/app/src/main/java/com/example/agvcontroller/socket/NettyServerHandler.java index 13abdea..73271c0 100644 --- a/app/src/main/java/com/example/agvcontroller/socket/NettyServerHandler.java +++ b/app/src/main/java/com/example/agvcontroller/socket/NettyServerHandler.java @@ -3,11 +3,21 @@ import java.nio.charset.StandardCharsets; import io.netty.buffer.ByteBuf; +import io.netty.buffer.ByteBufUtil; import io.netty.buffer.Unpooled; import io.netty.channel.Channel; import io.netty.channel.ChannelHandlerContext; import io.netty.channel.ChannelInboundHandlerAdapter; import android.util.Log; + +import com.example.agvcontroller.Item; + + +import org.greenrobot.eventbus.EventBus; +import org.greenrobot.eventbus.Subscribe; +import org.greenrobot.eventbus.ThreadMode; + +import java.util.Arrays; import java.util.concurrent.ConcurrentHashMap; public class NettyServerHandler extends ChannelInboundHandlerAdapter { @@ -19,6 +29,7 @@ public void channelActive(ChannelHandlerContext ctx) throws Exception { String clientId = ctx.channel().remoteAddress().toString(); channelMap.put(clientId, ctx.channel()); + EventBus.getDefault().post(new Item("1",clientId,"3")); Log.d(TAG, "Client connected: " + clientId); } @@ -26,6 +37,7 @@ public void channelInactive(ChannelHandlerContext ctx) throws Exception { String clientId = ctx.channel().remoteAddress().toString(); channelMap.remove(clientId); + EventBus.getDefault().post(clientId); Log.d(TAG, "Client disconnected: " + clientId); } @@ -33,19 +45,6 @@ public void channelRead(ChannelHandlerContext ctx, Object msg) throws Exception { // 澶勭悊鎺ユ敹鍒扮殑娑堟伅 ByteBuf byteBuf = (ByteBuf) msg; - //byte[] data = new byte[byteBuf.readableBytes()]; - //byteBuf.readBytes(data); - //String received = new String(data, StandardCharsets.UTF_8); - //System.out.println("Received from client: " + received); - // - //// 鍥炲娑堟伅 - ////ByteBuf response = Unpooled.copiedBuffer("Response from server", StandardCharsets.UTF_8); - ////ctx.writeAndFlush(response); - // - //byte[] responseHex = hexStringToByteArray("48656c6c6f20576f726c64"); // "Hello World" in hex - //ByteBuf response = Unpooled.wrappedBuffer(responseHex); - //ctx.writeAndFlush(response); - try { while (byteBuf.isReadable()) { byte[] bytes = new byte[byteBuf.readableBytes()]; @@ -82,6 +81,8 @@ Channel channel = channelMap.get(clientId); if (channel != null && channel.isActive()) { ByteBuf buf = Unpooled.wrappedBuffer(message); + String upperCase = ByteBufUtil.hexDump(buf).toUpperCase(); + Log.d(TAG, "upperCase " + upperCase); channel.writeAndFlush(buf); } else { Log.d(TAG, "Client " + clientId + " is not connected"); -- Gitblit v1.9.1