1# # @ohos.net.webSocket (WebSocket Connection) 2 3> **NOTE** 4> 5> The initial APIs of this module are supported since API version 6. Newly added APIs will be marked with a superscript to indicate their earliest API version. 6 7 8You can use WebSocket to establish a bidirectional connection between a server and a client. Before doing this, you need to use the <span name="createWebSocket">[createWebSocket](#websocketcreatewebsocket)</span> API to create a <span name="WebSocket">[WebSocket](#websocket)</span> object and then use the <span name="connect">[connect](#connected)</span> API to connect to the server. 9If the connection is successful, the client will receive a callback of the <span name="open">[open](#onopen)</span> event. Then, the client can communicate with the server using the <span name="sended">[send](#send)</span> API. 10When the server sends a message to the client, the client will receive a callback of the <span name="message">[message](#onmessage)</span> event. If the client no longer needs this connection, it can call the <span name="closed">[close](#close)</span> API to disconnect from the server. Then, the client will receive a callback of the <span name="closes">[close](#onclose)</span> event. 11 12If an error occurs in any of the preceding processes, the client will receive a callback of the <span name="error">[error](#onerror)</span> event. 13 14## Modules to Import 15 16```js 17import webSocket from '@ohos.net.webSocket'; 18``` 19 20## Examples 21 22```js 23import webSocket from '@ohos.net.webSocket'; 24import { BusinessError } from '@ohos.base'; 25 26let defaultIpAddress = "ws://"; 27let ws = webSocket.createWebSocket(); 28ws.on('open', (err:BusinessError, value: Object) => { 29 if (err != undefined) { 30 console.log(JSON.stringify(err)); 31 return; 32 } 33 // When receiving the on('open') event, the client can use the send() API to communicate with the server. 34 ws.send("Hello, server!", (err: BusinessError, value: boolean) => { 35 if (!err) { 36 console.log("send success"); 37 } else { 38 console.log("send fail, err:" + JSON.stringify(err)); 39 } 40 }); 41}); 42ws.on('message', (err: BusinessError, value: string) => { 43 console.log("on message, message:" + value); 44 // When receiving the `bye` message (the actual message name may differ) from the server, the client proactively disconnects from the server. 45 if (value === 'bye') { 46 ws.close((err: BusinessError, value: boolean) => { 47 if (!err) { 48 console.log("close success"); 49 } else { 50 console.log("close fail, err is " + JSON.stringify(err)); 51 } 52 }); 53 } 54}); 55ws.on('close', (err: BusinessError, value: webSocket.CloseResult) => { 56 console.log("on close, code is " + value.code + ", reason is " + value.reason); 57}); 58ws.on('error', (err: BusinessError) => { 59 console.log("on error, error:" + JSON.stringify(err)); 60}); 61ws.connect(defaultIpAddress, (err: BusinessError, value: boolean) => { 62 if (!err) { 63 console.log("connect success"); 64 } else { 65 console.log("connect fail, err:" + JSON.stringify(err)); 66 } 67 ws.close((err: BusinessError) => { 68 if (!err) { 69 console.log("close success"); 70 } else { 71 console.log("close fail, err is " + JSON.stringify(err)); 72 } 73 }); 74}); 75``` 76 77## <span name="websocketcreatewebsocket">webSocket.createWebSocket<sup>6+</sup></span> 78 79createWebSocket(): WebSocket 80 81Creates a WebSocket connection. You can use this API to create or close a WebSocket connection, send data over it, or enable or disable listening for the **open**, **close**, **message**, and **error** events. 82 83**System capability**: SystemCapability.Communication.NetStack 84 85**Return value** 86 87| Type | Description | 88| :---------------------------------- | :----------------------------------------------------------- | 89| [WebSocket](#websocket) | A **WebSocket** object, which contains the **connect**, **send**, **close**, **on**, or **off** method.| 90 91**Example** 92 93```js 94let ws: webSocket = webSocket.createWebSocket(); 95``` 96 97## <span name="websocket">WebSocket<sup>6+</sup></span> 98 99Defines a **WebSocket** object. Before invoking WebSocket APIs, you need to call [webSocket.createWebSocket](#websocketcreatewebsocket) to create a **WebSocket** object. 100 101### <span name="connected">connect<sup>6+</sup></span> 102 103connect(url: string, callback: AsyncCallback\<boolean\>): void 104 105Initiates a WebSocket request to establish a WebSocket connection to a given URL. This API uses an asynchronous callback to return the result. 106 107> **NOTE** 108> You can listen to **error** events to obtain the operation result. If an error occurs, the error code 200 will be returned. 109 110**Required permissions**: ohos.permission.INTERNET 111 112**System capability**: SystemCapability.Communication.NetStack 113 114**Parameters** 115 116| Name | Type | Mandatory| Description | 117| -------- | ------------------------ | ---- | ---------------------------- | 118| url | string | Yes | URL for establishing a WebSocket connection.| 119| callback | AsyncCallback\<boolean\> | Yes | Callback used to return the result. | 120 121**Error codes** 122 123| ID| Error Message | 124| ------- | ----------------------- | 125| 401 | Parameter error. | 126| 201 | Permission denied. | 127 128**Example** 129 130```js 131import webSocket from '@ohos.net.webSocket'; 132import { BusinessError } from '@ohos.base'; 133 134let ws = webSocket.createWebSocket(); 135let url = "ws://"; 136ws.connect(url, (err: BusinessError, value: boolean) => { 137 if (!err) { 138 console.log("connect success"); 139 } else { 140 console.log("connect fail, err:" + JSON.stringify(err)); 141 } 142}); 143``` 144 145### connect<sup>6+</sup> 146 147connect(url: string, options: WebSocketRequestOptions, callback: AsyncCallback\<boolean\>): void 148 149Initiates a WebSocket request carrying specified options to establish a WebSocket connection to a given URL. This API uses an asynchronous callback to return the result. 150 151> **NOTE** 152> You can listen to **error** events to obtain the operation result. If an error occurs, the error code 200 will be returned. 153 154**Required permissions**: ohos.permission.INTERNET 155 156**System capability**: SystemCapability.Communication.NetStack 157 158**Parameters** 159 160| Name | Type | Mandatory| Description | 161| -------- | ------------------------ | ---- | ------------------------------------------------------- | 162| url | string | Yes | URL for establishing a WebSocket connection. | 163| options | WebSocketRequestOptions | Yes | Request options. For details, see [WebSocketRequestOptions](#websocketrequestoptions).| 164| callback | AsyncCallback\<boolean\> | Yes | Callback used to return the result. | 165 166**Error codes** 167 168| ID| Error Message | 169| ------- | ----------------------- | 170| 401 | Parameter error. | 171| 201 | Permission denied. | 172 173**Example** 174 175```js 176import webSocket from '@ohos.net.webSocket'; 177import { BusinessError } from '@ohos.base'; 178 179let ws = webSocket.createWebSocket(); 180let header: Map<string, string> 181header.set("key", "value") 182header.set("key2", "value2") 183let url = "ws://" 184ws.connect(url, header as webSocket.WebSocketRequestOptions, (err: BusinessError, value: Object) => { 185 if (!err) { 186 console.log("connect success"); 187 } else { 188 console.log("connect fail, err:" + JSON.stringify(err)) 189 } 190}); 191``` 192 193### connect<sup>6+</sup> 194 195connect(url: string, options?: WebSocketRequestOptions): Promise\<boolean\> 196 197Initiates a WebSocket request carrying specified options to establish a WebSocket connection to a given URL. This API uses a promise to return the result. 198 199> **NOTE** 200> You can listen to **error** events to obtain the operation result. If an error occurs, the error code 200 will be returned. 201 202**Required permissions**: ohos.permission.INTERNET 203 204**System capability**: SystemCapability.Communication.NetStack 205 206**Parameters** 207 208| Name | Type | Mandatory| Description | 209| ------- | ----------------------- | ---- | ------------------------------------------------------- | 210| url | string | Yes | URL for establishing a WebSocket connection. | 211| options | WebSocketRequestOptions | No | Request options. For details, see [WebSocketRequestOptions](#websocketrequestoptions).| 212 213**Return value** 214 215| Type | Description | 216| :----------------- | :-------------------------------- | 217| Promise\<boolean\> | Promise used to return the result.| 218 219**Error codes** 220 221| ID| Error Message | 222| ------- | ----------------------- | 223| 401 | Parameter error. | 224| 201 | Permission denied. | 225 226**Example** 227 228```js 229import webSocket from '@ohos.net.webSocket'; 230 231let ws = webSocket.createWebSocket(); 232let url = "ws://" 233let promise = ws.connect(url); 234promise.then((value: boolean) => { 235 console.log("connect success") 236}).catch((err:string) => { 237 console.log("connect fail, error:" + JSON.stringify(err)) 238}); 239``` 240 241### <span name="send">send<sup>6+</sup></span> 242 243send(data: string | ArrayBuffer, callback: AsyncCallback\<boolean\>): void 244 245Sends data through a WebSocket connection. This API uses an asynchronous callback to return the result. 246 247**Required permissions**: ohos.permission.INTERNET 248 249**System capability**: SystemCapability.Communication.NetStack 250 251**Parameters** 252 253| Name | Type | Mandatory| Description | 254| -------- | ------------------------ | ---- | ------------ | 255| data | string \| ArrayBuffer | Yes | Data to send.<br>Only the string type is supported for API version 6 or earlier. Both the string and ArrayBuffer types are supported for API version 8 or later.| 256| callback | AsyncCallback\<boolean\> | Yes | Callback used to return the result. | 257 258**Error codes** 259 260| ID| Error Message | 261| ------- | ----------------------- | 262| 401 | Parameter error. | 263| 201 | Permission denied. | 264 265**Example** 266 267```js 268import webSocket from '@ohos.net.webSocket'; 269import { BusinessError } from '@ohos.base'; 270 271let ws = webSocket.createWebSocket(); 272let url = "ws://" 273ws.connect(url, (err: BusinessError, value: boolean) => { 274 ws.send("Hello, server!", (err: BusinessError, value: boolean) => { 275 if (!err) { 276 console.log("send success"); 277 } else { 278 console.log("send fail, err:" + JSON.stringify(err)) 279 } 280 }); 281}); 282``` 283 284### send<sup>6+</sup> 285 286send(data: string | ArrayBuffer): Promise\<boolean\> 287 288Sends data through a WebSocket connection. This API uses a promise to return the result. 289 290**Required permissions**: ohos.permission.INTERNET 291 292**System capability**: SystemCapability.Communication.NetStack 293 294**Parameters** 295 296| Name| Type | Mandatory| Description | 297| ------ | ------ | ---- | ------------ | 298| data | string \| ArrayBuffer | Yes | Data to send.<br>Only the string type is supported for API version 6 or earlier. Both the string and ArrayBuffer types are supported for API version 8 or later.| 299 300**Return value** 301 302| Type | Description | 303| :----------------- | :-------------------------------- | 304| Promise\<boolean\> | Promise used to return the result.| 305 306**Error codes** 307 308| ID| Error Message | 309| ------- | ----------------------- | 310| 401 | Parameter error. | 311| 201 | Permission denied. | 312 313**Example** 314 315```js 316import webSocket from '@ohos.net.webSocket'; 317import { BusinessError } from '@ohos.base'; 318 319let ws = webSocket.createWebSocket(); 320let url = "ws://" 321ws.connect(url, (err: BusinessError, value: boolean) => { 322 let promise = ws.send("Hello, server!"); 323 promise.then((value: boolean) => { 324 console.log("send success") 325 }).catch((err:string) => { 326 console.log("send fail, error:" + JSON.stringify(err)) 327 }); 328}); 329``` 330 331### <span name="close">close<sup>6+</sup></span> 332 333close(callback: AsyncCallback\<boolean\>): void 334 335Closes a WebSocket connection. This API uses an asynchronous callback to return the result. 336 337**Required permissions**: ohos.permission.INTERNET 338 339**System capability**: SystemCapability.Communication.NetStack 340 341**Parameters** 342 343| Name | Type | Mandatory| Description | 344| -------- | ------------------------ | ---- | ---------- | 345| callback | AsyncCallback\<boolean\> | Yes | Callback used to return the result.| 346 347**Error codes** 348 349| ID| Error Message | 350| ------- | ----------------------- | 351| 401 | Parameter error. | 352| 201 | Permission denied. | 353 354**Example** 355 356```js 357import webSocket from '@ohos.net.webSocket'; 358import { BusinessError } from '@ohos.base'; 359 360let ws = webSocket.createWebSocket(); 361ws.close((err: BusinessError) => { 362 if (!err) { 363 console.log("close success") 364 } else { 365 console.log("close fail, err is " + JSON.stringify(err)) 366 } 367}); 368``` 369 370### close<sup>6+</sup> 371 372close(options: WebSocketCloseOptions, callback: AsyncCallback\<boolean\>): void 373 374Closes a WebSocket connection carrying specified options such as **code** and **reason**. This API uses an asynchronous callback to return the result. 375 376**Required permissions**: ohos.permission.INTERNET 377 378**System capability**: SystemCapability.Communication.NetStack 379 380**Parameters** 381 382| Name | Type | Mandatory| Description | 383| -------- | ------------------------ | ---- | ----------------------------------------------------- | 384| options | WebSocketCloseOptions | Yes | Request options. For details, see [WebSocketCloseOptions](#websocketcloseoptions).| 385| callback | AsyncCallback\<boolean\> | Yes | Callback used to return the result. | 386 387**Error codes** 388 389| ID| Error Message | 390| ------- | ----------------------- | 391| 401 | Parameter error. | 392| 201 | Permission denied. | 393 394**Example** 395 396```js 397import webSocket from '@ohos.net.webSocket'; 398import { BusinessError } from '@ohos.base'; 399 400let ws = webSocket.createWebSocket(); 401 402let options: webSocket.WebSocketCloseOptions 403options.code = 1000 404options.reason = "your reason" 405ws.close(options, (err: BusinessError) => { 406 if (!err) { 407 console.log("close success") 408 } else { 409 console.log("close fail, err is " + JSON.stringify(err)) 410 } 411}); 412``` 413 414### close<sup>6+</sup> 415 416close(options?: WebSocketCloseOptions): Promise\<boolean\> 417 418Closes a WebSocket connection carrying specified options such as **code** and **reason**. This API uses a promise to return the result. 419 420**Required permissions**: ohos.permission.INTERNET 421 422**System capability**: SystemCapability.Communication.NetStack 423 424**Parameters** 425 426| Name | Type | Mandatory| Description | 427| ------- | --------------------- | ---- | ----------------------------------------------------- | 428| options | WebSocketCloseOptions | No | Request options. For details, see [WebSocketCloseOptions](#websocketcloseoptions).| 429 430**Return value** 431 432| Type | Description | 433| :----------------- | :-------------------------------- | 434| Promise\<boolean\> | Promise used to return the result.| 435 436**Error codes** 437 438| ID| Error Message | 439| ------- | ----------------------- | 440| 401 | Parameter error. | 441| 201 | Permission denied. | 442 443**Example** 444 445```js 446import webSocket from '@ohos.net.webSocket'; 447 448let ws = webSocket.createWebSocket(); 449let options: webSocket.WebSocketCloseOptions 450options.code = 1000 451options.reason = "your reason" 452let promise = ws.close(); 453promise.then((value: boolean) => { 454 console.log("close success") 455}).catch((err:string) => { 456 console.log("close fail, err is " + JSON.stringify(err)) 457}); 458``` 459 460### <span name="onopen">on('open')<sup>6+</sup></span> 461 462on(type: 'open', callback: AsyncCallback\<Object\>): void 463 464Enables listening for the **open** events of a WebSocket connection. This API uses an asynchronous callback to return the result. 465 466**System capability**: SystemCapability.Communication.NetStack 467 468**Parameters** 469 470| Name | Type | Mandatory| Description | 471| -------- | ----------------------- | ---- | ----------------------------- | 472| type | string | Yes | Event type. <br />**open**: event indicating that a WebSocket connection has been opened.| 473| callback | AsyncCallback\<Object\> | Yes | Callback used to return the result. | 474 475**Example** 476 477```js 478import webSocket from '@ohos.net.webSocket'; 479import { BusinessError, Callback } from '@ohos.base'; 480 481let ws= webSocket.createWebSocket(); 482class OutValue { 483 status: number = 0 484 message: string = "" 485} 486ws.on('open', (err: BusinessError, value: OutValue) => { 487 console.log("on open, status:" + value.status + ", message:" + value.message); 488}); 489``` 490 491### off('open')<sup>6+</sup> 492 493off(type: 'open', callback?: AsyncCallback\<Object\>): void 494 495Disables listening for the **open** events of a WebSocket connection. This API uses an asynchronous callback to return the result. 496 497> **NOTE** 498> You can pass the callback of the **on** function if you want to cancel listening for a certain type of event. If you do not pass the callback, you will cancel listening for all events. 499 500**System capability**: SystemCapability.Communication.NetStack 501 502**Parameters** 503 504| Name | Type | Mandatory| Description | 505| -------- | ----------------------- | ---- | ----------------------------- | 506| type | string | Yes | Event type. <br />**open**: event indicating that a WebSocket connection has been opened.| 507| callback | AsyncCallback\<Object\> | No | Callback used to return the result. | 508 509**Example** 510 511```js 512import webSocket from '@ohos.net.webSocket'; 513import { BusinessError } from '@ohos.base'; 514 515let ws = webSocket.createWebSocket(); 516class OutValue { 517 status: number = 0 518 message: string = "" 519} 520let callback1 = (err: BusinessError, value: OutValue) => { 521 console.log("on open, status:" + value.status + ", message:" + value.message); 522} 523ws.on('open', callback1); 524// You can pass the callback of the on function to cancel listening for a certain type of callback. If you do not pass the callback, you will cancel listening for all callbacks. 525ws.off('open', callback1); 526``` 527 528### <span name="onmessage">on('message')<sup>6+</sup></spam> 529 530on(type: 'message', callback: AsyncCallback\<string | ArrayBuffer\>): void 531 532Enables listening for the **message** events of a WebSocket connection. This API uses an asynchronous callback to return the result. The maximum length of each message is 4 KB. If the length exceeds 4 KB, the message is automatically fragmented. 533 534> **NOTE** 535> The data in **AsyncCallback** can be in the format of string (API version 6) or ArrayBuffer (API version 8). 536 537**System capability**: SystemCapability.Communication.NetStack 538 539**Parameters** 540 541| Name | Type | Mandatory| Description | 542| -------- | ----------------------- | ---- | -------------------------------------------- | 543| type | string | Yes | Event type.<br />**message**: event indicating that a message has been received from the server.| 544| callback | AsyncCallback\<string \| ArrayBuffer <sup>8+</sup>\> | Yes | Callback used to return the result. | 545 546**Example** 547 548```js 549import webSocket from '@ohos.net.webSocket'; 550import { BusinessError } from '@ohos.base'; 551 552let ws = webSocket.createWebSocket(); 553ws.on('message', (err: BusinessError, value: string) => { 554 console.log("on message, message:" + value); 555}); 556``` 557 558### off('message')<sup>6+</sup> 559 560off(type: 'message', callback?: AsyncCallback\<string | ArrayBuffer\>): void 561 562Disables listening for the **message** events of a WebSocket connection. This API uses an asynchronous callback to return the result. The maximum length of each message is 4 KB. If the length exceeds 4 KB, the message is automatically fragmented. 563 564> **NOTE** 565> The data in **AsyncCallback** can be in the format of string (API version 6) or ArrayBuffer (API version 8). 566> You can pass the callback of the **on** function if you want to cancel listening for a certain type of event. If you do not pass the callback, you will cancel listening for all events. 567 568**System capability**: SystemCapability.Communication.NetStack 569 570**Parameters** 571 572| Name | Type | Mandatory| Description | 573| -------- | --------------------------------------------------- | ---- | -------------------------------------------- | 574| type | string | Yes | Event type.<br />**message**: event indicating that a message has been received from the server.| 575| callback | AsyncCallback\<string \|ArrayBuffer <sup>8+</sup>\> | No | Callback used to return the result. | 576 577**Example** 578 579```js 580import webSocket from '@ohos.net.webSocket'; 581 582let ws = webSocket.createWebSocket(); 583ws.off('message'); 584``` 585 586### <span name="onclose">on('close')<sup>6+</sup></span> 587 588on(type: 'close', callback: AsyncCallback\<CloseResult\>): void 589 590Enables listening for the **close** events of a WebSocket connection. This API uses an asynchronous callback to return the result. 591 592**System capability**: SystemCapability.Communication.NetStack 593 594**Parameters** 595 596| Name | Type | Mandatory| Description | 597| -------- | ----------------------------------------------- | ---- | ------------------------------ | 598| type | string | Yes | Event type. <br />**close**: event indicating that a WebSocket connection has been closed.| 599| callback | AsyncCallback\<CloseResult\> | Yes | Callback used to return the result.<br>**close** indicates the close error code and **reason** indicates the error code description.| 600 601**Example** 602 603```js 604import webSocket from '@ohos.net.webSocket'; 605import { BusinessError } from '@ohos.base'; 606 607let ws = webSocket.createWebSocket(); 608ws.on('close', (err: BusinessError, value: webSocket.CloseResult) => { 609 console.log("on close, code is " + value.code + ", reason is " + value.reason); 610}); 611``` 612 613### off('close')<sup>6+</sup> 614 615off(type: 'close', callback?: AsyncCallback\<CloseResult\>): void 616 617Disables listening for the **close** events of a WebSocket connection. This API uses an asynchronous callback to return the result. 618 619> **NOTE** 620> You can pass the callback of the **on** function if you want to cancel listening for a certain type of event. If you do not pass the callback, you will cancel listening for all events. 621 622**System capability**: SystemCapability.Communication.NetStack 623 624**Parameters** 625 626| Name | Type | Mandatory| Description | 627| -------- | ----------------------------------------------- | ---- | ------------------------------ | 628| type | string | Yes | Event type. <br />**close**: event indicating that a WebSocket connection has been closed.| 629| callback | AsyncCallback\<CloseResult\> | No | Callback used to return the result.<br>**close** indicates the close error code and **reason** indicates the error code description.| 630 631**Example** 632 633```js 634import webSocket from '@ohos.net.webSocket'; 635 636let ws = webSocket.createWebSocket(); 637ws.off('close'); 638``` 639 640### <span name="onerror">on('error')<sup>6+</sup></span> 641 642on(type: 'error', callback: ErrorCallback): void 643 644Enables listening for the **error** events of a WebSocket connection. This API uses an asynchronous callback to return the result. 645 646**System capability**: SystemCapability.Communication.NetStack 647 648**Parameters** 649 650| Name | Type | Mandatory| Description | 651| -------- | ------------- | ---- | ------------------------------- | 652| type | string | Yes | Event type.<br />**error**: event indicating the WebSocket connection has encountered an error.| 653| callback | ErrorCallback | Yes | Callback used to return the result.<br>Common error code: 200| 654 655**Example** 656 657```js 658import webSocket from '@ohos.net.webSocket'; 659import { BusinessError } from '@ohos.base'; 660 661let ws = webSocket.createWebSocket(); 662ws.on('error', (err: BusinessError) => { 663 console.log("on error, error:" + JSON.stringify(err)) 664}); 665``` 666 667### off('error')<sup>6+</sup> 668 669off(type: 'error', callback?: ErrorCallback): void 670 671Disables listening for the **error** events of a WebSocket connection. This API uses an asynchronous callback to return the result. 672 673> **NOTE** 674> You can pass the callback of the **on** function if you want to cancel listening for a certain type of event. If you do not pass the callback, you will cancel listening for all events. 675 676**System capability**: SystemCapability.Communication.NetStack 677 678**Parameters** 679 680| Name | Type | Mandatory| Description | 681| -------- | ------------- | ---- | ------------------------------- | 682| type | string | Yes | Event type.<br />**error**: event indicating the WebSocket connection has encountered an error.| 683| callback | ErrorCallback | No | Callback used to return the result. | 684 685**Example** 686 687```js 688import webSocket from '@ohos.net.webSocket'; 689let ws = webSocket.createWebSocket(); 690ws.off('error'); 691``` 692 693### on('dataEnd')<sup>11+</sup> 694 695on(type: 'dataEnd', callback: Callback\<void\>): void 696 697Enables listening for the **dataEnd** events of a WebSocket connection. This API uses an asynchronous callback to return the result. 698 699**System capability**: SystemCapability.Communication.NetStack 700 701**Parameters** 702 703| Name | Type | Mandatory| Description | 704| -------- | ---------------- | ---- | --------------------------------------- | 705| type | string | Yes | Event type.<br />**dataEnd**: event indicating the data receiving over the WebSocket connection has ended.| 706| callback | Callback\<void\> | Yes | Callback used to return the result. | 707 708**Example** 709 710```js 711import webSocket from '@ohos.net.webSocket'; 712import { BusinessError } from '@ohos.base'; 713 714let ws = webSocket.createWebSocket(); 715ws.on('dataEnd', (err: BusinessError) => { 716 console.log("on dataEnd, error:" + JSON.stringify(err)) 717}); 718``` 719 720### off('dataEnd')<sup>11+</sup> 721 722off(type: 'dataEnd', callback?: Callback\<void\>): void 723 724Disables listening for the **dataEnd** events of a WebSocket connection. This API uses an asynchronous callback to return the result. 725 726> **NOTE** 727> You can pass the callback of the **on** function if you want to cancel listening for a certain type of event. If you do not pass the callback, you will cancel listening for all events. 728 729**System capability**: SystemCapability.Communication.NetStack 730 731**Parameters** 732 733| Name | Type | Mandatory| Description | 734| -------- | ---------------- | ---- | -------------------------------------- | 735| type | string | Yes | Event type.<br />**dataEnd**: event indicating the data receiving over the WebSocket connection has ended.| 736| callback | Callback\<void\> | No | Callback used to return the result. | 737 738**Example** 739 740```js 741import webSocket from '@ohos.net.webSocket'; 742let ws = webSocket.createWebSocket(); 743ws.off('dataEnd'); 744``` 745 746## WebSocketRequestOptions 747 748Defines the optional parameters carried in the request for establishing a WebSocket connection. 749 750**System capability**: SystemCapability.Communication.NetStack 751 752| Name| Type | Mandatory| Description | 753| ------ | ------ | ---- | ------------------------------------------------------------ | 754| header | Object | No | Header carrying optional parameters in the request for establishing a WebSocket connection. You can customize the parameter or leave it unspecified.| 755 756## WebSocketCloseOptions 757 758Defines the optional parameters carried in the request for closing a WebSocket connection. 759 760**System capability**: SystemCapability.Communication.NetStack 761 762| Name| Type | Mandatory| Description | 763| ------ | ------ | ---- | ------------------------------------------------------------ | 764| code | number | No | Error code. Set this parameter based on the actual situation. The default value is **1000**.| 765| reason | string | No | Error cause. Set this parameter based on the actual situation. The default value is an empty string ("").| 766 767## CloseResult<sup>10+</sup> 768 769Represents the result obtained from the **close** event reported when the WebSocket connection is closed. 770 771**System capability**: SystemCapability.Communication.NetStack 772 773| Name| Type | Mandatory| Description | 774| ------ | ------ | ---- | ------------------------------------------------------------ | 775| code | number | Yes | Error code for closing the connection.| 776| reason | string | Yes | Error cause for closing the connection.| 777 778## Result Codes for Closing a WebSocket Connection 779 780You can customize the result codes sent to the server. The result codes in the following table are for reference only. 781 782**System capability**: SystemCapability.Communication.NetStack 783 784| Value | Description | 785| :-------- | :----------------- | 786| 1000 | Normally closed. | 787| 1001 | Connection closed by the server. | 788| 1002 | Incorrect protocol. | 789| 1003 | Data unable to be processed.| 790| 1004~1015 | Reserved. | 791