From 08c044bfd417ae54d09fdd57c0b22a0b17a8f77e Mon Sep 17 00:00:00 2001 From: leo9001 Date: Thu, 11 Sep 2025 16:22:59 +0800 Subject: [PATCH] websocket api support ani Signed-off-by: leo9001 --- api/@ohos.net.webSocket.d.ts | 198 +++++++++++++++++++++++++---------- 1 file changed, 145 insertions(+), 53 deletions(-) diff --git a/api/@ohos.net.webSocket.d.ts b/api/@ohos.net.webSocket.d.ts index 2d84e71f4a..94cd4b044c 100644 --- a/api/@ohos.net.webSocket.d.ts +++ b/api/@ohos.net.webSocket.d.ts @@ -40,14 +40,14 @@ import type connection from './@ohos.net.connection'; * @syscap SystemCapability.Communication.NetStack * @crossplatform * @atomicservice - * @since 11 dynamic + * @since 11 dynamic * @since 20 static */ declare namespace webSocket { /** * @typedef { connection.HttpProxy } * @syscap SystemCapability.Communication.NetManager.Core - * @since 12 dynamic + * @since 12 dynamic * @since 20 static */ type HttpProxy = connection.HttpProxy; @@ -71,7 +71,7 @@ declare namespace webSocket { * @syscap SystemCapability.Communication.NetStack * @crossplatform * @atomicservice - * @since 11 dynamic + * @since 11 dynamic * @since 20 static */ function createWebSocket(): WebSocket; @@ -95,7 +95,7 @@ declare namespace webSocket { * @syscap SystemCapability.Communication.NetStack * @crossplatform * @atomicservice - * @since 11 dynamic + * @since 11 dynamic * @since 20 static */ export interface WebSocketRequestOptions { @@ -141,7 +141,7 @@ declare namespace webSocket { * @type {?string} * @syscap SystemCapability.Communication.NetStack * @crossplatform - * @since 12 dynamic + * @since 12 dynamic * @since 20 static */ caPath?: string; @@ -157,7 +157,7 @@ declare namespace webSocket { * @type {?ClientCert} * @syscap SystemCapability.Communication.NetStack * @crossplatform - * @since 12 dynamic + * @since 12 dynamic * @since 20 static */ clientCert?: ClientCert; @@ -166,7 +166,7 @@ declare namespace webSocket { * HTTP proxy configuration. Use 'system' if this filed is not set. * @type {?ProxyConfiguration} * @syscap SystemCapability.Communication.NetStack - * @since 12 dynamic + * @since 12 dynamic * @since 20 static */ proxy?: ProxyConfiguration; @@ -175,7 +175,7 @@ declare namespace webSocket { * Self defined protocol. * @type {?string} * @syscap SystemCapability.Communication.NetStack - * @since 12 dynamic + * @since 12 dynamic * @since 20 static */ protocol?: string; @@ -188,7 +188,7 @@ declare namespace webSocket { * object of @type {connection.HttpProxy} means providing custom proxy settings * @typedef { 'system' | 'no-proxy' | HttpProxy } * @syscap SystemCapability.Communication.NetStack - * @since 12 dynamic + * @since 12 dynamic * @since 20 static */ export type ProxyConfiguration = 'system' | 'no-proxy' | HttpProxy; @@ -208,7 +208,7 @@ declare namespace webSocket { * @interface ClientCert * @syscap SystemCapability.Communication.NetStack * @crossplatform - * @since 12 dynamic + * @since 12 dynamic * @since 20 static */ export interface ClientCert { @@ -223,7 +223,7 @@ declare namespace webSocket { * @type {string} * @syscap SystemCapability.Communication.NetStack * @crossplatform - * @since 12 dynamic + * @since 12 dynamic * @since 20 static */ certPath: string; @@ -239,7 +239,7 @@ declare namespace webSocket { * @type {string} * @syscap SystemCapability.Communication.NetStack * @crossplatform - * @since 12 dynamic + * @since 12 dynamic * @since 20 static */ keyPath: string; @@ -255,7 +255,7 @@ declare namespace webSocket { * @type {?string} * @syscap SystemCapability.Communication.NetStack * @crossplatform - * @since 12 dynamic + * @since 12 dynamic * @since 20 static */ keyPassword?: string; @@ -280,7 +280,7 @@ declare namespace webSocket { * @syscap SystemCapability.Communication.NetStack * @crossplatform * @atomicservice - * @since 11 dynamic + * @since 11 dynamic * @since 20 static */ export interface WebSocketCloseOptions { @@ -303,7 +303,7 @@ declare namespace webSocket { * @syscap SystemCapability.Communication.NetStack * @crossplatform * @atomicservice - * @since 11 dynamic + * @since 11 dynamic * @since 20 static */ code?: int; @@ -326,7 +326,7 @@ declare namespace webSocket { * @syscap SystemCapability.Communication.NetStack * @crossplatform * @atomicservice - * @since 11 dynamic + * @since 11 dynamic * @since 20 static */ reason?: string; @@ -345,7 +345,7 @@ declare namespace webSocket { * @syscap SystemCapability.Communication.NetStack * @crossplatform * @atomicservice - * @since 11 dynamic + * @since 11 dynamic * @since 20 static */ export interface CloseResult { @@ -362,7 +362,7 @@ declare namespace webSocket { * @syscap SystemCapability.Communication.NetStack * @crossplatform * @atomicservice - * @since 11 dynamic + * @since 11 dynamic * @since 20 static */ code: int; @@ -379,12 +379,36 @@ declare namespace webSocket { * @syscap SystemCapability.Communication.NetStack * @crossplatform * @atomicservice - * @since 11 dynamic + * @since 11 dynamic * @since 20 static */ reason: string; } + /** + * The result for connect status a WebSocket connection. + * @interface OpenResult + * @syscap SystemCapability.Communication.NetStack + * @since 20 static + */ + export interface OpenResult { + /** + * result status. + * @type {int} + * @syscap SystemCapability.Communication.NetStack + * @since 20 static + */ + status: int; + + /** + * result message. + * @type {string} + * @syscap SystemCapability.Communication.NetStack + * @since 20 static + */ + message: string; + } + /** * HTTP response headers. * @typedef { object } @@ -395,6 +419,14 @@ declare namespace webSocket { [k: string]: string | string[] | undefined; } + /** + * HTTP response headers. + * @typedef { object } + * @syscap SystemCapability.Communication.NetStack + * @since 20 static + */ + export type ResponseHeaders = Record; + /** *

Defines a WebSocket object. Before invoking WebSocket APIs, * you need to call webSocket.createWebSocket to create a WebSocket object.

@@ -417,7 +449,7 @@ declare namespace webSocket { * @syscap SystemCapability.Communication.NetStack * @crossplatform * @atomicservice - * @since 11 dynamic + * @since 11 dynamic * @since 20 static */ export interface WebSocket { @@ -471,7 +503,7 @@ declare namespace webSocket { * @syscap SystemCapability.Communication.NetStack * @crossplatform * @atomicservice - * @since 12 dynamic + * @since 12 dynamic * @since 20 static */ connect(url: string, callback: AsyncCallback): void; @@ -530,7 +562,7 @@ declare namespace webSocket { * @syscap SystemCapability.Communication.NetStack * @crossplatform * @atomicservice - * @since 12 dynamic + * @since 12 dynamic * @since 20 static */ connect(url: string, options: WebSocketRequestOptions, callback: AsyncCallback): void; @@ -589,7 +621,7 @@ declare namespace webSocket { * @syscap SystemCapability.Communication.NetStack * @crossplatform * @atomicservice - * @since 12 dynamic + * @since 12 dynamic * @since 20 static */ connect(url: string, options?: WebSocketRequestOptions): Promise; @@ -625,7 +657,7 @@ declare namespace webSocket { * @syscap SystemCapability.Communication.NetStack * @crossplatform * @atomicservice - * @since 11 dynamic + * @since 11 dynamic * @since 20 static */ send(data: string | ArrayBuffer, callback: AsyncCallback): void; @@ -661,7 +693,7 @@ declare namespace webSocket { * @syscap SystemCapability.Communication.NetStack * @crossplatform * @atomicservice - * @since 11 dynamic + * @since 11 dynamic * @since 20 static */ send(data: string | ArrayBuffer): Promise; @@ -694,7 +726,7 @@ declare namespace webSocket { * @syscap SystemCapability.Communication.NetStack * @crossplatform * @atomicservice - * @since 11 dynamic + * @since 11 dynamic * @since 20 static */ close(callback: AsyncCallback): void; @@ -730,7 +762,7 @@ declare namespace webSocket { * @syscap SystemCapability.Communication.NetStack * @crossplatform * @atomicservice - * @since 11 dynamic + * @since 11 dynamic * @since 20 static */ close(options: WebSocketCloseOptions, callback: AsyncCallback): void; @@ -766,7 +798,7 @@ declare namespace webSocket { * @syscap SystemCapability.Communication.NetStack * @crossplatform * @atomicservice - * @since 11 dynamic + * @since 11 dynamic * @since 20 static */ close(options?: WebSocketCloseOptions): Promise; @@ -823,6 +855,24 @@ declare namespace webSocket { */ off(type: 'open', callback?: AsyncCallback): void; + /** + * Enables listening for the open events of a WebSocket connection. + * @param { 'open' } type - event indicating that a WebSocket connection has been opened. + * @param { AsyncCallback } callback - the callback used to return the result. + * @syscap SystemCapability.Communication.NetStack + * @since 20 static + */ + on(type: 'open', callback: AsyncCallback): void; + + /** + * Cancels listening for the open events of a WebSocket connection. + * @param { 'open' } type - event indicating that a WebSocket connection has been opened. + * @param { AsyncCallback } callback - the callback used to return the result. + * @syscap SystemCapability.Communication.NetStack + * @since 20 static + */ + off(type: 'open', callback?: AsyncCallback): void; + /** * Enables listening for the message events of a WebSocket connection. * data in AsyncCallback can be a string(API 6) or an ArrayBuffer(API 8). @@ -849,6 +899,7 @@ declare namespace webSocket { * @crossplatform * @atomicservice * @since 11 dynamic + * @since 20 static */ on(type: 'message', callback: AsyncCallback): void; @@ -878,6 +929,7 @@ declare namespace webSocket { * @crossplatform * @atomicservice * @since 11 dynamic + * @since 20 static */ off(type: 'message', callback?: AsyncCallback): void; @@ -907,6 +959,7 @@ declare namespace webSocket { * @crossplatform * @atomicservice * @since 11 dynamic + * @since 20 static */ on(type: 'close', callback: AsyncCallback): void; @@ -936,6 +989,7 @@ declare namespace webSocket { * @crossplatform * @atomicservice * @since 11 dynamic + * @since 20 static */ off(type: 'close', callback?: AsyncCallback): void; @@ -962,6 +1016,7 @@ declare namespace webSocket { * @crossplatform * @atomicservice * @since 11 dynamic + * @since 20 static */ on(type: 'error', callback: ErrorCallback): void; @@ -988,6 +1043,7 @@ declare namespace webSocket { * @crossplatform * @atomicservice * @since 11 dynamic + * @since 20 static */ off(type: 'error', callback?: ErrorCallback): void; @@ -1005,6 +1061,7 @@ declare namespace webSocket { * @syscap SystemCapability.Communication.NetStack * @crossplatform * @since 12 dynamic + * @since 20 static */ on(type: 'dataEnd', callback: Callback): void; @@ -1022,6 +1079,7 @@ declare namespace webSocket { * @syscap SystemCapability.Communication.NetStack * @crossplatform * @since 12 dynamic + * @since 20 static */ off(type: 'dataEnd', callback?: Callback): void; @@ -1031,6 +1089,7 @@ declare namespace webSocket { * @param { Callback } callback - the callback used to return the result. * @syscap SystemCapability.Communication.NetStack * @since 12 dynamic + * @since 20 static */ on(type: 'headerReceive', callback: Callback): void; @@ -1040,6 +1099,7 @@ declare namespace webSocket { * @param { Callback } [callback] - the callback used to return the result. * @syscap SystemCapability.Communication.NetStack * @since 12 dynamic + * @since 20 static */ off(type: 'headerReceive', callback?: Callback): void; } @@ -1049,6 +1109,7 @@ declare namespace webSocket { * @returns { WebSocketServer } the WebSocketServer Object. * @syscap SystemCapability.Communication.NetStack * @since 19 dynamic + * @since 20 static */ function createWebSocketServer(): WebSocketServer; @@ -1057,6 +1118,7 @@ declare namespace webSocket { * @interface WebSocketServerConfig * @syscap SystemCapability.Communication.NetStack * @since 19 dynamic + * @since 20 static */ export interface WebSocketServerConfig { /** @@ -1065,6 +1127,7 @@ declare namespace webSocket { * @type {?string} * @syscap SystemCapability.Communication.NetStack * @since 19 dynamic + * @since 20 static */ serverIP?: string; @@ -1073,6 +1136,7 @@ declare namespace webSocket { * @type {number} * @syscap SystemCapability.Communication.NetStack * @since 19 dynamic + * @since 20 static */ serverPort: number; @@ -1081,6 +1145,7 @@ declare namespace webSocket { * @type {?ServerCert} * @syscap SystemCapability.Communication.NetStack * @since 19 dynamic + * @since 20 static */ serverCert?: ServerCert; @@ -1089,6 +1154,7 @@ declare namespace webSocket { * @type { number } * @syscap SystemCapability.Communication.NetStack * @since 19 dynamic + * @since 20 static */ maxConcurrentClientsNumber: number; @@ -1097,6 +1163,7 @@ declare namespace webSocket { * @type {?string} * @syscap SystemCapability.Communication.NetStack * @since 19 dynamic + * @since 20 static */ protocol?: string; @@ -1105,6 +1172,7 @@ declare namespace webSocket { * @type {number} * @syscap SystemCapability.Communication.NetStack * @since 19 dynamic + * @since 20 static */ maxConnectionsForOneClient: number; } @@ -1115,6 +1183,7 @@ declare namespace webSocket { * @interface ServerCert * @syscap SystemCapability.Communication.NetStack * @since 19 dynamic + * @since 20 static */ export interface ServerCert { /** @@ -1122,6 +1191,7 @@ declare namespace webSocket { * @type {string} * @syscap SystemCapability.Communication.NetStack * @since 19 dynamic + * @since 20 static */ certPath: string; @@ -1130,6 +1200,7 @@ declare namespace webSocket { * @type {string} * @syscap SystemCapability.Communication.NetStack * @since 19 dynamic + * @since 20 static */ keyPath: string; } @@ -1139,6 +1210,7 @@ declare namespace webSocket { * @interface WebSocketConnection * @syscap SystemCapability.Communication.NetStack * @since 19 dynamic + * @since 20 static */ export interface WebSocketConnection { /** @@ -1146,6 +1218,7 @@ declare namespace webSocket { * @type {string} * @syscap SystemCapability.Communication.NetStack * @since 19 dynamic + * @since 20 static */ clientIP: string; @@ -1154,6 +1227,7 @@ declare namespace webSocket { * @type {number} * @syscap SystemCapability.Communication.NetStack * @since 19 dynamic + * @since 20 static */ clientPort: number; } @@ -1163,6 +1237,7 @@ declare namespace webSocket { * @interface WebSocketMessage * @syscap SystemCapability.Communication.NetStack * @since 19 dynamic + * @since 20 static */ export interface WebSocketMessage { /** @@ -1170,6 +1245,7 @@ declare namespace webSocket { * @type {string | ArrayBuffer} * @syscap SystemCapability.Communication.NetStack * @since 19 dynamic + * @since 20 static */ data: string | ArrayBuffer; @@ -1178,6 +1254,7 @@ declare namespace webSocket { * @type {WebSocketConnection} * @syscap SystemCapability.Communication.NetStack * @since 19 dynamic + * @since 20 static */ clientConnection: WebSocketConnection; } @@ -1189,6 +1266,7 @@ declare namespace webSocket { * @param { CloseResult } closeReason - the error code and reason why the connection is closed. * @syscap SystemCapability.Communication.NetStack * @since 19 dynamic + * @since 20 static */ export type ClientConnectionCloseCallback = (clientConnection: WebSocketConnection, closeReason: CloseResult) => void; @@ -1198,6 +1276,7 @@ declare namespace webSocket { * @interface WebSocketServer * @syscap SystemCapability.Communication.NetStack * @since 19 dynamic + * @since 20 static */ export interface WebSocketServer { /** @@ -1212,6 +1291,7 @@ declare namespace webSocket { * @throws { BusinessError } 2302999 - Websocket other unknown error. * @syscap SystemCapability.Communication.NetStack * @since 19 dynamic + * @since 20 static */ start(config: WebSocketServerConfig): Promise; @@ -1222,6 +1302,7 @@ declare namespace webSocket { * @throws { BusinessError } 201 - Permission denied. * @syscap SystemCapability.Communication.NetStack * @since 19 dynamic + * @since 20 static */ listAllConnections(): WebSocketConnection[]; @@ -1235,6 +1316,7 @@ declare namespace webSocket { * @throws { BusinessError } 2302006 - websocket connection does not exist. * @syscap SystemCapability.Communication.NetStack * @since 19 dynamic + * @since 20 static */ close(connection: WebSocketConnection, options?: webSocket.WebSocketCloseOptions): Promise; @@ -1248,17 +1330,19 @@ declare namespace webSocket { * @throws { BusinessError } 2302006 - websocket connection does not exist. * @syscap SystemCapability.Communication.NetStack * @since 19 dynamic + * @since 20 static */ send(data: string | ArrayBuffer, connection: WebSocketConnection): Promise; - /** - * Stop listening. - * @permission ohos.permission.INTERNET - * @returns { Promise } The Indicating whether the server stops sucessfully. - * @throws { BusinessError } 201 - Permission denied. - * @syscap SystemCapability.Communication.NetStack - * @since 19 dynamic - */ + /** + * Stop listening. + * @permission ohos.permission.INTERNET + * @returns { Promise } The Indicating whether the server stops sucessfully. + * @throws { BusinessError } 201 - Permission denied. + * @syscap SystemCapability.Communication.NetStack + * @since 19 dynamic + * @since 20 static + */ stop(): Promise; /** @@ -1267,6 +1351,7 @@ declare namespace webSocket { * @param { ErrorCallback } callback - the callback used to return the result. * @syscap SystemCapability.Communication.NetStack * @since 19 dynamic + * @since 20 static */ on(type: 'error', callback: ErrorCallback): void; @@ -1276,26 +1361,29 @@ declare namespace webSocket { * @param { Callback } callback - the callback used to return the result. * @syscap SystemCapability.Communication.NetStack * @since 19 dynamic + * @since 20 static */ - on(type: 'connect', callback: Callback): void; + on(type: 'connect', callback: Callback): void; - /** - * Enables listening for events that the server received a message. - * @param { 'messageReceive' } type - event indicating that the server received a message. - * @param { Callback } callback - the callback used to return the result. - * @syscap SystemCapability.Communication.NetStack - * @since 19 dynamic - */ - on(type: 'messageReceive', callback: Callback): void; + /** + * Enables listening for events that the server received a message. + * @param { 'messageReceive' } type - event indicating that the server received a message. + * @param { Callback } callback - the callback used to return the result. + * @syscap SystemCapability.Communication.NetStack + * @since 19 dynamic + * @since 20 static + */ + on(type: 'messageReceive', callback: Callback): void; - /** - * Enables listening for events that a connection from a given client has been closed. - * @param { 'close' } type - event indicating that a connection from a given client has been closed. - * @param { ClientConnectionCloseCallback } callback - the callback function when a client connection is closed. - * @syscap SystemCapability.Communication.NetStack - * @since 19 dynamic - */ - on(type: 'close', callback: ClientConnectionCloseCallback): void; + /** + * Enables listening for events that a connection from a given client has been closed. + * @param { 'close' } type - event indicating that a connection from a given client has been closed. + * @param { ClientConnectionCloseCallback } callback - the callback function when a client connection is closed. + * @syscap SystemCapability.Communication.NetStack + * @since 19 dynamic + * @since 20 static + */ + on(type: 'close', callback: ClientConnectionCloseCallback): void; /** * Cancels listening for the error events of a WebSocket Server. @@ -1303,6 +1391,7 @@ declare namespace webSocket { * @param { ErrorCallback } callback - the callback used to return the result. * @syscap SystemCapability.Communication.NetStack * @since 19 dynamic + * @since 20 static */ off(type: 'error', callback?: ErrorCallback): void; @@ -1312,6 +1401,7 @@ declare namespace webSocket { * @param { Callback } callback - the callback used to return the result. * @syscap SystemCapability.Communication.NetStack * @since 19 dynamic + * @since 20 static */ off(type: 'connect', callback?: Callback): void; @@ -1321,6 +1411,7 @@ declare namespace webSocket { * @param { ClientConnectionCloseCallback } callback - the callback used to return the result. * @syscap SystemCapability.Communication.NetStack * @since 19 dynamic + * @since 20 static */ off(type: 'close', callback?: ClientConnectionCloseCallback): void; @@ -1330,6 +1421,7 @@ declare namespace webSocket { * @param { Callback } callback - the callback used to return the result. * @syscap SystemCapability.Communication.NetStack * @since 19 dynamic + * @since 20 static */ off(type: 'messageReceive', callback?: Callback): void; } -- Gitee