diff --git a/lobby.lua b/lobby.lua index b275e3d..e4ce589 100644 --- a/lobby.lua +++ b/lobby.lua @@ -38,9 +38,19 @@ function lobby_server_checkclients() oct_timer_register("lobby_server_checkclients", 3000, "lobby_server_checkclients", "") 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) if lobby_server_first_call then oct_timer_register("lobby_server_checkclients", 3000, "lobby_server_checkclients", "") + lobby_server_ping() lobby_server_first_call = false end @@ -154,6 +164,21 @@ function lobby_clear_connect_form() unregister_textbox(lobby_name_textbox) 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 function lobby_client_ping() local msg = json.encode({ @@ -216,8 +241,9 @@ function lobby_client(key, ch, my_port) end msg,addr,port = oct_recv() - if (msg ~= "") then - msg_obj = json.decode(msg)[1] + if (msg and msg ~= "") then + local msg_obj = json.decode(msg)[1] + OCT_LOG_DEBUG(msg_obj["msg_type"]) -- State machine code here -- Registration was successful @@ -230,6 +256,8 @@ function lobby_client(key, ch, my_port) client_connected = true client_wait_for_reg_response = false client_filling_in_connect_form = false + lobby_client_pinged = true + lobby_client_checkping() lobby_clear_connect_form() -- ping the server to let them know we are alive local ping_msg = json.encode({ @@ -261,6 +289,11 @@ function lobby_client(key, ch, my_port) do client_clientlist_sprite["shape"] = client_clientlist_sprite["shape"] .. "\n" .. k 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