netty同时支持socket和http
2024-10-01 06:09:02
项目需要使用netty做中转服务器,同时支持两种不同协议的客户端,经过几天查询资料终于找到合适的方案了,同时感谢Netty权威指南及论坛问答,开始贴代码
客户端1==》socket
public class Bluetooth implements Runnable {
//蓝牙 private int port;
@Override
public void run() {
System.out.println("--------进入蓝牙---------");
EventLoopGroup bossGroup = new NioEventLoopGroup();
EventLoopGroup workGroup = new NioEventLoopGroup();
try {
ServerBootstrap b = new ServerBootstrap();
b.group(bossGroup, workGroup);
b.channel(NioServerSocketChannel.class);
b.childHandler(new ChannelInitializer<SocketChannel>() {
@Override
public void initChannel(SocketChannel ch) throws Exception {
System.out.println("chhhh"+ch.id());
// 注册handler
/*ch.pipeline().addLast("http-codec", new HttpServerCodec());
ch.pipeline().addLast("aggregator", new HttpObjectAggregator(65536));
ch.pipeline().addLast("http-chunked", new ChunkedWriteHandler());*/
ch.pipeline().addLast(new SimpleServerHandler());
}
});
// b.childHandler(new ChannelFilter());
System.out.println("平台监听开启....");
Channel ch = b.bind(5500).sync().channel();
ch.closeFuture().sync(); } catch (Exception e) {
e.printStackTrace();
}finally{
//优雅的退出程序
bossGroup.shutdownGracefully();
workGroup.shutdownGracefully();
}
}
客户端2==》http
public class Myweb implements Runnable {
//Myweb private int port;
@Override
public void run() {
System.out.println("--------进入web---------");
EventLoopGroup bossGroup = new NioEventLoopGroup();
EventLoopGroup workGroup = new NioEventLoopGroup();
try {
ServerBootstrap b = new ServerBootstrap();
b.group(bossGroup, workGroup);
b.channel(NioServerSocketChannel.class);
b.childHandler(new ChannelInitializer<SocketChannel>() {
@Override
public void initChannel(SocketChannel e) throws Exception {
System.out.println("chhhh"+e.id());
e.pipeline().addLast("http-codec", new HttpServerCodec());
e.pipeline().addLast("aggregator", new HttpObjectAggregator(65536));
e.pipeline().addLast("http-chunked", new ChunkedWriteHandler());
e.pipeline().addLast(new SimpleServerHandler());
}
});
// b.childHandler(new ChannelFilter());
System.out.println("平台监听开启....");
Channel ch = b.bind(8888).sync().channel();
ch.closeFuture().sync(); } catch (Exception e) {
e.printStackTrace();
}finally{
//优雅的退出程序
bossGroup.shutdownGracefully();
workGroup.shutdownGracefully();
}
}
}
main==>开启两个监听线程
public class main {
private int port; public main(int port) {
this.port = port;
} public void run() throws Exception { } public static void main(String[] args) throws Exception {
// new main(5500).run();
Bluetooth bluetooth = new Bluetooth();
Myweb myweb = new Myweb(); Thread th1 = new Thread(bluetooth);
Thread th2 = new Thread(myweb);
th1.start();
th2.start(); }
}
Handler代码就不贴了,网上很多,主要是通过多线程分别使用不同编解码器,
对不同客户端的协议进行解析。同时将Chnnel通道保存在Map集合中,两个线程可共享这个Chnnel。 参考:https://blog.csdn.net/lmianhuatang/article/details/79675790
最新文章
- KMP算法实现
- About SQLite
- SQLite 函数大全
- 还记得高中的向量吗?leetcode 335. Self Crossing(判断线段相交)
- App开发的过程
- C++ Qt 框架静态编译 操作记录
- Java基础知识强化101:Java 中的 String对象真的不可变吗 ?
- Effective C++ 笔记二 构造/析构/赋值运算
- linux下的webserver BOA及CGIC库的使用指南(转帖)
- 【转】在写一个iOS应用之前必须做的7件事(附相关资源)
- 将网站固定到开始菜单,自定义图标、颜色和Windows推送通知
- 【APP测试初体验】android测试命令----压力测试
- 用vue官方提供的模板vue-cli搭建一个helloWorld案例
- table给tbody设置滚动条
- HDU 2176 取(m堆)石子游戏 (尼姆博奕)
- sencha touch datepicker/datepickerfield(时间选择控件)扩展(废弃 仅参考)
- IntelliJ IDEA部署tomcat时Edit Configuration无artifact选项
- PLSQL_性能优化索引Index介绍(概念)
- FromBottomToTop团队项目总结
- Weak References