码农知识堂 - 1000bd
  •   Python
  •   PHP
  •   JS/TS
  •   JAVA
  •   C/C++
  •   C#
  •   GO
  •   Kotlin
  •   Swift
  • Spring Boot + Netty + WebSocket 实现消息推送


    关于Netty

    Netty 是一个利用 Java 的高级网络的能力,隐藏其背后的复杂性而提供一个易于使用的 API 的客户端/服务器框架。

    2

    Maven依赖

    1. <dependencies>
    2. <!-- https://mvnrepository.com/artifact/io.netty/netty-all -->
    3. <dependency>
    4. <groupId>io.netty</groupId>
    5. <artifactId>netty-all</artifactId>
    6. <version>4.1.36.Final</version>
    7. </dependency>
    8. </dependencies>

    3

    SpringBootApplication

    启动器中需要new一个NettyServer,并显式调用启动netty。

    1. @SpringBootApplication
    2. public class SpringCloudStudyDemoApplication {
    3. public static void main(String[] args) {
    4. SpringApplication.run(SpringCloudStudyDemoApplication.class,args);
    5. try {
    6. new NettyServer(12345).start();
    7. System.out.println("https://blog.csdn.net/moshowgame");
    8. System.out.println("http://127.0.0.1:6688/netty-websocket/index");
    9. }catch(Exception e) {
    10. System.out.println("NettyServerError:"+e.getMessage());
    11. }
    12. }
    13. }

    4

    NettyServer

    启动的NettyServer,这里进行配置

    1. /**
    2. * NettyServer Netty服务器配置
    3. */
    4. public class NettyServer {
    5. private final int port;
    6. public NettyServer(int port) {
    7. this.port = port;
    8. }
    9. public void start() throws Exception {
    10. EventLoopGroup bossGroup = new NioEventLoopGroup();
    11. EventLoopGroup group = new NioEventLoopGroup();
    12. try {
    13. ServerBootstrap sb = new ServerBootstrap();
    14. sb.option(ChannelOption.SO_BACKLOG, 1024);
    15. sb.group(group, bossGroup) // 绑定线程池
    16. .channel(NioServerSocketChannel.class) // 指定使用的channel
    17. .localAddress(this.port)// 绑定监听端口
    18. .childHandler(new ChannelInitializer<SocketChannel>() { // 绑定客户端连接时候触发操作
    19. @Override
    20. protected void initChannel(SocketChannel ch) throws Exception {
    21. System.out.println("收到新连接");
    22. //websocket协议本身是基于http协议的,所以这边也要使用http解编码器
    23. ch.pipeline().addLast(new HttpServerCodec());
    24. //以块的方式来写的处理器
    25. ch.pipeline().addLast(new ChunkedWriteHandler());
    26. ch.pipeline().addLast(new HttpObjectAggregator(8192));
    27. ch.pipeline().addLast(new WebSocketServerProtocolHandler("/ws", null, true, 65536 * 10));
  • 相关阅读:
    vue3发送验证码倒计时 (防止连点、封装复用)
    优炫数据库获“2022能源企业信息化产品技术创新”案例
    Pr:基本图形(对齐并变换)
    AutoCAD2014第一章 快速入门
    PCL 源码分析:ICP点云精配准
    ERR_PNPM_NO_GLOBAL_BIN_DIR Unable to find the global bin directory
    window 服务器 短文件名漏洞处理
    【iOS】暑假第二周——网易云APP 仿写
    高等数值计算方法学习笔记第4章第二部分【数值积分(数值微分)】
    指定列求和或计数,不会受选择鼠标选择区域影响
  • 原文地址:https://blog.csdn.net/Q54665642ljf/article/details/126616926
  • 最新文章
  • 攻防演习之三天拿下官网站群
    数据安全治理学习——前期安全规划和安全管理体系建设
    企业安全 | 企业内一次钓鱼演练准备过程
    内网渗透测试 | Kerberos协议及其部分攻击手法
    0day的产生 | 不懂代码的"代码审计"
    安装scrcpy-client模块av模块异常,环境问题解决方案
    leetcode hot100【LeetCode 279. 完全平方数】java实现
    OpenWrt下安装Mosquitto
    AnatoMask论文汇总
    【AI日记】24.11.01 LangChain、openai api和github copilot
  • 热门文章
  • 十款代码表白小特效 一个比一个浪漫 赶紧收藏起来吧!!!
    奉劝各位学弟学妹们,该打造你的技术影响力了!
    五年了,我在 CSDN 的两个一百万。
    Java俄罗斯方块,老程序员花了一个周末,连接中学年代!
    面试官都震惊,你这网络基础可以啊!
    你真的会用百度吗?我不信 — 那些不为人知的搜索引擎语法
    心情不好的时候,用 Python 画棵樱花树送给自己吧
    通宵一晚做出来的一款类似CS的第一人称射击游戏Demo!原来做游戏也不是很难,连憨憨学妹都学会了!
    13 万字 C 语言从入门到精通保姆级教程2021 年版
    10行代码集2000张美女图,Python爬虫120例,再上征途
Copyright © 2022 侵权请联系2656653265@qq.com    京ICP备2022015340号-1
正则表达式工具 cron表达式工具 密码生成工具

京公网安备 11010502049817号