广告
返回顶部
首页 > 资讯 > 后端开发 > Python >SpringBoot整合Netty实现WebSocket的示例代码
  • 629
分享到

SpringBoot整合Netty实现WebSocket的示例代码

2024-04-02 19:04:59 629人浏览 泡泡鱼

Python 官方文档:入门教程 => 点击学习

摘要

目录一、pom.xml依赖配置二、代码2.1、NettyServer 类2.2、SocketHandler 类2.3、ChannelHandlerPool 类2.4、Applicat

一、pom.xml依赖配置

<!-- netty -->
<dependency>
    <groupId>io.netty</groupId>
    <artifactId>netty-all</artifactId>
    <version>4.1.50.Final</version>
</dependency>

二、代码

2.1、NettyServer 类

package com.wangjing.socket.server;
 
import com.wangjing.socket.handler.CoordinationSocketHandler;
import io.netty.bootstrap.ServerBootstrap;
import io.netty.channel.ChannelFuture;
import io.netty.channel.ChannelInitializer;
import io.netty.channel.ChannelOption;
import io.netty.channel.EventLoopGroup;
import io.netty.channel.NIO.NioEventLoopGroup;
import io.netty.channel.socket.SocketChannel;
import io.netty.channel.socket.nio.NiOServerSocketChannel;
import io.netty.handler.codec.Http.HttpObjectAggregator;
import io.netty.handler.codec.http.httpserverCodec;
import io.netty.handler.codec.http.websocketx.WEBSocketServerProtocolHandler;
import io.netty.handler.stream.ChunkedWriteHandler;
 
public class CoordinationNettyServer {
 
    private final int port;
 
    public CoordinationNettyServer(int port) {
        this.port = port;
    }
 
 
    public void start() throws Exception {
        EventLoopGroup bossGroup = new NioEventLoopGroup();
        EventLoopGroup group = new NioEventLoopGroup();
        try {
            ServerBootstrap sb = new ServerBootstrap();
            sb.option(ChannelOption.SO_BACKLOG, 1024);
            sb.group(group, bossGroup) // 绑定线程池
                    .channel(NioServerSocketChannel.class) // 指定使用的channel
                    .localAddress(this.port)// 绑定监听端口
                    .childHandler(new ChannelInitializer<SocketChannel>() { // 绑定客户端连接时候触发操作
                        @Override
                        protected void initChannel(SocketChannel ch) throws Exception {
                            //websocket协议本身是基于http协议的,所以这边也要使用http解编码器
                            ch.pipeline().addLast(new HttpServerCodec());
                            //以块的方式来写的处理器
                            ch.pipeline().addLast(new ChunkedWriteHandler());
                            ch.pipeline().addLast(new HttpObjectAggregator(8192));
                            ch.pipeline().addLast(new WebSocketServerProtocolHandler("/ws", "WebSocket", true, 65536 * 10));
                            ch.pipeline().addLast(new CoordinationSocketHandler());//自定义消息处理类
                        }
                    });
            ChannelFuture cf = sb.bind().sync(); // 服务器异步创建绑定
            System.out.println(CoordinationNettyServer.class + "已启动,正在监听: " + cf.channel().localAddress());
            cf.channel().closeFuture().sync(); // 关闭服务器通道
        } finally {
            group.shutdownGracefully().sync(); // 释放线程池资源
            bossGroup.shutdownGracefully().sync();
        }
    }
}
 

2.2、SocketHandler 类

package com.wangjing.socket.handler;
 
import com.wangjing.socket.pool.CoordinationChannelHandlerPool;
import io.netty.channel.ChannelHandlerContext;
import io.netty.channel.SimpleChannelInboundHandler;
import io.netty.handler.codec.http.websocketx.TextWebSocketFrame;
 
public class CoordinationSocketHandler extends SimpleChannelInboundHandler<TextWebSocketFrame> {
 
 
    @Override
    public void channelActive(ChannelHandlerContext ctx) throws Exception {
        System.out.println("与客户端建立连接,通道开启!");
        //添加到channelGroup通道组
        CoordinationChannelHandlerPool.channelGroup.add(ctx.channel());
    }
 
    @Override
    public void channelInactive(ChannelHandlerContext ctx) throws Exception {
        System.out.println("与客户端断开连接,通道关闭!");
        //从channelGroup通道组删除
        CoordinationChannelHandlerPool.channelGroup.remove(ctx.channel());
    }
 
    @Override
    protected void channelRead0(ChannelHandlerContext ctx, TextWebSocketFrame msg) throws Exception {
        //接收的消息
        System.out.println(String.fORMat("收到客户端%s的数据:%s", ctx.channel().id(), msg.text()));
 
        // 单独发消息
        // sendMessage(ctx);
        // 群发消息
        sendAllMessage();
    }
 
    private void sendMessage(ChannelHandlerContext ctx) throws InterruptedException {
        String message = "我是服务器,你好呀";
        ctx.writeAndFlush(new TextWebSocketFrame("hello"));
    }
 
    private void sendAllMessage() {
        String message = "我是服务器,这是群发消息";
        CoordinationChannelHandlerPool.channelGroup.writeAndFlush(new TextWebSocketFrame(message));
    }
 
 
}

2.3、ChannelHandlerPool 类

package com.wangjing.socket.pool;
 
import io.netty.channel.group.ChannelGroup;
import io.netty.channel.group.DefaultChannelGroup;
import io.netty.util.concurrent.GlobalEventExecutor;
 
public class CoordinationChannelHandlerPool {
 
    public CoordinationChannelHandlerPool() {
    }
 
    //可以存储userId与ChannelId的映射表
//    public static ConcurrentHashMap<String, ChannelId> channelIdMap = new ConcurrentHashMap<>();
 
    //channelGroup通道组
    public static ChannelGroup channelGroup = new DefaultChannelGroup(GlobalEventExecutor.INSTANCE);
 
}

2.4、Application启动类

package com.wangjing.socket;
 
import com.wangjing.socket.server.CoordinationNettyServer;
import org.springframework.boot.SpringApplication;
import org.springframework.boot.autoconfigure.SpringBootApplication;
 
@SpringBootApplication(scanBasePackages = "com.wangjing")
public class SocketApplication {
 
 
    public static void main(String[] args) {
        SpringApplication.run(SocketApplication.class, args);
 
        try {
            new CoordinationNettyServer(8804).start();
        } catch (Exception e) {
            System.out.println("NettyServerError:" + e.getMessage());
        }
    }
 
}

三、测试

websocket 在线测试推荐:在线websocket测试-online tool-postJSON

到此这篇关于SpringBoot整合Netty实现WebSocket的示例代码的文章就介绍到这了,更多相关SpringBoot整合Netty实现WebSocket内容请搜索编程网以前的文章或继续浏览下面的相关文章希望大家以后多多支持编程网! 

--结束END--

本文标题: SpringBoot整合Netty实现WebSocket的示例代码

本文链接: https://www.lsjlt.com/news/148653.html(转载时请注明来源链接)

有问题或投稿请发送至: 邮箱/279061341@qq.com    QQ/279061341

本篇文章演示代码以及资料文档资料下载

下载Word文档到电脑,方便收藏和打印~

下载Word文档
猜你喜欢
软考高级职称资格查询
编程网,编程工程师的家园,是目前国内优秀的开源技术社区之一,形成了由开源软件库、代码分享、资讯、协作翻译、讨论区和博客等几大频道内容,为IT开发者提供了一个发现、使用、并交流开源技术的平台。
  • 官方手机版

  • 微信公众号

  • 商务合作