码农知识堂 - 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));
  • 相关阅读:
    查询优化_单表使用索引及常见索引失效
    Week 7 Latent Variable Models and Expectation Maximization
    Git入门到精通(大全)
    this指针和相关的用处——C++
    <wx-open-launch-weapp>微信公众号H5激活微信小程序躺坑日记
    Tomcat内存马学习4:结合反序列化注入
    shell反弹
    专车数据层架构进化往事:好的架构是进化来的,不是设计来的
    2024 年如何成为一名成功的漏洞赏金猎人?成长总结以及相关资料推荐
    C语言中的内存池和内存管理技术是什么?
  • 原文地址:https://blog.csdn.net/Q54665642ljf/article/details/126616926
  • 最新文章
  • 沪漂五周年了:我越来越迷茫了
    Agentic Skill Routing 实战:别再把所有 Skill 塞进 AI Agent 上下文
    MySQL-Seconds_behind_master的精度误差
    [MAF预定义ChatClient中间件-03]CachingChatClient——利用缓存省钱省时间
    AI的至暗历史:从万众期待到被政府撤资,AI的两次死亡徘徊
    Agent OS :五种驯服不确定性的范式
    PortSwigger SQL注入LAB11
    数据库即时编译JIT
    [Begin]AI Learn Data Day 0
    深度学习进阶(二十七)现代 LLM 的核心架构设计其二:SwiGLU
  • 热门文章
  • 十款代码表白小特效 一个比一个浪漫 赶紧收藏起来吧!!!
    奉劝各位学弟学妹们,该打造你的技术影响力了!
    五年了,我在 CSDN 的两个一百万。
    Java俄罗斯方块,老程序员花了一个周末,连接中学年代!
    面试官都震惊,你这网络基础可以啊!
    你真的会用百度吗?我不信 — 那些不为人知的搜索引擎语法
    心情不好的时候,用 Python 画棵樱花树送给自己吧
    通宵一晚做出来的一款类似CS的第一人称射击游戏Demo!原来做游戏也不是很难,连憨憨学妹都学会了!
    13 万字 C 语言从入门到精通保姆级教程2021 年版
    10行代码集2000张美女图,Python爬虫120例,再上征途
小工具 小游戏
Copyright © 2022 侵权请联系2656653265@qq.com    京ICP备2022015340号-1

京公网安备 11010502049817号