# ohos_stomp **Repository Path**: pizice/stomp ## Basic Information - **Project Name**: ohos_stomp - **Description**: 通过Stomp har包与WebSocket服务进行无缝集成,以实现高效的双向数据通信。 - **Primary Language**: Unknown - **License**: Apache-2.0 - **Default Branch**: master - **Homepage**: None - **GVP Project**: No ## Statistics - **Stars**: 0 - **Forks**: 1 - **Created**: 2024-11-02 - **Last Updated**: 2024-12-19 ## Categories & Tags **Categories**: Uncategorized **Tags**: None ## README # Stomp ## 简介 > 通过stomp架构实现WebSocket的连接,订阅,发送,接收,取消订阅,断开连接等功能。 > ## 下载安装 1. 参考安装教程 [如何安装OpenHarmony ohpm包](https://gitee.com/openharmony-tpc/docs/blob/master/OpenHarmony_har_usage.md) 2. 安装命令如下: ```` ohpm install @ohos/stompjs ```` ## 使用说明 1、初始化:实例化compatClient对象 ``` this.compatClient = Stomp.client(this.wsAddress); ``` 2、连接到Websocket ``` this.compatClient.connect("admin", "admin", (frame: FrameImpl) => { this.message = frame.command + ' succeed ' + '\n' + this.message; this.isConnect = true; }, (error: BusinessError) => { this.message = 'Connect fail!' + error.message + '\n' + this.message; this.isConnect = false; }) ``` 3、断开连接。 ``` this.compatClient.disconnect((options: webSocket.WebSocketCloseOptions) => { if (options.code === 1000) { this.message = 'disconnect succeed options.code:' + options.code + ' options.reason:' + options.reason + '\n' + this.message; this.isConnect = false; if (this.isSubscribed) { this.message = 'Unsubscribe address:' + this.subscribeAddress + '\n' + this.message; this.isSubscribed = false; if (this.pushSubscriptionPromise) { this.pushSubscriptionPromise.unsubscribe(); } } } else { this.message = 'disconnect fail options.code:' + options.code + ' options.reason:' + options.reason + '\n' + this.message; } }); ``` 4、订阅节点并接收WebSocket数据。 ``` this.pushSubscriptionPromise = this.compatClient.subscribe(this.subscribeAddress, message => { // 处理接收到的消息 let nowMessage = 'The ' + number + ' time get the message:' + JSON.stringify(JSON.parse(message.body)) + '\r\n'; this.message = nowMessage + this.message; number++; }); ``` 5、取消订阅。 ``` if (this.pushSubscriptionPromise) { this.pushSubscriptionPromise.unsubscribe(); } ``` 6、发送数据。 ``` if (!this.isConnect) { console.log('Please connect first \n'); } else if (!this.isSubscribed) { console.log('Please subscribe first \n'); } else { this.compatClient.send(this.subscribeAddress, { "content-type": "text/plain" }, JSON.stringify(this.sendMessage)); } ``` ## 接口说明 ### CompatClient 方法 | 方法名 | 描述 | | ----------------------- | ------------------------------ | | connect(login:string, passCode:string, connectCallback?:function, errorCallback?:function)| 连接到webSocket | | disconnect(disconnectCallback?:function, headers?: StompHeaders) | 断开webSocket连接 | | subscribe(subscribeAddress, callback:function |订阅指定节点| | unsubscribe() | 取消订阅 | | send(destination: string, headers?: {[key: string]: any;}, body?: string)|对指定节点发送数据 | ## 约束与限制 在下述版本验证通过: - DevEco Studio 版本: NEXT Developer Beta3-5.0.3.530 - OpenHarmony SDK:API12 (5.0.0.35) ## 目录结构 ```` |---- StompDemo | |---- entry # 示例代码文件夹 | |---- library # Stompjs库文件夹 | |----src | |----main | |----ets | |----Stompjs # Stompjs库核心代码 | |---- index.ets # 对外接口 | |---- README.md # 安装使用方法 ```` ## 贡献代码 使用过程中发现任何问题都可以提 [Issue](https://gitee.com/openharmony-sig/ohos_stompjs/issues)给组件,当然,也非常欢迎发 [PR](https://gitee.com/openharmony-sig/ohos_stompjs/pulls)共建 。 ## 开源协议 本项目基于[Apache License 2.0](https://gitee.com/openharmony-sig/ohos_video_trimmer/blob/master/LICENSE) ,请自由地享受和参与开源。