How to send messages to clients actively by netty

demand scenario:
Smart Home Gateway (hereinafter referred to as gateway), needs to communicate with netty server (hereinafter referred to as netty), netty and gateway need to maintain a long connection (in other words, both netty and gateway will actively send messages to each other)

problems encountered:
how does netty actively send messages to gateway as a server? I try to use a map to bind the id of the channelSocket to the channelSocket when each gateway connects to the netty (TCP/IP)

    @Override
    public void write(ChannelHandlerContext ctx, Object msg, ChannelPromise promise) throws Exception {
        System.out.println("write handler");
        ctx.writeAndFlush(msg);
    }

but in the end, the client did not receive any message. How can netty actively send a message to the client?

Oct.24,2021

it can be guessed that the server does not have an encoder, so it cannot be sent at all
you can see from the posted code that there are two ways to send a message to the client in the code:

  

I would like to ask if the subject has found a solution, kneel and beg! What if the server cannot join the decoder?

Menu