net_websocket.h
Overview
Defines the WebSocket client module APIs.
File to include: <network/netstack/net_websocket.h>
Library: libnet_websocket.so
System capability: SystemCapability.Communication.NetStack
Since: 11
Related module: netstack
Summary
Functions
| Name | Description |
|---|---|
| struct WebSocket *OH_WebSocketClient_Constructor(WebSocket_OnOpenCallback onOpen, WebSocket_OnMessageCallback onMessage,WebSocket_OnErrorCallback onError, WebSocket_OnCloseCallback onclose) | Constructor used to create a WebSocket client. |
| int OH_WebSocketClient_AddHeader(struct WebSocket *client, struct WebSocket_Header header) | Adds the header information to the client request. |
| int OH_WebSocketClient_Connect(struct WebSocket *client, const char *url, struct WebSocket_RequestOptions options) | Connects the WebSocket client to the server. |
| int OH_WebSocketClient_Send(struct WebSocket *client, char *data, size_t length) | Sends data from the WebSocket client to the server. |
| int OH_WebSocketClient_Close(struct WebSocket *client, struct WebSocket_CloseOption options) | Closes the connection on the WebSocket client. |
| int OH_WebSocketClient_Destroy(struct WebSocket *client) | Destroys the WebSocket client and releases the context and resources of the WebSocket connection. |
Function Description
OH_WebSocketClient_Constructor()
struct WebSocket *OH_WebSocketClient_Constructor(WebSocket_OnOpenCallback onOpen, WebSocket_OnMessageCallback onMessage,WebSocket_OnErrorCallback onError, WebSocket_OnCloseCallback onclose)
Description
Constructor used to create a WebSocket client.
System capability: SystemCapability.Communication.NetStack
Since: 11
Parameters
| Name | Description |
|---|---|
| WebSocket_OnOpenCallback onOpen | Callback invoked when the WebSocket client receives an open message. |
| WebSocket_OnMessageCallback onMessage | Callback invoked when the WebSocket client receives a Message message. |
| WebSocket_OnErrorCallback onError | Callback invoked when the WebSocket client receives an error message. |
| WebSocket_OnCloseCallback onclose | Callback invoked when the WebSocket client receives a close message. |
Returns
| Type | Description |
|---|---|
| struct WebSocket * | Pointer to the WebSocket client if the operation is successful; NULL otherwise. |
OH_WebSocketClient_AddHeader()
int OH_WebSocketClient_AddHeader(struct WebSocket *client, struct WebSocket_Header header)
Description
Adds the header information to the client request.
System capability: SystemCapability.Communication.NetStack
Since: 11
Parameters
| Name | Description |
|---|---|
| struct WebSocket *client | Pointer to the WebSocket client. |
| struct WebSocket_Header header | Header information. |
Returns
| Type | Description |
|---|---|
| int | 0 if the operation is successful; a non-0 value otherwise. For details about the return values, see OH_Websocket_ErrCode. |
OH_WebSocketClient_Connect()
int OH_WebSocketClient_Connect(struct WebSocket *client, const char *url, struct WebSocket_RequestOptions options)
Description
Connects the WebSocket client to the server.
System capability: SystemCapability.Communication.NetStack
Required permission: ohos.permission.INTERNET
Since: 11
Parameters
| Name | Description |
|---|---|
| struct WebSocket *client | Pointer to the WebSocket client. |
| const char *url | IP address for the WebSocket client to connect to the server. |
| struct WebSocket_RequestOptions options | Optional parameters for connection establishment. |
Returns
| Type | Description |
|---|---|
| int | 0 if the operation is successful; a non-0 value otherwise. For details about the return values, see OH_Websocket_ErrCode. |
OH_WebSocketClient_Send()
int OH_WebSocketClient_Send(struct WebSocket *client, char *data, size_t length)
Description
Sends data from the WebSocket client to the server.
System capability: SystemCapability.Communication.NetStack
Required permission: ohos.permission.INTERNET
Since: 11
Parameters
| Name | Description |
|---|---|
| struct WebSocket *client | WebSocket client. |
| char *data | Data sent by the WebSocket client. |
| size_t length | Length of the data sent by the WebSocket client. |
Returns
| Type | Description |
|---|---|
| int | 0 if the operation is successful; a non-0 value otherwise. For details about the return values, see OH_Websocket_ErrCode. |
OH_WebSocketClient_Close()
int OH_WebSocketClient_Close(struct WebSocket *client, struct WebSocket_CloseOption options)
Description
Closes the connection on the WebSocket client.
System capability: SystemCapability.Communication.NetStack
Required permission: ohos.permission.INTERNET
Since: 11
Parameters
| Name | Description |
|---|---|
| struct WebSocket *client | WebSocket client. |
| struct WebSocket_CloseOption options | Optional parameters for connection closure. |
Returns
| Type | Description |
|---|---|
| int | 0 if the operation is successful; a non-0 value otherwise. For details about the return values, see OH_Websocket_ErrCode. |
OH_WebSocketClient_Destroy()
int OH_WebSocketClient_Destroy(struct WebSocket *client)
Description
Destroys the WebSocket client and releases the context and resources of the WebSocket connection. Usage:
-
Call WebSocket_OnCloseCallback to subscribe to the WebSocket connection close event, and call the OH_WebSocketClient_Destroy method in the callback function.
-
Call OH_WebSocketClient_Close to close the WebSocket connection.
NOTE
Ensure that this API is called after the WebSocket_OnCloseCallback callback is triggered. Otherwise, socket leaks may occur and the connection may not be closed after the system memory resources are released.
System capability: SystemCapability.Communication.NetStack
Required permission: ohos.permission.INTERNET
Since: 11
Parameters
| Name | Description |
|---|---|
| struct WebSocket *client | WebSocket client. |
Returns
| Type | Description |
|---|---|
| int | 0 if the operation is successful; a non-0 value otherwise. For details about the return values, see OH_Websocket_ErrCode. |