I only delve into netty, I can make serious mistakes, do not hit with slippers
For the sake of experiment, I wrote chat, server and client. If I write a message in Russian, it will be in Russian for me, but for others it will be krakozyabry. If another person writes a message in Russian, then instead of the text there will be a question (s).
Write / Read Strings from a ByteBuffer
private ByteBuf buffer; public String readString() { int len = readInt(); byte[] bytes = readBytes(len); return new String(bytes); } public void writeBytes(byte[] bytes) { buffer.writeBytes(bytes); } public byte[] readBytes(int len) { byte[] b = new byte[len]; buffer.readBytes(b); return b; } public void writeString(String s) { byte[] bytes = s.getBytes(); writeInt(bytes.length); writeBytes(bytes); } Part of the code from the controller
//chatArea => TextFlow[JavaFX 8] public void appendText(String text, String image) { Platform.runLater(() -> { Text textObj = new Text(text); if(!image.equals("null")) { //image не может быть null, но может быть "null" ImageView view = new ImageView(SwingFXUtils.toFXImage(ImageCache.getCache(image).getImage(), null)); chatArea.getChildren().addAll(textObj, view); } else { chatArea.getChildren().add(textObj); } chatArea.getChildren().add(new Text("\n")); }); } Part of the code from the client
public static void handleChat(String text, String image) { ChatController.getController().appendText(text, image); } public static void handlePacket(Packet packet, Channel channel) { {...} if(packet instanceof OutMessagePacket) { OutMessagePacket messagePacket = (OutMessagePacket) packet; handleChat(messagePacket.getMessage(), messagePacket.getImage()); } } Unfortunately there are no screenshots.
I hope this will be enough.
Update: + more code
Handler client side
public static class ClientHandler extends ChannelInboundHandlerAdapter { @Override public void channelRead(ChannelHandlerContext ctx, Object msg) throws Exception { PacketManager.readPacket(Side.OUT, ctx.channel(), (ByteBuf) msg); } @Override public void exceptionCaught(ChannelHandlerContext ctx, Throwable cause) throws Exception { ctx.close(); } } Handler server side.
public static class ServerHandler extends ChannelInboundHandlerAdapter { @Override public void channelRead(ChannelHandlerContext ctx, Object msg) throws Exception { PacketManager.readPacket(Side.IN, ctx.channel(), (ByteBuf) msg); } @Override public void exceptionCaught(ChannelHandlerContext ctx, Throwable cause) throws Exception { User user = fromChannel(ctx.channel()); if(user != null) { handleDisconnect(users.remove(user.getName())); } cause.printStackTrace(); ctx.close(); } } Packet Reading (PacketManager :: readPacket)
public static void readPacket(Side side, Channel channel, ByteBuf buf) { PacketBuffer packetBuffer = new PacketBuffer(buf); try { int id = packetBuffer.readInt(); Constructor<? extends Packet> packetConstructor = packets.get(id); if(packetConstructor == null) { System.out.println("Unknown packet id:" + id); return; } Packet packet = packetConstructor.newInstance(); packet.read(packetBuffer); switch (side) { case IN: { Server.PacketHandler.handlePacket(packet, channel); break; } case OUT: { Client.PacketHandler.handlePacket(packet, channel); break; } } } catch (InstantiationException | InvocationTargetException | IllegalAccessException e) { e.printStackTrace(); } packetBuffer.getBuffer().release(); }