{"id":24922850,"url":"https://github.com/twelvet-projects/netty-websocket-spring-boot-starter","last_synced_at":"2025-04-14T23:14:32.711Z","repository":{"id":53581874,"uuid":"521488380","full_name":"twelvet-projects/netty-websocket-spring-boot-starter","owner":"twelvet-projects","description":"(Based on Netty's WebSocket)基于Netty作为底层实现的Spring Boot Starter轻量级、高性能WebSocket框架，支持多端口，多路径，SSL，jdk \u003e= 1.8 (兼容Spring Boot 3)","archived":false,"fork":false,"pushed_at":"2023-11-27T00:38:27.000Z","size":114,"stargazers_count":12,"open_issues_count":0,"forks_count":3,"subscribers_count":1,"default_branch":"master","last_synced_at":"2025-04-14T23:14:22.962Z","etag":null,"topics":["java","netty","spring-boot","websocket"],"latest_commit_sha":null,"homepage":"https://twelvet.cn","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/twelvet-projects.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":"2022-08-05T03:20:17.000Z","updated_at":"2024-09-12T03:13:27.000Z","dependencies_parsed_at":"2025-03-28T11:26:47.144Z","dependency_job_id":null,"html_url":"https://github.com/twelvet-projects/netty-websocket-spring-boot-starter","commit_stats":null,"previous_names":["twelvet-projects/netty-websocket-spring-boot-starter"],"tags_count":0,"template":false,"template_full_name":null,"repository_url":"https://repos.ecosyste.ms/api/v1/hosts/GitHub/repositories/twelvet-projects%2Fnetty-websocket-spring-boot-starter","tags_url":"https://repos.ecosyste.ms/api/v1/hosts/GitHub/repositories/twelvet-projects%2Fnetty-websocket-spring-boot-starter/tags","releases_url":"https://repos.ecosyste.ms/api/v1/hosts/GitHub/repositories/twelvet-projects%2Fnetty-websocket-spring-boot-starter/releases","manifests_url":"https://repos.ecosyste.ms/api/v1/hosts/GitHub/repositories/twelvet-projects%2Fnetty-websocket-spring-boot-starter/manifests","owner_url":"https://repos.ecosyste.ms/api/v1/hosts/GitHub/owners/twelvet-projects","download_url":"https://codeload.github.com/twelvet-projects/netty-websocket-spring-boot-starter/tar.gz/refs/heads/master","host":{"name":"GitHub","url":"https://github.com","kind":"github","repositories_count":248975330,"owners_count":21192210,"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":["java","netty","spring-boot","websocket"],"created_at":"2025-02-02T11:33:54.043Z","updated_at":"2025-04-14T23:14:32.693Z","avatar_url":"https://github.com/twelvet-projects.png","language":"Java","funding_links":[],"categories":[],"sub_categories":[],"readme":"[中文](https://github.com/twelvet-projects/netty-websocket-spring-boot-starter/blob/master/README_ZH.md) | [English](https://github.com/twelvet-projects/netty-websocket-spring-boot-starter/blob/master/README.md)\n# netty-websocket-spring-boot-starter\n\nIt is a code refactoring and feature enhancement for the original [netty-websocket-spring-boot-starter](https://github.com/YeautyYE/netty-websocket-spring-boot-starter).\n\nThank you very much for the author's sharing of `netty-websocket-spring-boot-starter`.\n\n[![AUR](https://img.shields.io/github/license/twelvet-projects/twelvet)](https://github.com/twelvet-projects/netty-websocket-spring-boot-starter/blob/master/LICENSE)\n[![](https://img.shields.io/badge/Author-TwelveT-orange.svg)](https://twelvet.cn)\n[![](https://img.shields.io/badge/version-1.0.0-success)](https://github.com/twelvet-projects/netty-websocket-spring-boot-starter)\n[![GitHub stars](https://img.shields.io/github/stars/twelvet-projects/netty-websocket-spring-boot-starter.svg?style=social\u0026label=Stars)](https://github.com/twelvet-projects/netty-websocket-spring-boot-starter/stargazers)\n[![GitHub forks](https://img.shields.io/github/forks/twelvet-projects/netty-websocket-spring-boot-starter.svg?style=social\u0026label=Fork)](https://github.com/twelvet-projects/netty-websocket-spring-boot-starter/network/members)\n[![star](https://gitee.com/twelvet/netty-websocket-spring-boot-starter/badge/star.svg?theme=white)](https://gitee.com/twelvet/netty-websocket-spring-boot-starter/stargazers)\n[![fork](https://gitee.com/twelvet/netty-websocket-spring-boot-starter/badge/fork.svg?theme=white)](https://gitee.com/twelvet/netty-websocket-spring-boot-starter/members)\n\n### Introduction\nDeveloping a WebSocket server using Netty in Spring Boot, with the simplicity and high performance of spring-websocket annotations.\n\n### Requirements\n- jdk \u003e= 1.8 (compatible with jdk 17、21)\n\n### Quick Start\n\n- Add dependencies:\n\n```xml\n\u003cdependency\u003e\n    \u003cgroupId\u003ecn.twelvet\u003c/groupId\u003e\n    \u003cartifactId\u003enetty-websocket-spring-boot-starter\u003c/artifactId\u003e\n    \u003cversion\u003e${version}\u003c/version\u003e\n\u003c/dependency\u003e\n```\n\n- Add the `@WebSocketEndpoint` annotation to the endpoint class, and add the `@BeforeHandshake`、`@OnOpen`、`@OnClose`、`@OnError`、`@OnMessage`、`@OnBinary` and `@OnEvent` annotations to the respective methods. Here's an example:\n- Use `@PathVariable` to retrieve path parameters and `@RequestParam` to retrieve query parameters, both of which have the same effect as the corresponding Spring annotations (Note: Use the annotations provided by this framework, not Spring's annotations).\n\n```java\n\nimport cn.twelvet.websocket.netty.annotation.*;\nimport cn.twelvet.websocket.netty.domain.NettySession;\nimport io.netty.handler.codec.http.HttpHeaders;\nimport io.netty.handler.timeout.IdleStateEvent;\nimport org.springframework.util.MultiValueMap;\n\nimport java.util.Map;\n\n@WebSocketEndpoint(path = \"/ws\")\npublic class MyWebSocket {\n\n    @BeforeHandshake\n    public void handshake(NettySession nettySession, HttpHeaders headers, @RequestParam String req, @RequestParam MultiValueMap reqMap, @PathVariable String arg, @PathVariable Map pathMap) {\n        nettySession.setSubprotocols(\"stomp\");\n        if (!\"ok\".equals(req)) {\n            System.out.println(\"Authentication failed!\");\n            // nettySession.close();\n        }\n    }\n\n    @OnOpen\n    public void onOpen(NettySession nettySession, HttpHeaders headers, @RequestParam String req, @RequestParam MultiValueMap reqMap, @PathVariable String arg, @PathVariable Map pathMap) {\n        System.out.println(\"new connection\");\n        System.out.println(req);\n    }\n\n    @OnClose\n    public void onClose(NettySession nettySession) {\n        System.out.println(\"one connection closed\");\n    }\n\n    @OnError\n    public void onError(NettySession nettySession, Throwable throwable) {\n        throwable.printStackTrace();\n    }\n\n    @OnMessage\n    public void onMessage(NettySession nettySession, String message) {\n        System.out.println(message);\n        nettySession.sendText(\"Hello Netty!\");\n    }\n\n    @OnBinary\n    public void onBinary(NettySession nettySession, byte[] bytes) {\n        for (byte b : bytes) {\n            System.out.println(b);\n        }\n        nettySession.sendBinary(bytes);\n    }\n\n    @OnEvent\n    public void onEvent(NettySession nettySession, Object evt) {\n        if (evt instanceof IdleStateEvent) {\n            IdleStateEvent idleStateEvent = (IdleStateEvent) evt;\n            switch (idleStateEvent.state()) {\n                case READER_IDLE:\n                    System.out.println(\"read idle\");\n                    break;\n                case WRITER_IDLE:\n                    System.out.println(\"write idle\");\n                    break;\n                case ALL_IDLE:\n                    System.out.println(\"all idle\");\n                    break;\n                default:\n                    break;\n            }\n        }\n    }\n\n}\n```\n\n- Open the WebSocket client and connect to `ws://127.0.0.1:80/ws/xxx`\n\n### Multi Endpoint\n- base on [Quick-Start](#quick-start),use annotation `@WebSocketEndpoint` in classes which hope to become a endpoint.\n- you can get all socket addresses in `WebSocketEndpointExporter.getAddressWebsocketServerMap()`.\n- when there are different addresses(different host or different port) in WebSocket,they will use different `ServerBootstrap` instance.\n- when the addresses are the same,but path is different,they will use the same `ServerBootstrap` instance.\n- when multiple port of endpoint is 0 ,they will use the same random port\n- when multiple port of endpoint is the same as the path,host can't be set as \"0.0.0.0\",because it means it binds all of the addresses\n\n### Configure using application.properties.\n\n\u003e All parameters can be obtained from the configuration in `application.yml` using `${...}` placeholders. Here's an example:：\n\n- First, use `${...}` placeholders in the attributes of the `@WebSocketEndpoint` annotation.\n```java\n@WebSocketEndpoint(host = \"${ws.host}\", port = \"${ws.port}\")\npublic class MyWebSocket {\n    ...\n}\n```\n- Next, you can configure it in the `application.yml` file.\n```\nws:\n  host: 0.0.0.0\n  port: 80\n```\n","project_url":"https://awesome.ecosyste.ms/api/v1/projects/github.com%2Ftwelvet-projects%2Fnetty-websocket-spring-boot-starter","html_url":"https://awesome.ecosyste.ms/projects/github.com%2Ftwelvet-projects%2Fnetty-websocket-spring-boot-starter","lists_url":"https://awesome.ecosyste.ms/api/v1/projects/github.com%2Ftwelvet-projects%2Fnetty-websocket-spring-boot-starter/lists"}