package com.linkin.netty.nov.client; import io.netty.bootstrap.Bootstrap; import io.netty.channel.ChannelFuture; import io.netty.channel.ChannelFutureListener; import io.netty.channel.ChannelInitializer; import io.netty.channel.EventLoopGroup; import io.netty.channel.nio.NioEventLoopGroup; import io.netty.channel.socket.SocketChannel; import io.netty.channel.socket.nio.NioSocketChannel; public class Client { public static void main(String[] args) throws InterruptedException { EventLoopGroup group = new NioEventLoopGroup(); Bootstrap clientBootstrap = new Bootstrap(); try { clientBootstrap.group(group) .channel(NioSocketChannel.class) .remoteAddress("localhost", 8888) .handler(new ChannelInitializer<SocketChannel>() { @Override protected void initChannel(SocketChannel ch) throws Exception { ch.pipeline().addLast(new ClientInitHandler()); } }); ChannelFuture f = clientBootstrap.connect().sync(); f.channel().closeFuture().sync(); f.addListener((ChannelFutureListener) future -> { boolean isCancellable = future.isCancellable(); if (isCancellable) { System.out.println("Main ChannelFutureListener isCancellable"); } boolean isCancelled = future.isCancelled(); if (isCancelled) { System.out.println("Main ChannelFutureListener isCancelled"); } boolean isDone = future.isDone(); if (isDone) { System.out.println("Main ChannelFutureListener isDone"); } boolean isSuccess = future.isSuccess(); if (isSuccess) { System.out.println("Main ChannelFutureListener isSuccess"); } boolean isVoid = future.isVoid(); if (isVoid) { System.out.println("Main ChannelFutureListener isVoid"); } }); } catch (InterruptedException e) { e.printStackTrace(); } finally { group.shutdownGracefully().sync(); } } }
package com.linkin.netty.nov.server; import io.netty.bootstrap.ServerBootstrap; import io.netty.channel.ChannelFuture; import io.netty.channel.ChannelInitializer; import io.netty.channel.nio.NioEventLoopGroup; import io.netty.channel.socket.nio.NioServerSocketChannel; import io.netty.channel.socket.nio.NioSocketChannel; public class Server { public static void main(String[] args) { ServerBootstrap serverBootstrap = new ServerBootstrap(); NioEventLoopGroup bossGroup = new NioEventLoopGroup(); NioEventLoopGroup workerGroup = new NioEventLoopGroup(); serverBootstrap.group(bossGroup, workerGroup) .channel(NioServerSocketChannel.class) .localAddress(8888) .childHandler(new ChannelInitializer<NioSocketChannel>() { @Override protected void initChannel(NioSocketChannel ch) throws Exception { System.out.println("---Server init success---"); ch.pipeline().addLast(new ServerInitHandler()); } }); try { ChannelFuture f = serverBootstrap.bind().sync(); f.channel().closeFuture().sync(); } catch (InterruptedException e) { e.printStackTrace(); } finally { workerGroup.shutdownGracefully(); bossGroup.shutdownGracefully(); } } }
package com.linkin.netty.nov.client; import io.netty.buffer.ByteBuf; import io.netty.buffer.Unpooled; import io.netty.channel.Channel; import io.netty.channel.ChannelHandler.Sharable; import io.netty.channel.ChannelHandlerContext; import io.netty.channel.ChannelInboundHandlerAdapter; import io.netty.util.AttributeKey; import io.netty.util.AttributeMap; import io.netty.util.CharsetUtil; @Sharable public class ClientInitHandler extends ChannelInboundHandlerAdapter { @Override public void handlerAdded(ChannelHandlerContext ctx) throws Exception { System.out.println("---Client channel handler added"); } @Override public void channelRegistered(ChannelHandlerContext ctx) throws Exception { System.out.println("---Client channel registered"); } @Override public void channelActive(ChannelHandlerContext ctx) throws Exception { ctx.channel().attr(AttributeKey.<String>valueOf("user")).set("shijie"); System.out.println("---Client channel active"); Channel channel = ctx.channel(); channel.writeAndFlush(Unpooled.copiedBuffer("hello-world", CharsetUtil.UTF_8)); } @Override public void channelReadComplete(ChannelHandlerContext ctx) throws Exception { System.out.println("---Client channel readComplete"); } @Override public void channelInactive(ChannelHandlerContext ctx) throws Exception { System.out.println("---Client channel inactive"); } @Override public void channelUnregistered(ChannelHandlerContext ctx) throws Exception { System.out.println("---Client channel unregistered"); } @Override public void handlerRemoved(ChannelHandlerContext ctx) throws Exception { System.out.println("---Client channel handler removed"); } @Override public void channelRead(ChannelHandlerContext ctx, Object msg) throws Exception { ByteBuf in = (ByteBuf) msg; System.out.println("---Client channel read: " + in.toString(CharsetUtil.UTF_8)); Thread.sleep(2000); ctx.channel().writeAndFlush(Unpooled.copiedBuffer(in)); } @Override public void channelWritabilityChanged(ChannelHandlerContext ctx) throws Exception { System.out.println("---Client channel writeability changed"); } @Override public void exceptionCaught(ChannelHandlerContext ctx, Throwable cause) throws Exception { System.out.println("---Client channel exceptionCaught"); System.out.println(cause.getMessage()); } @Override public void userEventTriggered(ChannelHandlerContext ctx, Object evt) throws Exception { System.out.println("---Client channel userEventTriggerd"); } }
package com.linkin.netty.nov.server; import io.netty.buffer.ByteBuf; import io.netty.buffer.Unpooled; import io.netty.channel.ChannelHandlerContext; import io.netty.channel.ChannelInboundHandlerAdapter; import io.netty.util.Attribute; import io.netty.util.AttributeKey; import io.netty.util.CharsetUtil; public class ServerInitHandler extends ChannelInboundHandlerAdapter { @Override public void channelRead(ChannelHandlerContext ctx, Object msg) throws Exception { Attribute<String> attr = ctx.channel().attr(AttributeKey.<String>valueOf("user")); String object = attr.get(); System.out.println(object); ByteBuf in = (ByteBuf) msg; System.out.println("chennel read Server 收到: " + in.toString(CharsetUtil.UTF_8)); ctx.writeAndFlush(Unpooled.copiedBuffer(in)); } @Override public void channelActive(ChannelHandlerContext ctx) throws Exception { System.out.println("----------Server Channel Active"); } @Override public void channelReadComplete(ChannelHandlerContext ctx) { System.out.println("---------Channel 读取完成---------"); } @Override public void exceptionCaught(ChannelHandlerContext ctx, Throwable cause) { cause.printStackTrace(); ctx.close(); } @Override public void channelInactive(ChannelHandlerContext ctx) throws Exception { System.out.println("server channel status : channelInactive"); super.channelInactive(ctx); } @Override public void channelUnregistered(ChannelHandlerContext ctx) throws Exception { System.out.println("server channel status : channelUnregistered"); super.channelUnregistered(ctx); } @Override public void handlerRemoved(ChannelHandlerContext ctx) throws Exception { System.out.println("server channel status : handlerRemoved"); super.handlerRemoved(ctx); } }
来源:即时通讯网 - 即时通讯开发者社区!
轻量级开源移动端即时通讯框架。
快速入门 / 性能 / 指南 / 提问
轻量级Web端即时通讯框架。
详细介绍 / 精编源码 / 手册教程
移动端实时音视频框架。
详细介绍 / 性能测试 / 安装体验
基于MobileIMSDK的移动IM系统。
详细介绍 / 产品截图 / 安装体验
一套产品级Web端IM系统。
详细介绍 / 产品截图 / 演示视频
引用此评论
引用:JackJiang 发表于 2022-12-01 18:11 你不如直接去读 MobileIMSDK 的服务端代码:https://github.com/JackJiang2011/MobileIMSDK/stargazers ...
引用:shijie7 发表于 2022-12-01 18:25 Client的ChannelHandlerContext跟Server的ChannelHandlerContext不是同一个, 所以Server才没办法获取到Clie ...
精华主题数超过100个。
连续任职达2年以上的合格正式版主
为论区做出突出贡献的开发者、版主等。
Copyright © 2014-2024 即时通讯网 - 即时通讯开发者社区 / 版本 V4.4
苏州网际时代信息科技有限公司 (苏ICP备16005070号-1)
Processed in 0.109375 second(s), 41 queries , Gzip On.