| | |
| | | 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 android.util.Log; |
| | | |
| | | import com.example.agvcontroller.Item; |
| | | import com.example.agvcontroller.protocol.AgvAction; |
| | | |
| | | |
| | | import org.greenrobot.eventbus.EventBus; |
| | |
| | | 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()]; |
| | |
| | | } |
| | | |
| | | public static void sendMessageToClient(String clientId, byte[] message) { |
| | | |
| | | 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"); |
| | | } |
| | | } |
| | | |
| | | Log.d(TAG, "Client " + clientId + " is not connected"); |
| | | public static void sendMessageToClient(String clientId, AgvAction<?> action) { |
| | | |
| | | |
| | | |
| | | Channel channel = channelMap.get(clientId); |
| | | if (channel != null && channel.isActive()) { |
| | | |
| | | channel.writeAndFlush(action); |
| | | } else { |
| | | Log.d(TAG, "Client " + clientId + " is not connected"); |
| | | } |
| | | } |
| | | |
| | | @Override |