Location>code7788 >text

The use of Hongmeng WebSocket is so simple

Popularity:737 ℃/2025-03-13 12:22:17

Using WebSocket to establish a two-way connection between the server and the client, you need to first create a WebSocket object through the createWebSocket() method, and then connect to the server through the connect() method. When the connection is successful, the client will receive a callback from the open event, and the client can communicate with the server through the send() method. When the server sends a message to the client, the client will receive a callback for the message event. When the client does not need this connection, you can actively disconnect by calling the close() method, and the client will receive a callback for the close event.

If an error occurs in any of the above processes, the client will receive a callback of the error event.

The websocket supports a heartbeat detection mechanism. After establishing a WebSocket connection between the client and the server, the client will send a Ping frame to the server every period of time. The server should reply to the Pong frame immediately after receiving it.

Interface description

The WebSocket connection function is mainly provided by the webSocket module. Use this function requires permission. The specific interface is as follows

  • createWebSocket(): Create a WebSocket connection
  • connect(): Establish a WebSocket connection based on the URL address
  • send(): Send data through WebSocket connection
  • close(): Close WebSocket connection
  • on(type: 'open'): Subscribe to the open event of WebSocket
  • off(type: 'open'): Unsubscribe to the open event of WebSocket
  • on(type: 'message'): Subscribe to WebSocket to receive server message event
  • off(type: 'message'): Unsubscribe to the received server message event of WebSocket
  • on(type: 'close'): Subscribe to the closed event of WebSocket
  • off(type: 'close'): Unsubscribe to the closed event of WebSocket
  • on(type: 'error'): Subscribe to the Error event of WebSocket
  • off(type: 'error'): Unsubscribe to the Error event of WebSocket

Development steps

  1. Import the required webSocket module
  2. Create a WebSocket connection and return a WebSocket object
  3. (Optional) Subscribe to the Open, Message Receive, Close, Error Events of WebSocket
  4. Start a WebSocket connection based on the URL address
  5. After using WebSocket connection, actively disconnect

Sample code

import { webSocket } from '@';
 import { BusinessError } from '@';

 let defaultIpAddress = "ws://";
 let ws = ();
 ('open', (err: BusinessError, value: Object) => {
   ("on open, status:" + (value));
   // When the on('open') event is received, you can communicate with the server through the send() method
   ("Hello, server!", (err: BusinessError, value: boolean) => {
     if (!err) {
       ("Message send successfully");
     } else {
       ("Failed to send the message. Err:" + (err));
     }
   });
 });
 ('message', (err: BusinessError, value: string | ArrayBuffer) => {
   ("on message, message:" + value);
   // When receiving the `bye` message from the server (this message field is only an indication, and the specific field needs to be negotiated with the server), actively disconnect the connection
   if (value === 'bye') {
     ((err: BusinessError, value: boolean) => {
       if (!err) {
         ("Connection closed successfully");
       } else {
         ("Failed to close the connection. Err: " + (err));
       }
     });
   }
 });
 ('close', (err: BusinessError, value: ) => {
   ("on close, code is " + + ", reason is " + );
 });
 ('error', (err: BusinessError) => {
   ("on error, error:" + (err));
 });
 (defaultIpAddress, (err: BusinessError, value: boolean) => {
   if (!err) {
     ("Connected successfully");
   } else {
     ("Connection failed. Err:" + (err));
   }
 });