栏目分类:
子分类:
返回
名师互学网用户登录
快速导航关闭
当前搜索
当前分类
子分类
实用工具
热门搜索
名师互学网 > IT > 软件开发 > 后端开发 > Java

Netty案例介绍-群聊案例实现

Java 更新时间: 发布时间: IT归档 最新发布 模块sitemap 名妆网 法律咨询 聚返吧 英语巴士网 伯小乐 网商动力

Netty案例介绍-群聊案例实现

import io.netty.bootstrap.ServerBootstrap;

import io.netty.channel.*;

import io.netty.channel.nio.NioEventLoopGroup;

import io.netty.channel.socket.SocketChannel;

import io.netty.channel.socket.nio.NioServerSocketChannel;

import io.netty.handler.codec.string.StringDecoder;

import io.netty.handler.codec.string.StringEncoder;

public class GroupChatServer {

// 服务的端口号

private int port;

public GroupChatServer(int port){

this.port = port;

}

public void run() throws Exception{

// 创建BossGroup和WorkGroup

EventLoopGroup bossGroup = new NioEventLoopGroup(1);

EventLoopGroup workGroup = new NioEventLoopGroup();

// 创建 ServerBootstrap 服务启动对象

ServerBootstrap bootstrap = new ServerBootstrap();

try{

// 给bootstrap设置相关的参数

bootstrap.group(bossGroup,workGroup)

.channel(NioServerSocketChannel.class)

.option(ChannelOption.SO_BACKLOG,128)

.childOption(ChannelOption.SO_KEEPALIVE,true)

.childHandler(new ChannelInitializer() {

@Override

protected void initChannel(SocketChannel sc) throws Exception {

// 获取pipeline对象

ChannelPipeline pipeline = sc.pipeline();

// 设置对应的handler

pipeline.addLast(“docoder”,new StringDecoder());

pipeline.addLast(“encoder”,new StringEncoder());

pipeline.addLast(new GroupChatServerHandler());

}

});

System.out.println(“服务端启动了…”);

// 绑定端口

ChannelFuture future = bootstrap.bind(port).sync();

future.channel().closeFuture().sync();

}finally {

bossGroup.shutdownGracefully();

workGroup.shutdownGracefully();

}

}

public static void main(String[] args) throws Exception{

GroupChatServer server = new GroupChatServer(8888);

server.run();

}

}

[](()3. 客户端代码


[](()3.1 客户端处理器

获取服务器转发的消息

package com.dpb.netty.goupchat;

import io.netty.channel.ChannelHandlerContext;

import io.netty.channel.SimpleChannelInboundHandler;

public class GroupChatClientHandler extends SimpleChannelInboundHandler {

@Override

protected void channelRead0(ChannelHandlerContext channelHandlerContext, String s) throws Exception {

System.out.println(s.trim());

}

}

[](()3.2 客户端代码

连接服务器,发送消息

package com.dpb.netty.goupchat;

import io.netty.bootstrap.Bootstrap;

import io.netty.channel.Channel;

import io.netty.channel.ChannelFuture;

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;

import io.netty.handler.codec.string.StringDecoder;

import io.netty.handler.codec.string.StringEncoder;

import java.util.Scanner;

public class GroupChatClient {

public String host;

public int port ;

public GroupChatClient(String host,int port){

this.host = host;

this.port = port;

}

public void run() throws Exception{

EventLoopGroup clientGroup = new NioEventLoopGroup(1);

Bootstrap bootstrap = new Bootstrap();

try{

bootstrap.group(clientGroup)

.channel(NioSocketChannel.class)

.handler(new ChannelInitializer() {

@Override

protected void initChannel(SocketChannel sc) throws Exception {

sc.pipeline().addLast(“decoder”,new StringDecoder());

sc.pipelin 《一线大厂Java面试题解析+后端开发学习笔记+最新架构讲解视频+实战项目源码讲义》无偿开源 威信搜索公众号【编程进阶路】 e().addLast(“encoder”,new StringEncoder());

sc.pipeline().addLast(new GroupChatClientHandler());

}

});

ChannelFuture future = bootstrap.connect(host, port).sync();

Channel channel = future.channel();

// 发送消息

Scanner scanner = new Scanner(System.in);

while(scanner.hasNextLine()){

String msg = scanner.nextLine();

channel.writeAndFlush(msg + “n”);

}

}finally {

clientGroup.shutdownGracefully();

}

}

public static void main(String[] args) throws Exception {

GroupChatClient client = new GroupChatClient(“localhost”,8888);

client.run();

}

}

转载请注明:文章转载自 www.mshxw.com
本文地址:https://www.mshxw.com/it/874505.html
我们一直用心在做
关于我们 文章归档 网站地图 联系我们

版权所有 (c)2021-2022 MSHXW.COM

ICP备案号:晋ICP备2021003244-6号