ohos_stompjs:基于 OpenHarmony 的 STOMP 协议 WebSocket 客户端项目

通过stomp架构实现WebSocket的连接,订阅,发送,接收,取消订阅,断开连接等功能。

Branch1Tags2

ohos_stompjs

Overview

ohos_stompjs uses STOMP to handle WebSocket connection, subscription, message sending, and their reverse operations.

How to Install

  1. Configure the OpenHarmony ohpm environment. For details, see OpenHarmony HAR.
  2. Run the following installation command:
ohpm install  @ohos/stompjs

How to Use

  1. Initialization: Instantiate a compatClient object.
 this.compatClient = Stomp.client(this.wsAddress);
  1. Connect to the 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;
   })
  1. Disconnect from WebSocket.
 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;
       }
   });
  1. Subscribe to the node and receive WebSocket data.
 this.pushSubscriptionPromise = this.compatClient.subscribe(this.subscribeAddress, message => {
   // Process the received message.
   let nowMessage = 'The ' + number + ' time get the message: ' +
       JSON.stringify(JSON.parse(message.body)) + '\r\n';
   this.message = nowMessage + this.message;
   number++;
   });
  1. Cancel the subscription.
  if (this.pushSubscriptionPromise) {
             this.pushSubscriptionPromise.unsubscribe();
   }
  1. Send data.
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));
}

Available APIs

CompatClient Method

Name Input Parameter Description
connect(login:string, passCode:string, connectCallback?:function, errorCallback?:function):void login: login account (type: string)
passCode: login password (type: string)
connectCallback: success callback upon connection (type: function)
errorCallback: failure callback (type: function)
Connects to WebSocket.
disconnect(disconnectCallback?:function, headers?: StompHeaders) :void disconnectCallback: (optional) disconnection callback (type: function)
headers: (optional) STOMP header data (type: StompHeaders)
Disconnects from WebSocket.
subscribe(subscribeAddress:string, callback:function):void subscribeAddress: subscription address (type: string)
callback: subscription callback (type: function)
Subscribes to a specified node.
unsubscribe():void N/A Cancels a subscription.
send(destination: string, headers?: {[key: string]: any;}, body?: string):void destination: sending address (type: string)
headers: (optional) header array
Sends data to a specified node.
begin(id:string):void id: ID of a begin operation (type: string) Begins a transaction. The returned ITransaction contains the commit and abort methods.
commit(id:string):void id: ID of a commit operation (type: string) Commits a transaction by calling commit on the ITransaction returned by client.begin.
abort(id:string):void id: ID of an abort operation (type: string) Aborts a transaction. You are advised to call abort on the ITransaction returned by client.begin.
ack():void N/A Normal message. You are advised to call ack on the IMessage returned by the subscription callback to confirm the message.
nack():void N/A Exception message. You are advised to call nack on the IMessage returned by the subscription callback to confirm the message.

Constraints

This project has been verified in the following versions:

  • DevEco Studio NEXT Developer Beta3: 5.0.3.530

  • OpenHarmony SDK: API 12 (5.0.0.35)

Directory Structure

|---- StompDemo 
|     |---- entry  # Sample code
|     |---- library  # Stompjs library
|	    |----src 
|         |----main 
|            |----ets 
|               |----Stompjs # Core code of the Stompjs library
|         |---- index.ets  # External APIs
|     |---- README.md  # Readme

How to Contribute

If you find any problem when using the project, submit an issue or a PR.

License

This project is licensed under Apache License 2.0.

Introduction

通过stomp架构实现WebSocket的连接,订阅,发送,接收,取消订阅,断开连接等功能。

Customize your domain