Netty实现简单聊天室
来源:互联网 发布:方正排版印刷软件 编辑:程序博客网 时间:2024/05/16 15:01
Netty实现简单聊天室
服务端:
public class Server {int port;public Server(int port) throws InterruptedException {this.port = port;start_Server();}public void start_Server() throws InterruptedException {EventLoopGroup boos = new NioEventLoopGroup();EventLoopGroup worker = new NioEventLoopGroup();ServerBootstrap serverBootstrap = new ServerBootstrap();serverBootstrap.group(boos, worker).channel(NioServerSocketChannel.class).childHandler(new ChannelInitializer<Channel>() {@Overrideprotected void initChannel(Channel arg0) throws Exception {arg0.pipeline().addLast(new StringEncoder());arg0.pipeline().addLast(new StringDecoder());arg0.pipeline().addLast(new ServerHandler());}});ChannelFuture channelFuture = serverBootstrap.bind(port).sync();System.out.println("Server.start_Server()");channelFuture.channel().closeFuture().sync();}public static void main(String[] args) throws InterruptedException {Server server = new Server(9978);}}
服务端处理:
public class ServerHandler extends SimpleChannelInboundHandler<String>{/* * 对channel进行管理 */public static final ChannelGroup group = new DefaultChannelGroup( GlobalEventExecutor.INSTANCE);@Overrideprotected void channelRead0(ChannelHandlerContext arg0, String arg1) throws Exception {Channel channel = arg0.channel();for(Channel ch :group) {if (ch==channel) {arg0.writeAndFlush("[你说]:"+arg1+"\n");} else {arg0.writeAndFlush("["+channel.remoteAddress()+"]"+arg1+"\n");}}}/** * 上线 */@Overridepublic void channelActive(ChannelHandlerContext ctx) throws Exception {Channel channel = ctx.channel();System.out.println("["+channel.remoteAddress()+"]"+"线上");}/** * 下线 */@Overridepublic void channelInactive(ChannelHandlerContext ctx) throws Exception {Channel channel = ctx.channel();System.out.println("["+channel.remoteAddress()+"]"+"下线");}@Overridepublic void exceptionCaught(ChannelHandlerContext ctx, Throwable cause) throws Exception {System.out.println( "[" + ctx.channel().remoteAddress() + "]" + "exit the room"); ctx.close().sync();}/** * 当有新的连接的时候进行通知 */@Overridepublic void handlerAdded(ChannelHandlerContext ctx) throws Exception {Channel channel = ctx.channel();for(Channel ch :group) {ctx.writeAndFlush( "[" + channel.remoteAddress() + "] " + "上线了");}group.add(channel);}@Overridepublic void handlerRemoved(ChannelHandlerContext ctx) throws Exception { Channel channel = ctx.channel(); for (Channel ch : group) { ch.writeAndFlush( "[" + channel.remoteAddress() + "] " + "is comming"); } group.remove(channel);}}
客户端:
public class Client {int port;public Client(int port) throws InterruptedException, IOException {this.port = port;connect();}public void connect() throws InterruptedException, IOException {EventLoopGroup eventLoopGroup = new NioEventLoopGroup();Bootstrap bootstrap = new Bootstrap();bootstrap.group(eventLoopGroup).channel(NioSocketChannel.class).handler(new ChannelInitializer<Channel>() {@Overrideprotected void initChannel(Channel arg0) throws Exception { ChannelPipeline pipeline = arg0.pipeline(); pipeline.addLast("stringD", new StringDecoder()); pipeline.addLast("stringC", new StringEncoder()); pipeline.addLast("http", new HttpClientCodec()); pipeline.addLast("chat", new ClientHandler());}});Channel channel = bootstrap.connect("127.0.0.1",port).sync().channel(); while (true) { BufferedReader reader = new BufferedReader( new InputStreamReader(System.in)); String input = reader.readLine(); if (input != null) { if ("quit".equals(input)) { System.exit(1); } channel.writeAndFlush(input); } }}public static void main(String[] args) throws InterruptedException, IOException {Client client = new Client(9978);}}
客户处理:
public class ClientHandler extends SimpleChannelInboundHandler<String>{@Overrideprotected void channelRead0(ChannelHandlerContext arg0, String arg1) throws Exception {System.out.println(arg1);}}
阅读全文
0 0
- Netty实现简单聊天室
- netty实现简单聊天室
- Netty结合WebSocket实现简单网页聊天室
- WebSocket+Netty实现聊天室
- 基于Netty实现的简单聊天室(GUI版)
- Spring Boot实战之netty-socketio实现简单聊天室(给指定用户推送消息)
- Spring Boot实战之netty-socketio实现简单聊天室(给指定用户推送消息)
- 简单网络聊天室实现
- android实现简单聊天室
- 简单聊天室的实现
- 简单聊天室实现
- Socket实现简单聊天室
- Python实现简单聊天室
- java实现简单聊天室
- php聊天室简单实现
- netty 简单httpserver实现
- netty的简单实现
- Netty实现简单RPC
- 内核printk的用法
- 烧写uboot
- 刘强东表态后 京东宣布全面接入顺丰
- 案例-----拦截有序广播
- H.264 基础
- Netty实现简单聊天室
- 全局变量和局部变量 案例
- Java基础教程0-测试人员为什么要掌握Java基础
- String常见面试题
- 【HTML5学习笔记】12:尝试CSS层叠样式表和关键帧
- Android笔记一:Activity生命周期
- 多线程-线程控制之中断线程
- hashset
- Activity 生命周期