1 /* 2 * Copyright (C) 2023 Huawei Device Co., Ltd. 3 * Licensed under the Apache License, Version 2.0 (the "License"); 4 * you may not use this file except in compliance with the License. 5 * You may obtain a copy of the License at 6 * 7 * http://www.apache.org/licenses/LICENSE-2.0 8 * 9 * Unless required by applicable law or agreed to in writing, software 10 * distributed under the License is distributed on an "AS IS" BASIS, 11 * WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied. 12 * See the License for the specific language governing permissions and 13 * limitations under the License. 14 */ 15 16 /** 17 * @addtogroup netstack 18 * @{ 19 * 20 * @brief Provides C APIs for the websocket client module. 21 22 * @since 11 23 * @version 1.0 24 */ 25 26 /** 27 * @file net_websocket.h 28 * 29 * @brief Defines the APIs for the websocket client module. 30 * 31 * @library libnet_websocket.so 32 * @kit NetworkKit 33 * @syscap SystemCapability.Communication.NetStack 34 * @since 11 35 * @version 1.0 36 */ 37 38 #ifndef NET_WEBSOCKET_H 39 #define NET_WEBSOCKET_H 40 41 #include <signal.h> 42 #include <stdint.h> 43 #include <string.h> 44 45 #include "net_websocket_type.h" 46 47 #ifdef __cplusplus 48 extern "C" { 49 #endif 50 51 /** 52 * @brief Constructor of websocket. 53 * 54 * @param onOpen Callback function invoked when a connection setup message is received. 55 * @param onMessage Callback function invoked when a message is received. 56 * @param onError Callback function invoked when a connection error message is received. 57 * @param onClose Callback function invoked when a connection closing message is closed. 58 * @return Pointer to the websocket client if success; NULL otherwise. 59 * @syscap SystemCapability.Communication.NetStack 60 * @since 11 61 * @version 1.0 62 */ 63 struct WebSocket *OH_WebSocketClient_Constructor(WebSocket_OnOpenCallback onOpen, WebSocket_OnMessageCallback onMessage, 64 WebSocket_OnErrorCallback onError, WebSocket_OnCloseCallback onclose); 65 66 /** 67 * @brief Adds the header information to the client request. 68 * 69 * @param client Pointer to the websocket client. 70 * @param header Header information 71 * @return 0 if success; non-0 otherwise. For details about error codes, see {@link OH_Websocket_ErrCode}. 72 * @syscap SystemCapability.Communication.NetStack 73 * @since 11 74 * @version 1.0 75 */ 76 int OH_WebSocketClient_AddHeader(struct WebSocket *client, struct WebSocket_Header header); 77 78 /** 79 * @brief Connects the client to the server. 80 * 81 * @param client Pointer to the websocket client. 82 * @param url URL for the client to connect to the server. 83 * @param options Optional parameters. 84 * @return 0 if success; non-0 otherwise. For details about error codes, see {@link OH_Websocket_ErrCode}. 85 * @permission ohos.permission.INTERNET 86 * @syscap SystemCapability.Communication.NetStack 87 * @since 11 88 * @version 1.0 89 */ 90 int OH_WebSocketClient_Connect(struct WebSocket *client, const char *url, struct WebSocket_RequestOptions options); 91 92 /** 93 * @brief Sends data from the client to the server. 94 * 95 * @param client Pointer to the websocket client. 96 * @param data Data sent by the client. 97 * @param length Length of the data sent by the client. 98 * @return 0 if success; non-0 otherwise. For details about error codes, see {@link OH_Websocket_ErrCode}. 99 * @permission ohos.permission.INTERNET 100 * @syscap SystemCapability.Communication.NetStack 101 * @since 11 102 * @version 1.0 103 */ 104 int OH_WebSocketClient_Send(struct WebSocket *client, char *data, size_t length); 105 106 /** 107 * @brief Closes a webSocket connection. 108 * 109 * @param client Pointer to the websocket client. 110 * @param options Optional parameters. 111 * @return 0 if success; non-0 otherwise. For details about error codes, see {@link OH_Websocket_ErrCode}. 112 * @permission ohos.permission.INTERNET 113 * @syscap SystemCapability.Communication.NetStack 114 * @since 11 115 * @version 1.0 116 */ 117 int OH_WebSocketClient_Close(struct WebSocket *client, struct WebSocket_CloseOption options); 118 119 /** 120 * @brief Releases the context and resources of the websocket connection. 121 * 122 * @param client Pointer to the websocket client. 123 * @return 0 if success; non-0 otherwise. For details about error codes, see {@link OH_Websocket_ErrCode}. 124 * @permission ohos.permission.INTERNET 125 * @syscap SystemCapability.Communication.NetStack 126 * @since 11 127 * @version 1.0 128 */ 129 int OH_WebSocketClient_Destroy(struct WebSocket *client); 130 131 #ifdef __cplusplus 132 } 133 #endif 134 135 #endif // NET_WEBSOCKET_H 136 /** @} */ 137 138