>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.
>
>Newly added APIs are defined but not implemented in OpenHarmony 3.1 Release. They will be available for use in OpenHarmony 3.1 MR.
You can use WebSocket to establish a bidirectional connection between a server and a client. Before doing this, you need to use the [createWebSocket](#websocketcreatewebsocket) API to create a [WebSocket](#websocket) object and then use the [connect](#connect) API to connect to the server. If the connection is successful, the client will receive a callback of the [open](#onopen) event. Then, the client can communicate with the server using the [send](#send) API. When the server sends a message to the client, the client will receive a callback of the [message](#onmessage) event. If the client no longer needs this connection, it can call the [close](#close) API to disconnect from the server. Then, the client will receive a callback of the [close](#onclose) event.
...
...
@@ -12,13 +11,13 @@ If an error occurs in any of the preceding processes, the client will receive a
## Modules to Import
```
```js
importwebSocketfrom'@ohos.net.webSocket';
```
## Complete Example
```
```js
importwebSocketfrom'@ohos.net.webSocket';
vardefaultIpAddress="ws://";
...
...
@@ -78,7 +77,7 @@ Creates a WebSocket connection. You can use this API to create or close a WebSoc
**Example**
```
```js
letws=webSocket.createWebSocket();
```
...
...
@@ -107,7 +106,7 @@ Initiates a WebSocket request to establish a WebSocket connection to a given URL
**Example**
```
```js
letws=webSocket.createWebSocket();
leturl="ws://"
ws.connect(url,(err,value)=>{
...
...
@@ -141,7 +140,7 @@ Initiates a WebSocket request carrying specified options to establish a WebSocke
**Example**
```
```js
letws=webSocket.createWebSocket();
leturl="ws://"
ws.connect(url,{
...
...
@@ -184,7 +183,7 @@ Initiates a WebSocket request carrying specified options to establish a WebSocke
**Example**
```
```js
letws=webSocket.createWebSocket();
leturl="ws://"
letpromise=ws.connect(url);
...
...
@@ -215,7 +214,7 @@ Sends data through a WebSocket connection. This API uses an asynchronous callbac
**Example**
```
```js
letws=webSocket.createWebSocket();
leturl="ws://"
ws.connect(url,(err,value)=>{
...
...
@@ -254,7 +253,7 @@ Sends data through a WebSocket connection. This API uses a promise to return the
**Example**
```
```js
letws=webSocket.createWebSocket();
leturl="ws://"
ws.connect(url,(err,value)=>{
...
...
@@ -286,7 +285,7 @@ Closes a WebSocket connection. This API uses an asynchronous callback to return
**Example**
```
```js
letws=webSocket.createWebSocket();
leturl="ws://"
ws.close((err,value)=>{
...
...
@@ -318,7 +317,7 @@ Closes a WebSocket connection carrying specified options such as **code** and **
**Example**
```
```js
letws=webSocket.createWebSocket();
leturl="ws://"
ws.close({
...
...
@@ -358,7 +357,7 @@ Closes a WebSocket connection carrying specified options such as **code** and **
**Example**
```
```js
letws=webSocket.createWebSocket();
leturl="ws://"
letpromise=ws.close({
...
...
@@ -391,7 +390,7 @@ Enables listening for the **open** events of a WebSocket connection. This API us