0

In Netty, send messages are like this:

channel.writeAndFlush(new TextWebSocketFrame("Operation Succeed"));

to run this code, nothing abnormal. so I think, argument from writeAndFlush can be extracted to a static and final variable like this:

public class LogicHandler extends SimpleUserEventChannelHandler<WebSocketServerProtocolHandler.HandshakeComplete> {
    static final TextWebSocketFrame SUCCEED = new TextWebSocketFrame("Operation Succeed");

    @Override
    protected void eventReceived(ChannelHandlerContext context, WebSocketServerProtocolHandler.HandshakeComplete arg) {
        context.channel().writeAndFlush(SUCCEED.retain()); // the client received empty content
    } 
}

to run this codes, abnormal things happen:

the client(WebSocket from Chrome browser) received the message but empty content



Are there any ideas that i can do with it?

remove the method "retain" can not solve the problem, the only way is to remove static and final keyword.

2
  • Then why not remove static/final if that works? Not sure why you use retain, this comment gives some explanation: stackoverflow.com/a/52884904/18703 Commented Dec 9, 2022 at 7:39
  • take more usage and be more effective. i also tried ReferenceCountUtil.retain(message) but no use. retain method just change the internal value of its self, the declaration seems no problem. there's a lot of scenes can be done like this. Commented Dec 9, 2022 at 12:58

1 Answer 1

0

Because the underlying ByteBuf's readerIndex is updated once used. You can reuse it by reset the readerIndex, like below. But this code is thread-unsafe, it can cause problems when there are multiple connections.

public static class LogicHandler extends SimpleUserEventChannelHandler<WebSocketServerProtocolHandler.HandshakeComplete> {
    static final TextWebSocketFrame SUCCEED = new TextWebSocketFrame("Operation Succeed");

    @Override
    protected void eventReceived(ChannelHandlerContext context, WebSocketServerProtocolHandler.HandshakeComplete arg) {
        SUCCEED.content().readerIndex(0);
        context.channel().writeAndFlush(SUCCEED.retain());
    }
}

This is thread-safe

public static class LogicHandler extends SimpleUserEventChannelHandler<WebSocketServerProtocolHandler.HandshakeComplete> {
    static final TextWebSocketFrame SUCCEED = new TextWebSocketFrame("Operation Succeed");

    @Override
    protected void eventReceived(ChannelHandlerContext context, WebSocketServerProtocolHandler.HandshakeComplete arg) {
        context.channel().writeAndFlush(SUCCEED.retainedDuplicate());
    }
}
Sign up to request clarification or add additional context in comments.

1 Comment

thx. retainedDuplicate does not work either. still it returns empty string, but readerIndex(0) real worked but thread-unsafe. i am trying to solve this

Your Answer

By clicking “Post Your Answer”, you agree to our terms of service and acknowledge you have read our privacy policy.

Start asking to get answers

Find the answer to your question by asking.

Ask question

Explore related questions

See similar questions with these tags.