Skip to content

Commit

Permalink
add: websocket支持自定义keepalive长度,单位秒, 并整理代码
Browse files Browse the repository at this point in the history
  • Loading branch information
wendal committed Apr 28, 2024
1 parent 9a594f9 commit d819d11
Show file tree
Hide file tree
Showing 3 changed files with 24 additions and 19 deletions.
23 changes: 12 additions & 11 deletions components/network/websocket/luat_lib_websocket.c
Expand Up @@ -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);
Expand Down Expand Up @@ -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链接
Expand All @@ -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)
{
Expand All @@ -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;
Expand Down Expand Up @@ -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)}
};

Expand Down
12 changes: 8 additions & 4 deletions components/network/websocket/luat_websocket.c
Expand Up @@ -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

Expand Down Expand Up @@ -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;
}

Expand Down Expand Up @@ -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) {
Expand Down
8 changes: 4 additions & 4 deletions components/network/websocket/luat_websocket.h
Expand Up @@ -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
Expand All @@ -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 */
Expand All @@ -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

0 comments on commit d819d11

Please sign in to comment.