简体   繁体   中英

Send message to client any moment Netty Io

I have an architecture based on micro services, netty io runs on a spring boot application, when I want to send a message to a client I have to wait for a message from the client to send the reply to the one I want to send. How could I send a message to the client without waiting for him to communicate with me? I'm lost in this step.

You can process like that :

  1. Let the client connect on your server
  2. Let the client send a Hello Message (to be sure is one of your client)
  3. Catch the message on the server and store a reference on the channel (which is available on each channel handler by calling ctx.channel() )
  4. Catch channelInactive event to remove the reference when the client disconnects

Instead of waiting for a HelloMessage, you can also catch the channelActive event but it is fired each time somebody connects to your listened port, even if is not one of your client. That's why I prefer using an "Hello Message"

Now, you have a reference on the channel and you can send a message when you want by calling channel.writeAndFlush()

The technical post webpages of this site follow the CC BY-SA 4.0 protocol. If you need to reprint, please indicate the site URL or the original address.Any question please contact:yoyou2525@163.com.

 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM