{"id":38601043,"url":"https://github.com/niezhiliang/netty-websocket-spring-boot","last_synced_at":"2026-01-17T08:33:43.555Z","repository":{"id":52111210,"uuid":"116114849","full_name":"niezhiliang/netty-websocket-spring-boot","owner":"niezhiliang","description":"♨️基于Netty实现的websocket消息推送，单独发送信息，群发信息","archived":false,"fork":false,"pushed_at":"2023-06-27T03:09:42.000Z","size":3147,"stargazers_count":366,"open_issues_count":2,"forks_count":209,"subscribers_count":15,"default_branch":"master","last_synced_at":"2023-11-07T18:11:30.603Z","etag":null,"topics":[],"latest_commit_sha":null,"homepage":"","language":"Java","has_issues":true,"has_wiki":null,"has_pages":null,"mirror_url":null,"source_name":null,"license":null,"status":null,"scm":"git","pull_requests_enabled":true,"icon_url":"https://github.com/niezhiliang.png","metadata":{"files":{"readme":"README.md","changelog":null,"contributing":null,"funding":null,"license":null,"code_of_conduct":null,"threat_model":null,"audit":null,"citation":null,"codeowners":null,"security":null,"support":null}},"created_at":"2018-01-03T08:55:43.000Z","updated_at":"2023-11-07T08:00:34.000Z","dependencies_parsed_at":"2022-09-06T07:45:09.664Z","dependency_job_id":null,"html_url":"https://github.com/niezhiliang/netty-websocket-spring-boot","commit_stats":null,"previous_names":[],"tags_count":2,"template":null,"template_full_name":null,"purl":"pkg:github/niezhiliang/netty-websocket-spring-boot","repository_url":"https://repos.ecosyste.ms/api/v1/hosts/GitHub/repositories/niezhiliang%2Fnetty-websocket-spring-boot","tags_url":"https://repos.ecosyste.ms/api/v1/hosts/GitHub/repositories/niezhiliang%2Fnetty-websocket-spring-boot/tags","releases_url":"https://repos.ecosyste.ms/api/v1/hosts/GitHub/repositories/niezhiliang%2Fnetty-websocket-spring-boot/releases","manifests_url":"https://repos.ecosyste.ms/api/v1/hosts/GitHub/repositories/niezhiliang%2Fnetty-websocket-spring-boot/manifests","owner_url":"https://repos.ecosyste.ms/api/v1/hosts/GitHub/owners/niezhiliang","download_url":"https://codeload.github.com/niezhiliang/netty-websocket-spring-boot/tar.gz/refs/heads/master","sbom_url":"https://repos.ecosyste.ms/api/v1/hosts/GitHub/repositories/niezhiliang%2Fnetty-websocket-spring-boot/sbom","scorecard":null,"host":{"name":"GitHub","url":"https://github.com","kind":"github","repositories_count":286080680,"owners_count":28504364,"icon_url":"https://github.com/github.png","version":null,"created_at":"2022-05-30T11:31:42.601Z","updated_at":"2026-01-17T06:57:29.758Z","status":"ssl_error","status_checked_at":"2026-01-17T06:56:03.931Z","response_time":85,"last_error":"SSL_read: unexpected eof while reading","robots_txt_status":"success","robots_txt_updated_at":"2025-07-24T06:49:26.215Z","robots_txt_url":"https://github.com/robots.txt","online":false,"can_crawl_api":true,"host_url":"https://repos.ecosyste.ms/api/v1/hosts/GitHub","repositories_url":"https://repos.ecosyste.ms/api/v1/hosts/GitHub/repositories","repository_names_url":"https://repos.ecosyste.ms/api/v1/hosts/GitHub/repository_names","owners_url":"https://repos.ecosyste.ms/api/v1/hosts/GitHub/owners"}},"keywords":[],"created_at":"2026-01-17T08:33:43.454Z","updated_at":"2026-01-17T08:33:43.541Z","avatar_url":"https://github.com/niezhiliang.png","language":"Java","funding_links":[],"categories":["网络编程"],"sub_categories":["Spring Cloud框架"],"readme":"# netty-websocket-spring-boot \n\n## 前言\n\n该项目之前是基于`spring-boot-starter-websocket`做的一个Demo，start还不错， 偶然间看到大佬基于\nNetty实现了一个轻量级高性能的[Netty-Websocket框架](https://github.com/YeautyYE/netty-websocket-spring-boot-starter)， \n而且用法和`spring-boot-starter-websocket`一样，注解驱动，看到后感触很大， 觉得他很牛逼，看了下他的源码，\n大致原理和技术知道了以后，我之前也看过很多spring相关源码，觉得我应该也能写出来，于是就强迫自己重写了该项目，也算是\n对源码的巩固，太久不看，确实很多又忘了。\n\n## 介绍\n基于Netty实现了大部分spring-websocket的功能，从下图能知道Spring-Websocket使用起来非常方便，但是性能方面相较于Netty实现的WebScoket性能会有\n些差距，但是Netty做Websocket复杂度更高，为了让Netty实现WebSocket使用更简单，于是开发了这个starter,该starter使用起来可以和\nspring-websocket一样简单，而且使用语法尽可能的和Spring-Websocket一致。\n\n![演示gif](./imgs/img.png)\n\n## 使用\n\n### 支持事件类型\n| 事件   | 事件注解 | 应用场景 | \n|------|------|-----|\n| 握手前  | @HandshakeBefore | 认证、鉴权 |\n| 连接成功 | @OnOpen  | --  |\n| 收到消息 | @OnMessage  | --  |\n| 连接关闭 | @OnClose  |     |\n| 异常   | @OnError  | 异常捕获 |\n| 心跳超时 | @OnEvent  | 剔除失效连接 |\n\n### 注解方法支持的参数类型\n| 参数           | @HandshakeBefore | @OnOpen |  @OnClose |   @OnMessage | @OnEvent | @OnError | \n|--------------|---------------|-------------|------------|----------|----------|----------|\n| HttpHeaders  | √  | √  |√  | √  | √ | √ |\n| Session      | √  | √  |√  | √  | √  | √ |\n| @PathParam   | √  |√  |√ | √ | √        | √|\n| String(消息内容) | ×  | ×  |×  | √   | ×   | × |\n| Throwable    | ×  | ×  |×  | ×  | ×   | √  |\n| Object（事件）   |  ×  | ×  |×  | ×  |  √  | × |\n\n添加maven依赖\n```java\n\u003cdependency\u003e\n    \u003cgroupId\u003ecom.niezhiliang\u003c/groupId\u003e\n    \u003cartifactId\u003enetty-websocket-spring-boot-starter\u003c/artifactId\u003e\n    \u003cversion\u003e0.0.1-SNAPSHOT\u003c/version\u003e\n\u003c/dependency\u003e\n```\n\n```java\n@WsServerEndpoint(value = \"/websocket/{uid}/{arg}\")\npublic class ServerEndpoint {\n\n    @HandshakeBefore\n    public void before (HttpHeaders headers) {\n        System.out.println(\"before\");\n    }\n\n    /**\n     * 用户连接时触发\n     * @param session\n     */\n    @OnOpen\n    public void open(Session session, @PathParam (value=\"uid\") String uid, @PathParam String arg){\n        System.out.println(\"open\");\n        session.sendText(\"hello client\");\n    }\n\n    /**\n     * 收到信息时触发\n     * @param message\n     */\n    @OnMessage\n    public void onMessage(Session session,String message){\n        System.out.println(\"message:\" + message);\n        session.sendText(\"server: \" + message);\n    }\n\n    /**\n     * 连接关闭触发\n     */\n    @OnClose\n    public void onClose(){\n        System.out.println(\"close  \" + LocalDateTime.now());\n    }\n\n    /**\n     * 发生错误时触发\n     * @param session\n     * @param e\n     */\n    @OnError\n    public void onError(Session session, Throwable e) {\n        System.out.println(\"onError\");\n    }\n\n    /**\n     * 发生事件时触发\n     * @param session\n     * @param evt\n     */\n    @OnEvent\n    public void onEvent(Session session, Object evt) {\n        if (evt instanceof IdleStateEvent) {\n            // 心跳事件处理\n        }\n    }\n}\n```\n\n## 收获\n- Netty\n- 自定义注解修饰Bean，并注入容器\n- 扫描并回调自定义注解修饰方法\n- 自动装配Starter\n- 后置处理器`SmartInitializingSingleton`\n- 适配器模式\n- 方法参数解析\n- 路由匹配、请求路由参数获取\n\n\n\n\n![演示gif](https://suyu-img.oss-cn-shenzhen.aliyuncs.com/demo.gif)\n\n","project_url":"https://awesome.ecosyste.ms/api/v1/projects/github.com%2Fniezhiliang%2Fnetty-websocket-spring-boot","html_url":"https://awesome.ecosyste.ms/projects/github.com%2Fniezhiliang%2Fnetty-websocket-spring-boot","lists_url":"https://awesome.ecosyste.ms/api/v1/projects/github.com%2Fniezhiliang%2Fnetty-websocket-spring-boot/lists"}