feat(新增): 🚀 客户端addListener方式发送消息

This commit is contained in:
bunny 2024-05-24 08:58:45 +08:00
parent 8275bdc281
commit 15ace8f636
4 changed files with 59 additions and 2 deletions

View File

@ -10,7 +10,7 @@
</list> </list>
</option> </option>
</component> </component>
<component name="ProjectRootManager" version="2" languageLevel="JDK_21" default="true" project-jdk-name="21" project-jdk-type="JavaSDK"> <component name="ProjectRootManager" version="2" languageLevel="JDK_X" default="true" project-jdk-name="21" project-jdk-type="JavaSDK">
<output url="file://$PROJECT_DIR$/out" /> <output url="file://$PROJECT_DIR$/out" />
</component> </component>
</project> </project>

View File

@ -77,4 +77,16 @@
<version>1.6.14</version> <version>1.6.14</version>
</dependency> </dependency>
</dependencies> </dependencies>
<build>
<plugins>
<plugin>
<groupId>org.apache.maven.plugins</groupId>
<artifactId>maven-compiler-plugin</artifactId>
<configuration>
<source>14</source>
<target>14</target>
</configuration>
</plugin>
</plugins>
</build>
</project> </project>

View File

@ -25,7 +25,7 @@
<properties> <properties>
<maven.compiler.source>22</maven.compiler.source> <maven.compiler.source>22</maven.compiler.source>
<maven.compiler.target>22</maven.compiler.target> <maven.compiler.target>22</maven.compiler.target>
<java.version>21</java.version> <java.version>22</java.version>
<junit.version>3.8.1</junit.version> <junit.version>3.8.1</junit.version>
<mybatis-plus.version>3.5.6</mybatis-plus.version> <mybatis-plus.version>3.5.6</mybatis-plus.version>
<mysql.version>8.0.30</mysql.version> <mysql.version>8.0.30</mysql.version>

View File

@ -0,0 +1,45 @@
package cn.bunny.service.netty.demo3;
import io.netty.bootstrap.Bootstrap;
import io.netty.channel.Channel;
import io.netty.channel.ChannelFuture;
import io.netty.channel.ChannelFutureListener;
import io.netty.channel.ChannelInitializer;
import io.netty.channel.nio.NioEventLoopGroup;
import io.netty.channel.socket.nio.NioSocketChannel;
import io.netty.handler.codec.string.StringEncoder;
import lombok.extern.slf4j.Slf4j;
import java.net.InetSocketAddress;
@Slf4j
public class ChannelClient {
public static void main(String[] args) throws InterruptedException {
ChannelFuture channelFuture = new Bootstrap()
.group(new NioEventLoopGroup())
.channel(NioSocketChannel.class)
.handler(new ChannelInitializer<NioSocketChannel>() {
@Override
protected void initChannel(NioSocketChannel nioSocketChannel) throws Exception {
nioSocketChannel.pipeline().addLast(new StringEncoder());
}
})
.connect(new InetSocketAddress("localhost", 8080));
// 使用 sycn 方法同步处理结果五阻塞当前线程
// channelFuture.sync();
// 五阻塞向下执行获取Channel
// Channel channel = channelFuture.channel();
// channel.writeAndFlush("哈哈哈哈");
// 使用addLietener 方法异步处理结果
channelFuture.addListener(new ChannelFutureListener() {
@Override
// 在nio线程连接建立好之后会调用aperationComplate
public void operationComplete(ChannelFuture channelFuture) throws Exception {
Channel channel = channelFuture.channel();
log.debug("当前的 Channel: {}", channel);
channel.writeAndFlush("使用监听方法");
}
});
}
}