From 6075285b5c4e96ce23a4e3473c30906c15e91052 Mon Sep 17 00:00:00 2001 From: lampysprites Date: Tue, 19 Oct 2021 12:23:12 +0700 Subject: [PATCH] Refactor the websocket timer --- src/app/script/websocket_class.cpp | 23 +++++++++++------------ 1 file changed, 11 insertions(+), 12 deletions(-) diff --git a/src/app/script/websocket_class.cpp b/src/app/script/websocket_class.cpp index a96e7326e..8060fae9f 100644 --- a/src/app/script/websocket_class.cpp +++ b/src/app/script/websocket_class.cpp @@ -30,8 +30,8 @@ namespace { // Additional "enum" value to make message callback simpler #define MESSAGE_TYPE_BINARY ((int)ix::WebSocketMessageType::Fragment + 10) -static ui::Timer* timer; -static std::set connections; +static std::unique_ptr g_timer; +static std::set g_connections; int WebSocket_new(lua_State* L) { @@ -63,13 +63,13 @@ int WebSocket_new(lua_State* L) int type = lua_getfield(L, 1, "minreconnectwait"); if (type == LUA_TNUMBER) { - ws->setMinWaitBetweenReconnectionRetries(lua_tointeger(L, -1)); + ws->setMinWaitBetweenReconnectionRetries(1000 * lua_tonumber(L, -1)); } lua_pop(L, 1); type = lua_getfield(L, 1, "maxreconnectwait"); if (type == LUA_TNUMBER) { - ws->setMaxWaitBetweenReconnectionRetries(lua_tointeger(L, -1)); + ws->setMaxWaitBetweenReconnectionRetries(1000 * lua_tonumber(L, -1)); } lua_pop(L, 1); @@ -170,11 +170,11 @@ int WebSocket_connect(lua_State* L) auto ws = get_ptr(L, 1); ws->start(); - if (connections.empty()) { - timer = new ui::Timer(33, ui::Manager::getDefault()); - timer->start(); + if (g_connections.empty()) { + g_timer = std::make_unique(33, ui::Manager::getDefault()); + g_timer->start(); } - connections.insert(ws); + g_connections.insert(ws); return 0; } @@ -184,10 +184,9 @@ int WebSocket_close(lua_State* L) auto ws = get_ptr(L, 1); ws->stop(); - connections.erase(ws); - if (connections.empty()) { - delete timer; - timer = nullptr; + g_connections.erase(ws); + if (g_connections.empty()) { + g_timer = nullptr; } return 0;