# ohos_stompjs **Repository Path**: openharmony-sig/ohos_stompjs ## Basic Information - **Project Name**: ohos_stompjs - **Description**: 通过stomp架构实现WebSocket的连接,订阅,发送,接收,取消订阅,断开连接等功能。 - **Primary Language**: Unknown - **License**: Apache-2.0 - **Default Branch**: master - **Homepage**: https://gitee.com/openharmony-sig/ohos_stompjs - **GVP Project**: No ## Statistics - **Stars**: 1 - **Forks**: 3 - **Created**: 2024-12-16 - **Last Updated**: 2025-07-31 ## Categories & Tags **Categories**: Uncategorized **Tags**: None ## README # 🚨 **重要提示 | IMPORTANT** > > **⚠️ 此代码仓已归档。新地址请访问 [ohos_stompjs](https://gitcode.com/openharmony-sig/ohos_stompjs)。| ⚠️ This repository has been archived. For the new address, please visit [ohos_stompjs](https://gitcode.com/openharmony-sig/ohos_stompjs).** > --- > # ohos_stompjs ## 简介 > 通过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):void |login:登陆账号,类型string
passCode:登陆密码,类型string
connectCallback:连接成功回调,类型function
errorCallback:失败回调,类型function| 连接到webSocket | | disconnect(disconnectCallback?:function, headers?: StompHeaders) :void |disconnectCallback:可选参数 断开连接回调,类型function
headers:可选参数Stomp协议头部数据,类型StompHeaders| 断开webSocket连接| | subscribe(subscribeAddress:string, callback:function):void|subscribeAddress:订阅地址,类型string
callback:订阅回调,类型function|订阅指定节点| | unsubscribe():void | 无 |取消订阅 | | send(destination: string, headers?: {[key: string]: any;}, body?: string):void | destination:发送地址,类型string
headers:可选参数,header数组|对指定节点发送数据 | | begin(id:string):void |id:启动事务的id,类型:string|启动一个事务,返回的ITransaction有commit和abort方法 | | commit(id:string):void | id:提交事务的id,类型:string |通过直接在client.begin返回的ITransaction上调用commit来提交事务。 | | abort(id:string):void| id:终止事务的id,类型:string |中止交易。最好直接在client.begin返回的ITransaction上调用abort来中止事务。 | | ack():void | 无 |正常消息。最好直接在订阅回调处理的IMessage上调用ack来确认消息。 | | nack():void| 无 |异常消息。最好直接在订阅回调处理的IMessage上调用nack来确认消息 | ## 约束与限制 在下述版本验证通过: - 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_stompjs/blob/master/LICENSE) ,请自由地享受和参与开源。