简体   繁体   English

随时向客户发送消息

[英]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. 我有一个基于微服务的体系结构,netty io在spring boot应用程序上运行,当我想向客户端发送消息时,我必须等待客户端的消息才能将答复发送给我要发送的消息。 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) 让客户端发送Hello消息(确保是您的客户端之一)
  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() ) 在服务器上捕获消息并在通道上存储引用(可通过调用ctx.channel()在每个通道处理程序上使用该引用)
  4. Catch channelInactive event to remove the reference when the client disconnects 捕获channelInactive事件以在客户端断开连接时删除引用

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. 除了等待HelloMessage,还可以捕获channelActive事件,但是每次有人连接到您的侦听端口时都会触发该事件,即使它不是您的客户端之一。 That's why I prefer using an "Hello Message" 这就是为什么我更喜欢使用“ Hello Message”

Now, you have a reference on the channel and you can send a message when you want by calling channel.writeAndFlush() 现在,您在通道上有了一个引用,可以通过调用channel.writeAndFlush()在需要时发送消息channel.writeAndFlush()

声明:本站的技术帖子网页,遵循CC BY-SA 4.0协议,如果您需要转载,请注明本站网址或者原文地址。任何问题请咨询:yoyou2525@163.com.

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