openharmony 鸿蒙 capi-net-websocket-h

2026-08-25 浏览 (1)

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

NameDescription
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

NameDescription
WebSocket_OnOpenCallback onOpenCallback invoked when the WebSocket client receives an open message.
WebSocket_OnMessageCallback onMessageCallback invoked when the WebSocket client receives a Message message.
WebSocket_OnErrorCallback onErrorCallback invoked when the WebSocket client receives an error message.
WebSocket_OnCloseCallback oncloseCallback invoked when the WebSocket client receives a close message.

Returns

TypeDescription
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

NameDescription
struct WebSocket *clientPointer to the WebSocket client.
struct WebSocket_Header headerHeader information.

Returns

TypeDescription
int0 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

NameDescription
struct WebSocket *clientPointer to the WebSocket client.
const char *urlIP address for the WebSocket client to connect to the server.
struct WebSocket_RequestOptions optionsOptional parameters for connection establishment.

Returns

TypeDescription
int0 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

NameDescription
struct WebSocket *clientWebSocket client.
char *dataData sent by the WebSocket client.
size_t lengthLength of the data sent by the WebSocket client.

Returns

TypeDescription
int0 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

NameDescription
struct WebSocket *clientWebSocket client.
struct WebSocket_CloseOption optionsOptional parameters for connection closure.

Returns

TypeDescription
int0 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:

  1. Call WebSocket_OnCloseCallback to subscribe to the WebSocket connection close event, and call the OH_WebSocketClient_Destroy method in the callback function.

  2. 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

NameDescription
struct WebSocket *clientWebSocket client.

Returns

TypeDescription
int0 if the operation is successful; a non-0 value otherwise. For details about the return values, see OH_Websocket_ErrCode.

你可能感兴趣的鸿蒙文章

openharmony 鸿蒙 capi-netconnection-netconn-connectionproperties

openharmony 鸿蒙 errorcode-net-mdns

openharmony 鸿蒙 js-apis-net-ethernet-sys

openharmony 鸿蒙 errorcode-net-http

openharmony 鸿蒙 capi-netstack-http-proxy

openharmony 鸿蒙 js-apis-system-fetch

openharmony 鸿蒙 errorcode-kernel

openharmony 鸿蒙 capi-netstack-netstack-certblob

openharmony 鸿蒙 js-apis-net-mdns

openharmony 鸿蒙 capi-netstack-websocket-closeoption

  • 所属分类: 后端技术
  • 本文标签: 软件 鸿蒙
  • 版权声明: 本文链接 https://seaxiang.com/blog/c2ILgR6h