{"id":23099486,"url":"https://github.com/hiwepy/socketio-spring-boot-starter","last_synced_at":"2025-04-12T10:11:45.952Z","repository":{"id":57720047,"uuid":"127701236","full_name":"hiwepy/socketio-spring-boot-starter","owner":"hiwepy","description":"Spring Boot Starter For Netty-socketio","archived":false,"fork":false,"pushed_at":"2024-09-17T15:53:17.000Z","size":158,"stargazers_count":69,"open_issues_count":3,"forks_count":21,"subscribers_count":2,"default_branch":"master","last_synced_at":"2025-04-01T15:54:42.571Z","etag":null,"topics":["netty-socketio","socketio","spring-boot"],"latest_commit_sha":null,"homepage":null,"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/hiwepy.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":"2018-04-02T04:08:59.000Z","updated_at":"2025-02-06T17:35:55.000Z","dependencies_parsed_at":"2024-04-18T13:56:18.214Z","dependency_job_id":"f521dbc9-1435-4641-b06d-3dca534a81b4","html_url":"https://github.com/hiwepy/socketio-spring-boot-starter","commit_stats":null,"previous_names":["vindell/spring-boot-starter-socketio"],"tags_count":1,"template":false,"template_full_name":null,"repository_url":"https://repos.ecosyste.ms/api/v1/hosts/GitHub/repositories/hiwepy%2Fsocketio-spring-boot-starter","tags_url":"https://repos.ecosyste.ms/api/v1/hosts/GitHub/repositories/hiwepy%2Fsocketio-spring-boot-starter/tags","releases_url":"https://repos.ecosyste.ms/api/v1/hosts/GitHub/repositories/hiwepy%2Fsocketio-spring-boot-starter/releases","manifests_url":"https://repos.ecosyste.ms/api/v1/hosts/GitHub/repositories/hiwepy%2Fsocketio-spring-boot-starter/manifests","owner_url":"https://repos.ecosyste.ms/api/v1/hosts/GitHub/owners/hiwepy","download_url":"https://codeload.github.com/hiwepy/socketio-spring-boot-starter/tar.gz/refs/heads/master","host":{"name":"GitHub","url":"https://github.com","kind":"github","repositories_count":248550633,"owners_count":21122933,"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":["netty-socketio","socketio","spring-boot"],"created_at":"2024-12-16T23:19:20.970Z","updated_at":"2025-04-12T10:11:45.898Z","avatar_url":"https://github.com/hiwepy.png","language":"Java","funding_links":[],"categories":[],"sub_categories":[],"readme":"# socketio-spring-boot-starter\n\n\n#### 组件简介\n\n\u003e 基于 [netty-socketio 2.x](https://github.com/mrniko/netty-socketio) 开源项目实现的Socket项目整合\n\n#### 使用说明\n\n##### 1、Spring Boot 项目添加 Maven 依赖\n\n``` xml\n\u003cdependency\u003e\n\t\u003cgroupId\u003ecom.github.hiwepy\u003c/groupId\u003e\n\t\u003cartifactId\u003esocketio-spring-boot-starter\u003c/artifactId\u003e\n\t\u003cversion\u003e${project.version}\u003c/version\u003e\n\u003c/dependency\u003e\n```\n\n##### 2、在`application.yml`文件中增加如下配置\n\n```yaml\n#################################################################################################\n### SocketIO 配置：\n#################################################################################################\nsocketio:\n  redis:\n    redisson:\n      enabled: true\n      server: single\n      single:\n        address: redis://192.168.2.237:6379\n        password: redis\n        client-name: redis\n        connection-minimum-idle-size: 5\n        connection-pool-size: 50\n    template:\n      enabled: false\n  # 服务端配置\n  server:\n    enabled: true\n    ## host在本地测试可以设置为localhost或者本机IP，在Linux服务器跑可换成服务器IP\n    hostname: 192.168.2.222\n    ## netty启动端口\n    port: 10065\n    ## 添加头部版本信息\n    add-version-header: true\n    ## Ping消息间隔（毫秒），默认25秒。客户端向服务器发送一条心跳消息间隔\n    ping-interval: 25000\n    ## Ping消息超时时间（毫秒），默认60秒，这个时间间隔内没有接收到心跳消息就会发送超时事件\n    ping-timeout: 60000\n    ## 设置最大每帧处理数据的长度，防止他人利用大数据来攻击服务器\n    max-frame-payload-length: 1048576\n    ## 设置http交互最大内容长度\n    max-http-content-length: 1048576\n    ## socket连接数大小（如只监听一个端口boss线程组为1即可）\n    boss-threads: 1\n    transports:\n      - polling\n      - websocket\n    worker-threads: 100\n    ## 协议升级超时时间（毫秒），默认10秒。HTTP握手升级为ws协议超时时间\n    upgrade-timeout: 15000\n    ## socket配置\n    socket-config:\n      reuse-address: true\n      tcp-no-delay: true\n      so-linger: 0\n    ack-mode: auto\n    allow-custom-requests: true\n    ## sessionID 通过请求头io来获取\n    random-session: false\n```\n\n##### 3、前端示例\n\n```html\n\u003c!DOCTYPE html\u003e\n\u003chtml\u003e\n\u003chead\u003e\n    \u003cmeta http-equiv=\"Content-Type\" content=\"text/html; charset=UTF-8\"\u003e\n    \u003ctitle\u003eSocketio chat\u003c/title\u003e\n    \u003cscript src=\"https://cdn.bootcdn.net/ajax/libs/jquery/3.6.0/jquery.min.js\" type=\"text/javascript\"\u003e\u003c/script\u003e\n    \u003cscript type=\"text/javascript\" src=\"https://cdn.bootcdn.net/ajax/libs/socket.io/2.4.0/socket.io.min.js\"\u003e\u003c/script\u003e\n\u003c/head\u003e\n\u003cbody\u003e\n    \u003ch1\u003eNetty-socketio chat demo\u003c/h1\u003e\n    \u003cbr /\u003e\n    \u003cdiv id=\"console\" class=\"well\"\u003e\u003c/div\u003e\n    \u003cform class=\"well form-inline\" onsubmit=\"return false;\"\u003e\n        \u003cinput id=\"from\" class=\"input-xlarge\" type=\"text\" placeholder=\"from. . . \" /\u003e\n        \u003cinput id=\"to\" class=\"input-xlarge\" type=\"text\" placeholder=\"to. . . \" /\u003e\n        \u003cinput id=\"content\" class=\"input-xlarge\" type=\"text\" placeholder=\"content. . . \" /\u003e\n        \u003cbutton type=\"button\" onClick=\"sendMessage()\" class=\"btn\"\u003eSend\u003c/button\u003e\n        \u003cbutton type=\"button\" onClick=\"sendDisconnect()\" class=\"btn\"\u003eDisconnect\u003c/button\u003e\n    \u003c/form\u003e\n\u003c/body\u003e\n\u003cscript type=\"text/javascript\"\u003e\n\n    var socket = io.connect('ws://localhost:10065?userId=12121212',{\t  \n\t  path: \"/socket.io\"\n\t});\t \n\n    socket.on('connect',function() {\n\tconsole.log(111111);\n        output('\u003cspan class=\"connect-msg\"\u003eClient has connected to the server!\u003c/span\u003e');\n    });\n    socket.on('message', function(data) {\n        output('\u003cspan class=\"username-msg\"\u003e' + data.from + \"对你说：\" + data.content + '\u003c/span\u003e');\n    });\n    socket.on('disconnect',function() {\n        output('\u003cspan class=\"disconnect-msg\"\u003eThe client has disconnected! \u003c/span\u003e');\n    });\n    function sendDisconnect() {\n        socket.disconnect();\n    }\n    function sendMessage() {\n        var from = $(\"#from\").val();\n        var to = $(\"#to\").val();\n        var content = $('#content').val();\n        socket.emit('message', {\n            from : from,\n            to : to,\n            content : content\n        });\n    }\n    function output(message) {\n        var currentTime = \"\u003cspan class='time' \u003e\" + new Date() + \"\u003c/span\u003e\";\n        var element = $(\"\u003cdiv\u003e\" + currentTime + \" \" + message + \"\u003c/div\u003e\");\n        $('#console').prepend(element);\n    }\n\u003c/script\u003e\n\u003cstyle\u003e\nbody {\n    padding: 20px;\n}\n#console {\n    height: 400px;\n    overflow: auto;\n    border: 1px solid #ccc;\n}\n.username-msg {\n    color: orange;\n}\n.connect-msg {\n    color: green;\n}\n.disconnect-msg {\n    color: red;\n}\n.send-msg {\n    color: #888\n}\n\u003c/style\u003e\n\u003c/html\u003e\n```\n\n##### 3、如果通过Nginx进行服务负载，需要添加如下配置：\n\n```json\n\tlocation ~ /socket.io {   \n\n       \tproxy_http_version 1.1;\n        proxy_set_header Upgrade $http_upgrade;\n        proxy_set_header Connection \"upgrade\";\n        proxy_set_header Host $host;\n        proxy_buffering off;\n\n        proxy_set_header X-Real-IP $remote_addr;\n        proxy_set_header X-Forwarded-For $proxy_add_x_forwarded_for; \n        proxy_set_header X-Forwarded-Port $server_port;\n        proxy_set_header X-Nginx-Proxy true;\n\n        proxy_connect_timeout 60s;\n        proxy_send_timeout 600s;\n        proxy_read_timeout 600s;\n        send_timeout 600s;\n\n        proxy_pass http://122.224.247.131:10065;\n    }\n```\n\n##### 4、如果你的项目使用了uni-app进行移动端开发，需要进行接口对接，可参考如下代码进行\n\n```javascript\nvar socketTask = uni.connectSocket({\n\turl: 'wss://{your-domain}/socket.io/?transport=websocket',\n\theader: {\n\t\t'X-Authorization'\t: token,\n\t\t'io'\t\t\t\t: 'test'\n\t},\n\tsuccess: (res)=\u003e {\n\t\tconsole.log(res);\t\n\t},\n\tfail: (res)=\u003e {\n\t\tconsole.log(res);\t\n\t},\n\tcomplete: ()=\u003e {\n\t}\n});\nuni.onSocketOpen(function (res) {\n\tconsole.log('WebSocket连接已打开！');\t\t  \n});\nuni.onSocketError(function (res) {\n\tconsole.log(res,'WebSocket连接打开失败，请检查！');\n});\nuni.onSocketMessage(function (res) {\n\tconsole.log('收到服务器内容：' + res.data);\n});\nuni.onSocketClose(function (res) {\n\tconsole.log(res, 'WebSocket 已关闭！');\n});\n```\n\n#### 代码示例\n\n[https://github.com/hiwepy/spring-boot-starter-samples/tree/master/spring-boot-sample-socketio](https://github.com/hiwepy/spring-boot-starter-samples/tree/master/spring-boot-sample-socketio \"spring-boot-sample-socketio\")\n\n## Jeebiz 技术社区\n\nJeebiz 技术社区 **微信公共号**、**小程序**，欢迎关注反馈意见和一起交流，关注公众号回复「Jeebiz」拉你入群。\n\n|公共号|小程序|\n|---|---|\n| ![](https://raw.githubusercontent.com/hiwepy/static/main/images/qrcode_for_gh_1d965ea2dfd1_344.jpg)| ![](https://raw.githubusercontent.com/hiwepy/static/main/images/gh_09d7d00da63e_344.jpg)|\n","project_url":"https://awesome.ecosyste.ms/api/v1/projects/github.com%2Fhiwepy%2Fsocketio-spring-boot-starter","html_url":"https://awesome.ecosyste.ms/projects/github.com%2Fhiwepy%2Fsocketio-spring-boot-starter","lists_url":"https://awesome.ecosyste.ms/api/v1/projects/github.com%2Fhiwepy%2Fsocketio-spring-boot-starter/lists"}