Server pings clients periodically, clients error if lost connection

This commit is contained in:
j4nk 2025-03-23 18:09:32 -04:00
parent 3688c202a4
commit 21468434cb
1 changed files with 35 additions and 2 deletions

View File

@ -38,9 +38,19 @@ function lobby_server_checkclients()
oct_timer_register("lobby_server_checkclients", 3000, "lobby_server_checkclients", "") oct_timer_register("lobby_server_checkclients", 3000, "lobby_server_checkclients", "")
end end
function lobby_server_ping()
for k,v in pairs(oct_lobby_clientlist)
do
local msg = json.encode({{ msg_type=OCT_LOBBY_MSG_PING, token=v["token"] }})
oct_send(msg, v["addr"], v["port"])
end
oct_timer_register("lobby_server_ping", 3000, "lobby_server_ping", "")
end
function lobby_server(maxplayers) function lobby_server(maxplayers)
if lobby_server_first_call then if lobby_server_first_call then
oct_timer_register("lobby_server_checkclients", 3000, "lobby_server_checkclients", "") oct_timer_register("lobby_server_checkclients", 3000, "lobby_server_checkclients", "")
lobby_server_ping()
lobby_server_first_call = false lobby_server_first_call = false
end end
@ -154,6 +164,21 @@ function lobby_clear_connect_form()
unregister_textbox(lobby_name_textbox) unregister_textbox(lobby_name_textbox)
end end
lobby_client_pinged = false
function lobby_client_checkping()
if not lobby_client_pinged
then
lobby_connect_message["x"] = 0
lobby_connect_message["y"] = 20
lobby_connect_message["shape"] = "Lost connection to server"
lobby_connect_message["fg"] = TB_RED
else
lobby_client_pinged = false
oct_timer_register("lobby_client_checkping", 3000, "lobby_client_checkping", "")
end
end
-- every 1 second, ping the server to let it know we are still alive -- every 1 second, ping the server to let it know we are still alive
function lobby_client_ping() function lobby_client_ping()
local msg = json.encode({ local msg = json.encode({
@ -216,8 +241,9 @@ function lobby_client(key, ch, my_port)
end end
msg,addr,port = oct_recv() msg,addr,port = oct_recv()
if (msg ~= "") then if (msg and msg ~= "") then
msg_obj = json.decode(msg)[1] local msg_obj = json.decode(msg)[1]
OCT_LOG_DEBUG(msg_obj["msg_type"])
-- State machine code here -- State machine code here
-- Registration was successful -- Registration was successful
@ -230,6 +256,8 @@ function lobby_client(key, ch, my_port)
client_connected = true client_connected = true
client_wait_for_reg_response = false client_wait_for_reg_response = false
client_filling_in_connect_form = false client_filling_in_connect_form = false
lobby_client_pinged = true
lobby_client_checkping()
lobby_clear_connect_form() lobby_clear_connect_form()
-- ping the server to let them know we are alive -- ping the server to let them know we are alive
local ping_msg = json.encode({ local ping_msg = json.encode({
@ -261,6 +289,11 @@ function lobby_client(key, ch, my_port)
do do
client_clientlist_sprite["shape"] = client_clientlist_sprite["shape"] .. "\n" .. k client_clientlist_sprite["shape"] = client_clientlist_sprite["shape"] .. "\n" .. k
end end
-- We received a ping from the server
elseif (msg_obj["msg_type"] == OCT_LOBBY_MSG_PING and msg_obj["token"] == client_connect_token)
then
lobby_client_pinged = true
end end
end end
end end