1/* 2 * Copyright (c) 2022 Huawei Device Co., Ltd. 3 * Licensed under the Apache License, Version 2.0 (the "License"); 4 * you may not use this file except in compliance with the License. 5 * You may obtain a copy of the License at 6 * 7 * http://www.apache.org/licenses/LICENSE-2.0 8 * 9 * Unless required by applicable law or agreed to in writing, software 10 * distributed under the License is distributed on an "AS IS" BASIS, 11 * WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied. 12 * See the License for the specific language governing permissions and 13 * limitations under the License. 14 */ 15 16import {AsyncCallback, ErrorCallback} from "./basic"; 17 18/** 19 * Provides WebSocket APIs. 20 * 21 * @since 6 22 * @syscap SystemCapability.Communication.NetStack 23 */ 24declare namespace webSocket { 25 /** 26 * Creates a web socket connection. 27 */ 28 function createWebSocket(): WebSocket; 29 30 export interface WebSocketRequestOptions { 31 /** 32 * HTTP request header. 33 */ 34 header?: Object; 35 } 36 37 export interface WebSocketCloseOptions { 38 /** 39 * Error code. 40 */ 41 code?: number; 42 /** 43 * Error cause. 44 */ 45 reason?: string; 46 } 47 48 export interface WebSocket { 49 /** 50 * Initiates a WebSocket request to establish a WebSocket connection to a given URL. 51 * 52 * @param url URL for establishing a WebSocket connection. 53 * @param options Optional parameters {@link WebSocketRequestOptions}. 54 * @param callback Returns callback used to return the execution result. 55 * @permission ohos.permission.INTERNET 56 */ 57 connect(url: string, callback: AsyncCallback<boolean>): void; 58 connect(url: string, options: WebSocketRequestOptions, callback: AsyncCallback<boolean>): void; 59 connect(url: string, options?: WebSocketRequestOptions): Promise<boolean>; 60 61 /** 62 * Sends data through a WebSocket connection. 63 * 64 * @param data Data to send. It can be a string(API 6) or an ArrayBuffer(API 8). 65 * @param callback Returns callback used to return the execution result. 66 * @permission ohos.permission.INTERNET 67 */ 68 send(data: string | ArrayBuffer, callback: AsyncCallback<boolean>): void; 69 send(data: string | ArrayBuffer): Promise<boolean>; 70 71 /** 72 * Closes a WebSocket connection. 73 * 74 * @param options Optional parameters {@link WebSocketCloseOptions}. 75 * @param callback Returns callback used to return the execution result. 76 * @permission ohos.permission.INTERNET 77 */ 78 close(callback: AsyncCallback<boolean>): void; 79 close(options: WebSocketCloseOptions, callback: AsyncCallback<boolean>): void; 80 close(options?: WebSocketCloseOptions): Promise<boolean>; 81 82 /** 83 * Enables listening for the open events of a WebSocket connection. 84 */ 85 on(type: 'open', callback: AsyncCallback<Object>): void; 86 87 /** 88 * Cancels listening for the open events of a WebSocket connection. 89 */ 90 off(type: 'open', callback?: AsyncCallback<Object>): void; 91 92 /** 93 * Enables listening for the message events of a WebSocket connection. 94 * data in AsyncCallback can be a string(API 6) or an ArrayBuffer(API 8). 95 */ 96 on(type: 'message', callback: AsyncCallback<string | ArrayBuffer>): void; 97 98 /** 99 * Cancels listening for the message events of a WebSocket connection. 100 * data in AsyncCallback can be a string(API 6) or an ArrayBuffer(API 8). 101 */ 102 off(type: 'message', callback?: AsyncCallback<string | ArrayBuffer>): void; 103 104 /** 105 * Enables listening for the close events of a WebSocket connection. 106 */ 107 on(type: 'close', callback: AsyncCallback<{ code: number, reason: string }>): void; 108 109 /** 110 * Cancels listening for the close events of a WebSocket connection. 111 */ 112 off(type: 'close', callback?: AsyncCallback<{ code: number, reason: string }>): void; 113 114 /** 115 * Enables listening for the error events of a WebSocket connection. 116 */ 117 on(type: 'error', callback: ErrorCallback): void; 118 119 /** 120 * Cancels listening for the error events of a WebSocket connection. 121 */ 122 off(type: 'error', callback?: ErrorCallback): void; 123 } 124} 125 126export default webSocket;