How do downstream events work in jboss netty?

Just started playing with netty while implementing my own server. It took me a while to understand, but now I was able to accept clients by writing my own MessageHandler and inside messageReceived. I was able to read from the buffer and made some business logic related to the received data.

However, now the question arises: how do I write data to connected clients? I saw a sample code where you can write to the channel in case a new message appears:

public void messageReceived(ChannelHandlerContext ctx, MessageEvent e) {
    Channel ch = e.getChannel();
    ch.write(e.getMessage());
}

but what if you do not want to record data at this moment? What should I do if the client remains connected on the socket and waits until an event occurs on the server? In this case, how will my server find the correct socket to write to? Should I keep a link to the channel object? Is this an agreement?

I looked further into the code and saw the writeRequested method. It's related? Who calls that? And is it necessary?

+3
source share
1 answer

As long as you reference the channel (or ChannelHandlerContext), you can call Channel.write () (or Channels.write ()) from anywhere, any stream.

writeRequested () is called when the writeRequested event is fired by calling Channel.write () or calling ChannelHandlerContext.sendDownstream (MessageEvent).

+6

Source: https://habr.com/ru/post/1754073/


All Articles