{"id":15014252,"url":"https://github.com/avidbyte/netty-websocket-boot-starter","last_synced_at":"2026-03-04T22:31:58.185Z","repository":{"id":196417268,"uuid":"694661204","full_name":"avidbyte/netty-websocket-boot-starter","owner":"avidbyte","description":"websocket framework based on netty","archived":false,"fork":false,"pushed_at":"2024-12-06T02:34:11.000Z","size":202,"stargazers_count":5,"open_issues_count":0,"forks_count":1,"subscribers_count":1,"default_branch":"main","last_synced_at":"2025-03-26T02:51:13.309Z","etag":null,"topics":["chat","im","java","netty","netty-websocket","spring-boot-websocket","websocket","websocket-server","websocket-starter"],"latest_commit_sha":null,"homepage":"","language":"Java","has_issues":true,"has_wiki":null,"has_pages":null,"mirror_url":null,"source_name":null,"license":"apache-2.0","status":null,"scm":"git","pull_requests_enabled":true,"icon_url":"https://github.com/avidbyte.png","metadata":{"files":{"readme":"README.md","changelog":null,"contributing":null,"funding":null,"license":"LICENSE","code_of_conduct":null,"threat_model":null,"audit":null,"citation":null,"codeowners":null,"security":null,"support":null,"governance":null,"roadmap":null,"authors":null,"dei":null,"publiccode":null,"codemeta":null}},"created_at":"2023-09-21T12:49:38.000Z","updated_at":"2024-12-06T02:34:15.000Z","dependencies_parsed_at":null,"dependency_job_id":"b643f326-ec2a-401c-b117-750c7c5e5eb4","html_url":"https://github.com/avidbyte/netty-websocket-boot-starter","commit_stats":{"total_commits":25,"total_committers":2,"mean_commits":12.5,"dds":"0.040000000000000036","last_synced_commit":"9c6ae9a5473eb7181cd005e55eda2aa090b68d39"},"previous_names":["tianzunh/netty-websocket-boot-starter","syncbit/netty-websocket-boot-starter","avidbyte/netty-websocket","avidbyte/netty-websocket-boot-starter"],"tags_count":1,"template":false,"template_full_name":null,"repository_url":"https://repos.ecosyste.ms/api/v1/hosts/GitHub/repositories/avidbyte%2Fnetty-websocket-boot-starter","tags_url":"https://repos.ecosyste.ms/api/v1/hosts/GitHub/repositories/avidbyte%2Fnetty-websocket-boot-starter/tags","releases_url":"https://repos.ecosyste.ms/api/v1/hosts/GitHub/repositories/avidbyte%2Fnetty-websocket-boot-starter/releases","manifests_url":"https://repos.ecosyste.ms/api/v1/hosts/GitHub/repositories/avidbyte%2Fnetty-websocket-boot-starter/manifests","owner_url":"https://repos.ecosyste.ms/api/v1/hosts/GitHub/owners/avidbyte","download_url":"https://codeload.github.com/avidbyte/netty-websocket-boot-starter/tar.gz/refs/heads/main","host":{"name":"GitHub","url":"https://github.com","kind":"github","repositories_count":248536981,"owners_count":21120683,"icon_url":"https://github.com/github.png","version":null,"created_at":"2022-05-30T11:31:42.601Z","updated_at":"2022-07-04T15:15:14.044Z","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":["chat","im","java","netty","netty-websocket","spring-boot-websocket","websocket","websocket-server","websocket-starter"],"created_at":"2024-09-24T19:45:22.545Z","updated_at":"2026-03-04T22:31:58.165Z","avatar_url":"https://github.com/avidbyte.png","language":"Java","funding_links":[],"categories":[],"sub_categories":[],"readme":"netty-websocket-boot-starter\n===================================\n\n[![License](https://img.shields.io/badge/license-Apache--2.0-4D7A97)](https://github.com/avidbyte/netty-websocket-boot-starter/blob/main/LICENSE)\n[![Maven Central](https://img.shields.io/badge/maven--central-1.0.1-blue)](https://mvnrepository.com/artifact/io.github.avidbyte/netty-websocket-boot-starter/1.0.1)\n[![Maven Central](https://img.shields.io/badge/PRs--welcome-red)](https://github.com/avidbyte/netty-websocket-boot-starter/pulls)\n\n[简体中文](https://github.com/avidbyte/netty-websocket-boot-starter/blob/main/README.md) ｜ [English](https://github.com/avidbyte/netty-websocket-boot-starter/blob/main/README_en.md) \n\n\n### 概述\n这是一个轻量级、高性能的基于Netty的WebSocket框架，提升你的WebSocket开发体验，为Spring Boot带来全新的WebSocket功能。\n该项目使你可以轻松集成WebSocket功能到你的Spring Boot项目中，提供了Tomcat WebSocket的简单性，同时享受Netty性能和可扩展性的优势。\n\n主要特点：\n- 无缝集成Spring Boot\n- 轻量级和高性能\n- 简化实时应用程序开发\n- 基于强大的Netty框架构建\n\n开源之路不易，如果你觉得本项目对你有所帮助，请慷慨地点一个 Star，你的支持将是我持续前进的动力！\n### 要求\n- jdk version 1.8 or 1.8+\n\n### 快速开始\n\n- 添加依赖:\n\n```xml\n\n\u003cdependency\u003e\n    \u003cgroupId\u003eio.github.avidbyte\u003c/groupId\u003e\n    \u003cartifactId\u003enetty-websocket-boot-starter\u003c/artifactId\u003e\n    \u003cversion\u003e1.0.1\u003c/version\u003e\n\u003c/dependency\u003e\n```\n\n- 在端点类上注释`@ServerEndpoint`，并在方法上注释`@BeforeHandshake`,`@OnOpen`,`@OnClose`,`@OnError`,`@OnMessage`,`@OnBinary`,`@OnEvent`。\n\n```java\nimport io.github.avidbyte.annotation.*;\nimport io.github.avidbyte.standard.Session;\nimport io.netty.handler.codec.http.HttpHeaders;\nimport io.netty.handler.timeout.IdleStateEvent;\nimport lombok.extern.slf4j.Slf4j;\nimport org.springframework.stereotype.Service;\nimport org.springframework.util.StringUtils;\n\nimport java.util.Map;\nimport java.util.concurrent.ConcurrentHashMap;\n\n\n@Slf4j\n@Service\n@ServerEndpoint(\"/chat/text\")\npublic class TextWebSocket {\n    private static final Map\u003cString, Session\u003e CLIENTS = new ConcurrentHashMap\u003c\u003e();\n\n\n    @BeforeHandshake\n    public void handshake(Session session, HttpHeaders headers) {\n        String token = headers.get(\"token\");\n        System.out.println(\"token:\" + token);\n    }\n\n    @OnOpen\n    public void onOpen(@PathParam(\"username\") String username, Session session) {\n        if (!StringUtils.hasLength(username)) {\n            log.error(\"username is empty\");\n            return;\n        }\n        log.info(\"username={}\", username);\n        session.setAttribute(\"name\", username);\n        CLIENTS.put(username, session);\n    }\n\n    @OnClose\n    public void onClose(Session session){\n        String name = session.getAttribute(\"name\");\n        CLIENTS.remove(name);\n        session.close();\n    }\n\n    @OnMessage\n    public void onMessage(Session session, String message){\n        log.info(\"message={}\", message);\n        String ping = \"ping\";\n        if (ping.equals(message)) {\n            session.sendText(\"pong\");\n        }\n    }\n\n    @OnEvent\n    public void onEvent(Session session, Object evt) {\n        if (evt instanceof IdleStateEvent) {\n            IdleStateEvent idleStateEvent = (IdleStateEvent) evt;\n            switch (idleStateEvent.state()) {\n                case READER_IDLE:\n                    log.info(\"read idle\");\n                    break;\n                case WRITER_IDLE:\n                    log.info(\"write idle\");\n                    break;\n                case ALL_IDLE:\n                    log.info(\"all idle\");\n                    break;\n                default:\n                    break;\n            }\n        }\n    }\n\n    @OnError\n    public void onError(Session session, Throwable error){\n        String name = session.getAttribute(\"name\");\n        log.error(\"A communication error occurred and the connection was closed = {}\", name);\n        CLIENTS.remove(name);\n        session.close();\n    }\n\n    public void sendMessageTo(String message, Session session) {\n        session.sendText(message);\n    }\n\n    public void sendMessageAll(String message) {\n        for (Session session : CLIENTS.values()) {\n            session.sendText(message);\n        }\n    }\n\n}\n```\n\n- 使用Websocket客户端连接 `ws://127.0.0.1:80/chat/text?username=Aaron`\n\n\n### Annotation\n###### @ServerEndpoint\n\u003e 每个标有@ServerEndpoint的类都会为其启动一个websocket服务。 每个服务都可以在配置文件的路径中指定其端口。\n\n###### @BeforeHandshake\n\u003e 当有新的连接进入时，将调用`@BeforeHandshake`注释的方法\n\u003e 注入到方法中的类有：Session、HttpHeaders\n\n###### @OnOpen\n\u003e 当WebSocket连接完成时，会调用`@OnOpen`注解的方法\n\u003e 注入到方法中的类有：Session、HttpHeaders\n\n###### @OnClose\n\u003e 当WebSocket连接关闭时，将调用`@OnClose`注释的方法\n\u003e 注入到方法中的类有：Session\n\n###### @OnError\n\u003e 当WebSocket连接抛出Throwable时，将调用带有`@OnError`注释的方法\n\u003e 注入到方法中的类有：Session、Throwable\n\n###### @OnMessage\n\u003e 当WebSocket连接收到消息时，将调用带有`@OnMessage`注释的方法\n\u003e 注入到方法中的类有：Session、String\n\n###### @OnBinary\n\u003e 当WebSocket连接收到二进制文件时，将调用带有`@OnBinary`注释的方法\n\u003e 注入到方法中的类有：Session、byte[]\n\n###### @OnEvent\n\u003e 当WebSocket连接收到Netty的事件时，会调用`@OnEvent`注解的方法\n\u003e 注入到方法中的类有：Session、Object\n\n### Configuration\n\n\u003e @ServerEndpoint只需要配置路径，该路径对应的配置全部在application.yml 中\n\u003e 默认配置为host: 0.0.0.0, 端口: 80\n\n#### Configuration by application.yml\n```yaml\nnetty:\n  websocket:\n    endpoint:\n      chat/text:\n        port: 80\n        reader-idle-time-seconds: 10\n        writer-idle-time-seconds: 20\n        all-idle-time-seconds: 30\n      chat/audio:\n        port: 81\n        reader-idle-time-seconds: 10\n        writer-idle-time-seconds: 20\n        all-idle-time-seconds: 30\n```\n#### Configuration by application.properties\n```properties\nnetty.websocket.endpoint.chat/text.port=80\nnetty.websocket.endpoint.chat/text.reader-idle-time-seconds=10\nnetty.websocket.endpoint.chat/text.writer-idle-time-seconds=20\nnetty.websocket.endpoint.chat/text.all-idle-time-seconds=30\nnetty.websocket.endpoint.chat/audio.port=81\nnetty.websocket.endpoint.chat/audio.reader-idle-time-seconds=10\nnetty.websocket.endpoint.chat/audio.writer-idle-time-seconds=20\nnetty.websocket.endpoint.chat/audio.all-idle-time-seconds=30\n```\n\n\n\n#### 所有配置参数\n\n\n| property                                  | default          | description                                                                                             |\n|-------------------------------------------|------------------|---------------------------------------------------------------------------------------------------------|\n| host                                      | \"0.0.0.0\"        | WebSocket 的主机。`\"0.0.0.0\"` 表示所有本地地址                                                                      |\n| port                                      | 80               | websocket 服务端口                                                                                          |\n| boss-loop-group-threads                   | 1                | bossEventLoopGroup 的线程数                                                                                 |\n| worker-loop-group-threads                 | 0                | workerEventLoopGroup 的线程数                                                                               |\n| use-compression-handler                   | false            | 是否将WebSocketServerCompressionHandler添加到管道                                                               |\n| option-connect-timeout-millis             | 30000            | 与 Netty 中的 `ChannelOption.CONNECT_TIMEOUT_MILLIS` 相同                                                    |\n| option-so-backlog                         | 128              | 与 Netty 中的 `ChannelOption.SO_BACKLOG` 相同                                                                |\n| child-option-write-spin-count             | 16               | 与 Netty 中的 `ChannelOption.WRITE_SPIN_COUNT` 相同                                                          |\n| child-option-write-buffer-high-water-mark | 64*1024          | 与 Netty 中的`ChannelOption.WRITE_BUFFER_HIGH_WATER_MARK`相同，但实际上使用`ChannelOption.WRITE_BUFFER_WATER_MARK`。 |\n| child-option-write-buffer-low-water-mark  | 32*1024          | 与 Netty 中的`ChannelOption.WRITE_BUFFER_LOW_WATER_MARK`相同，但实际上使用`ChannelOption.WRITE_BUFFER_WATER_MARK`。  |\n| child-option-so-rcv-buf                   | -1(mean not set) | 与 Netty 中的`ChannelOption.SO_RCVBUF`相同                                                                   |\n| child-option-so-snd-buf                   | -1(mean not set) | 与 Netty 中的`ChannelOption.SO_SNDBUF`相同                                                                   |\n| child-option-tcp-nodelay                  | true             | 与 Netty 中的 `ChannelOption.TCP_NODELAY` 相同                                                               |\n| child-option-so-keepalive                 | false            | 与 Netty 中的`ChannelOption.SO_KEEPALIVE`相同                                                                |\n| child-option-so-linger                    | -1               | 与 Netty 中的 `ChannelOption.SO_LINGER` 相同                                                                 |\n| child-option-allow-half-closure           | false            | 与 Netty 中的 `ChannelOption.ALLOW_HALF_CLOSURE` 相同                                                        |\n| reader-idle-time-seconds                  | 0                | 与`IdleStateHandler`中的`readerIdleTimeSeconds`相同，当不为0时将`IdleStateHandler`添加到`pipeline`中                   |\n| writer-idle-time-seconds                  | 0                | 与`IdleStateHandler`中的`writerIdleTimeSeconds`相同，当不为0时将`IdleStateHandler`添加到`pipeline`中                   |\n| all-idle-time-seconds                     | 0                | 与`IdleStateHandler`中的`allIdleTimeSeconds`相同，当不为0时将`IdleStateHandler`添加到`pipeline`中                      |\n| max-frame-payload-length                  | 65536            | 最大允许帧有效负载长度。                                                                                            |\n| use-event-executor-group                  | true             | 是否使用另一个线程池来执行耗时的同步业务逻辑                                                                                  |\n| event-executor-group-threads              | 16               | bossEventLoopGroup 的线程数                                                                                 |\n| ssl-key-password                          | \"\"(mean not set) | 与 spring-boot 中的 `server.ssl.key-password` 相同                                                           |\n| ssl-key-store                             | \"\"(mean not set) | 与 spring-boot 中的 `server.ssl.key-store` 相同                                                              |\n| ssl-key-password                          | \"\"(mean not set) | 与 spring-boot 中的 `server.ssl.key-store-password` 相同                                                     |\n| ssl-key-store-type                        | \"\"(mean not set) | 与 spring-boot 中的 `server.ssl.key-store-type` 相同                                                         |\n| ssl-trust-store                           | \"\"(mean not set) | 与 spring-boot 中的 `server.ssl.trust-store` 相同                                                            |\n| ssl-trust-store-password                  | \"\"(mean not set) | 与 spring-boot 中的 `server.ssl.trust-store-password` 相同                                                   |\n| ssl-trust-store-type                      | \"\"(mean not set) | 与 spring-boot 中的 `server.ssl.trust-store-type` 相同                                                       |\n| cors-origins                              | {}(mean not set) | 与 spring-boot 中的“@CrossOrigin#origins”相同                                                                |\n| cors-allow-credentials                    | \"\"(mean not set) | 与 spring-boot 中的“@CrossOrigin#allowCredentials”相同                                                       |\n\n---\n\n\n### Explanation\n#### WebSocketServerCompressionHandler\nWebSocketServerCompressionHandler 是 Netty 中用于 WebSocket 消息压缩的处理器。它的主要作用是启用 WebSocket 消息压缩以减少数据传输的大小，从而提高网络传输效率。\n#### ChannelOption.CONNECT_TIMEOUT_MILLIS\n用于设置连接超时的时间（以毫秒为单位）。它用于控制在尝试建立连接时，如果连接不能在指定的时间内建立，将会触发连接超时异常;实际的超时行为可能会受到操作系统和网络条件的影响，因此超时时间并不总是精确的。但它可以作为一种有效的手段来避免无限等待并处理连接失败的情况。\n\n### 更新日志\n\n#### 1.0.0\n\n- 实现 websocket 服务的基本功能\n-\n#### 1.0.1\n\n- fix:调整项目结构，解决jar依赖找不到的问题\n\n","project_url":"https://awesome.ecosyste.ms/api/v1/projects/github.com%2Favidbyte%2Fnetty-websocket-boot-starter","html_url":"https://awesome.ecosyste.ms/projects/github.com%2Favidbyte%2Fnetty-websocket-boot-starter","lists_url":"https://awesome.ecosyste.ms/api/v1/projects/github.com%2Favidbyte%2Fnetty-websocket-boot-starter/lists"}