diff --git a/components/network/websocket/luat_lib_websocket.c b/components/network/websocket/luat_lib_websocket.c index 781005e3..a6f83da4 100644 --- a/components/network/websocket/luat_lib_websocket.c +++ b/components/network/websocket/luat_lib_websocket.c @@ -55,7 +55,7 @@ static luat_websocket_ctrl_t *get_websocket_ctrl(lua_State *L) } } -static int32_t l_websocket_callback(lua_State *L, void *ptr) +static int l_websocket_callback(lua_State *L, void *ptr) { (void)ptr; rtos_msg_t *msg = (rtos_msg_t *)lua_topointer(L, -1); @@ -192,9 +192,10 @@ static int l_websocket_set_debug(lua_State *L) /* websocket客户端创建 -@api websocket.create(adapter, url) +@api websocket.create(adapter, url, keepalive) @int 适配器序号, 只能是socket.ETH0, socket.STA, socket.AP,如果不填,会选择平台自带的方式,然后是最后一个注册的适配器 @string 连接字符串,参考usage +@int 心跳间隔,默认60秒. 2024.4.28新增 @return userdata 若成功会返回websocket客户端实例,否则返回nil @usage -- 普通TCP链接 @@ -210,6 +211,14 @@ static int l_websocket_create(lua_State *L) { return 0; } + // 连接参数相关 + luat_websocket_connopts_t opts = {0}; + size_t ip_len = 0; + opts.url = luaL_checklstring(L, 2, &ip_len); + if (lua_isinteger(L, 3)) { + opts.keepalive = luaL_checkinteger(L, 3); + } + luat_websocket_ctrl_t *websocket_ctrl = (luat_websocket_ctrl_t *)lua_newuserdata(L, sizeof(luat_websocket_ctrl_t)); if (!websocket_ctrl) { @@ -224,15 +233,8 @@ static int l_websocket_create(lua_State *L) return 0; } - luat_websocket_connopts_t opts = {0}; - - // 连接参数相关 - // const char *ip; - size_t ip_len = 0; network_set_ip_invaild(&websocket_ctrl->ip_addr); - opts.url = luaL_checklstring(L, 2, &ip_len); - - ret = luat_websocket_set_connopts(websocket_ctrl, luaL_checklstring(L, 2, &ip_len)); + ret = luat_websocket_set_connopts(websocket_ctrl, &opts); if (ret){ luat_websocket_release_socket(websocket_ctrl); return 0; @@ -519,7 +521,6 @@ const rotable_Reg_t reg_websocket[] = {"ready", ROREG_FUNC(l_websocket_ready)}, {"headers", ROREG_FUNC(l_websocket_headers)}, {"debug", ROREG_FUNC(l_websocket_set_debug)}, - {NULL, ROREG_INT(0)} }; diff --git a/components/network/websocket/luat_websocket.c b/components/network/websocket/luat_websocket.c index 305ddec1..c3eedaaf 100644 --- a/components/network/websocket/luat_websocket.c +++ b/components/network/websocket/luat_websocket.c @@ -161,11 +161,11 @@ int luat_websocket_init(luat_websocket_ctrl_t *websocket_ctrl, int adapter_index return 0; } -int luat_websocket_set_connopts(luat_websocket_ctrl_t *websocket_ctrl, const char *url) +int luat_websocket_set_connopts(luat_websocket_ctrl_t *websocket_ctrl, luat_websocket_connopts_t* opts) { int is_tls = 0; - const char *tmp = url; - LLOGD("url %s", url); + const char *tmp = opts->url; + LLOGD("url %s", tmp); // TODO 支持基本授权的URL ws://wendal:123@wendal.cn:8080/abc @@ -248,6 +248,10 @@ int luat_websocket_set_connopts(luat_websocket_ctrl_t *websocket_ctrl, const cha { network_deinit_tls(websocket_ctrl->netc); } + + if (opts->keepalive > 0) { + websocket_ctrl->keepalive = opts->keepalive; + } return 0; } @@ -644,7 +648,7 @@ int luat_websocket_connect(luat_websocket_ctrl_t *websocket_ctrl) return 0; } -int luat_websocket_set_headers(luat_websocket_ctrl_t *websocket_ctrl, const char *headers) { +int luat_websocket_set_headers(luat_websocket_ctrl_t *websocket_ctrl, char *headers) { if (websocket_ctrl == NULL) return 0; if (websocket_ctrl->headers != NULL) { diff --git a/components/network/websocket/luat_websocket.h b/components/network/websocket/luat_websocket.h index e7b757d1..b2786edb 100644 --- a/components/network/websocket/luat_websocket.h +++ b/components/network/websocket/luat_websocket.h @@ -41,7 +41,7 @@ typedef struct typedef struct luat_websocket_connopts { const char *url; - uint16_t is_tls; + uint16_t keepalive; } luat_websocket_connopts_t; typedef struct luat_websocket_pkg @@ -51,7 +51,7 @@ typedef struct luat_websocket_pkg uint8_t R; uint8_t mark; uint16_t plen; // 最多支持64k - char *payload; + const char *payload; } luat_websocket_pkg_t; #define WebSocket_OP_CONTINUE 0x0 /* 0000 - continue frame */ @@ -70,8 +70,8 @@ void luat_websocket_release_socket(luat_websocket_ctrl_t *websocket_ctrl); void luat_websocket_ping(luat_websocket_ctrl_t *websocket_ctrl); void luat_websocket_reconnect(luat_websocket_ctrl_t *websocket_ctrl); int luat_websocket_init(luat_websocket_ctrl_t *websocket_ctrl, int adapter_index); -int luat_websocket_set_connopts(luat_websocket_ctrl_t *websocket_ctrl, const char *url); +int luat_websocket_set_connopts(luat_websocket_ctrl_t *websocket_ctrl, luat_websocket_connopts_t* opts); int luat_websocket_payload(char *buff, luat_websocket_pkg_t *pkg, size_t limit); int luat_websocket_send_frame(luat_websocket_ctrl_t *websocket_ctrl, luat_websocket_pkg_t *pkg); -int luat_websocket_set_headers(luat_websocket_ctrl_t *websocket_ctrl, const char *headers); +int luat_websocket_set_headers(luat_websocket_ctrl_t *websocket_ctrl, char *headers); #endif