{"id":21803114,"url":"https://github.com/litongjava/tio-websocket-client","last_synced_at":"2025-03-21T07:16:16.104Z","repository":{"id":220076580,"uuid":"750684474","full_name":"litongjava/tio-websocket-client","owner":"litongjava","description":"tio-websocket-client","archived":false,"fork":false,"pushed_at":"2024-10-11T10:43:36.000Z","size":69,"stargazers_count":1,"open_issues_count":0,"forks_count":0,"subscribers_count":1,"default_branch":"main","last_synced_at":"2025-01-26T04:09:42.447Z","etag":null,"topics":[],"latest_commit_sha":null,"homepage":null,"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/litongjava.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,"governance":null,"roadmap":null,"authors":null,"dei":null}},"created_at":"2024-01-31T05:31:22.000Z","updated_at":"2024-10-11T10:43:39.000Z","dependencies_parsed_at":"2024-01-31T07:35:21.273Z","dependency_job_id":null,"html_url":"https://github.com/litongjava/tio-websocket-client","commit_stats":null,"previous_names":["litongjava/tio-websocket-client"],"tags_count":0,"template":false,"template_full_name":null,"repository_url":"https://repos.ecosyste.ms/api/v1/hosts/GitHub/repositories/litongjava%2Ftio-websocket-client","tags_url":"https://repos.ecosyste.ms/api/v1/hosts/GitHub/repositories/litongjava%2Ftio-websocket-client/tags","releases_url":"https://repos.ecosyste.ms/api/v1/hosts/GitHub/repositories/litongjava%2Ftio-websocket-client/releases","manifests_url":"https://repos.ecosyste.ms/api/v1/hosts/GitHub/repositories/litongjava%2Ftio-websocket-client/manifests","owner_url":"https://repos.ecosyste.ms/api/v1/hosts/GitHub/owners/litongjava","download_url":"https://codeload.github.com/litongjava/tio-websocket-client/tar.gz/refs/heads/main","host":{"name":"GitHub","url":"https://github.com","kind":"github","repositories_count":244752362,"owners_count":20504256,"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":[],"created_at":"2024-11-27T11:38:31.394Z","updated_at":"2025-03-21T07:16:16.076Z","avatar_url":"https://github.com/litongjava.png","language":"Java","funding_links":[],"categories":[],"sub_categories":[],"readme":"# tio-websocket-client\n\n## 1. 依赖添加\n首先，您需要在项目的`pom.xml`文件中添加以下依赖，以使用`tio-websocket-client`库。\n\n```xml\n\u003cdependency\u003e\n  \u003cgroupId\u003ecom.litongjava\u003c/groupId\u003e\n  \u003cartifactId\u003etio-websocket-client\u003c/artifactId\u003e\n  \u003cversion\u003e3.7.3.v20210706\u003c/version\u003e\n\u003c/dependency\u003e\n```\n\n这个依赖是WebSocket客户端的核心，提供了建立连接、发送和接收消息等功能。\n\n## 2. WebSocket客户端测试 \n\n### 主要组件和流程:\n\n1. **消息发送跟踪:** 使用`ConcurrentHashMap`来存储和跟踪每条消息的发送状态。\n2. **消息确认机制:** 使用RxJava的`Subject`和`PublishSubject`来处理消息确认。当所有消息都确认发送后，会打印出“All sent success!”。\n3. **WebSocket客户端配置:** \n    - `onOpen`：连接打开时的回调。\n    - `onMessage`：接收到消息时的回调。更新消息状态，并打印接收到的消息。\n    - `onClose`：连接关闭时的回调。\n    - `onError`：出现错误时的回调。\n    - `onThrows`：异常处理。\n4. **连接建立:** 使用`WsClient.create`创建WebSocket客户端，并通过`connect`方法建立连接。\n5. **消息发送:** 循环发送一定数量的消息，并打印发送状态。\n```\npackage demo;\n\nimport java.util.List;\nimport java.util.Map;\nimport java.util.concurrent.ConcurrentHashMap;\nimport java.util.function.Consumer;\n\nimport com.litongjava.tio.websocket.client.WebSocket;\nimport com.litongjava.tio.websocket.client.WsClient;\nimport com.litongjava.tio.websocket.client.config.WsClientConfig;\nimport com.litongjava.tio.websocket.client.event.CloseEvent;\nimport com.litongjava.tio.websocket.client.event.ErrorEvent;\nimport com.litongjava.tio.websocket.client.event.MessageEvent;\nimport com.litongjava.tio.websocket.client.event.OpenEvent;\nimport com.litongjava.tio.websocket.common.WsPacket;\n\nimport io.reactivex.subjects.PublishSubject;\nimport io.reactivex.subjects.Subject;\n\npublic class TioWebSocketDemo {\n\n  public static void main(String[] args) throws Exception {\n    Map\u003cLong, Boolean\u003e sent = new ConcurrentHashMap\u003c\u003e();\n    int total = 1000;\n    String uri = \"ws://localhost/hello\";\n\n    // onNext\n    io.reactivex.functions.Consumer\u003c? super List\u003cObject\u003e\u003e onNext = x -\u003e {\n      Boolean all = sent.values().stream().reduce(true, (p, c) -\u003e p \u0026\u0026 c);\n      if (all) {\n        System.out.println(\"All sent success! \");\n      }\n    };\n\n    // complete\n    Subject\u003cObject\u003e complete = PublishSubject.create().toSerialized();\n    // subscribe\n    complete.buffer(total).subscribe(onNext);\n\n    // wsClientConfig\n    Consumer\u003cOpenEvent\u003e onOpen = e -\u003e System.out.println(\"opened\");\n\n    Consumer\u003cMessageEvent\u003e onMessage = e -\u003e {\n      WsPacket data = e.data;\n      Long id = data.getId();\n      String wsBodyText = data.getWsBodyText();\n      sent.put(id, true);\n      System.out.println(\"recv: \" + wsBodyText);\n      complete.onNext(id);\n    };\n\n    Consumer\u003cCloseEvent\u003e onClose = e -\u003e System.out.printf(\"on close: %d, %s, %s\\n\", e.code, e.reason, e.wasClean);\n    Consumer\u003cErrorEvent\u003e onError = e -\u003e System.out.println(String.format(\"on error: %s\", e.msg));\n    Consumer\u003cThrowable\u003e onThrows = Throwable::printStackTrace;\n\n    // wsClientConfig\n    WsClientConfig wsClientConfig = new WsClientConfig(onOpen, onMessage, onClose, onError, onThrows);\n\n    // create\n    WsClient echo = WsClient.create(uri, wsClientConfig);\n\n    // connect\n    WebSocket ws = echo.connect();\n\n    // sent\n    for (int i = 0; i \u003c total; i++) {\n      ws.send(\"\" + i);\n      System.out.println(\"sent: \" + i);\n    }\n  }\n}\n\n```","project_url":"https://awesome.ecosyste.ms/api/v1/projects/github.com%2Flitongjava%2Ftio-websocket-client","html_url":"https://awesome.ecosyste.ms/projects/github.com%2Flitongjava%2Ftio-websocket-client","lists_url":"https://awesome.ecosyste.ms/api/v1/projects/github.com%2Flitongjava%2Ftio-websocket-client/lists"}