Hello there! At the moment I’m writing a feature to handle re-connections/re-authentications if a player temporarily loses network access during a session. I mostly wanted to sanity check that this is the best approach (using the Godot 4 client).
extends Node
var _match_data
var _attempt_reconnect = false
var _reconnection_timer = 0
var client: NakamaClient
var session: NakamaSession
var socket: NakamaSocket
var _match: NakamaRTAPI.Match
# Called when the node enters the scene tree for the first time.
func _ready():
var host = "<my.host>"
client = Nakama.create_client("defaultkey", host, 7350, "http")
await _do_attempt_connection()
func _process(delta):
if(_attempt_reconnect):
_reconnection_timer += delta
# attempt reconnection every 3 seconds
if _reconnection_timer > 3:
_do_attempt_connection()
_reconnection_timer = 0
_attempt_reconnect = false;
func find_match():
var result = await socket.rpc_async("FindMatchRpc")
_match_data = result.payload
_match = await socket.join_match_async(_match_data)
return result
func _on_socket_connected():
_attempt_reconnect = false
func _on_socket_closed():
# connection lost
_attempt_reconnect = true
socket = null
# reattempt connection
_do_attempt_connection()
func _do_attempt_connection():
if session == null || session.expired:
# Get the System's unique device identifier
var device_id = OS.get_unique_id()
session = await client.authenticate_device_async(device_id)
if socket == null || !socket.is_connected_to_host():
socket = Nakama.create_socket_from(client)
socket.connected.connect(_on_socket_connected)
socket.closed.connect(_on_socket_closed)
var conn = await socket.connect_async(session)
if !socket.is_connected_to_host():
socket = null
_attempt_reconnect = true
else:
# rejoin a match if one exists
if _match_data != null:
_match = await socket.join_match_async(_match_data)
else:
_attempt_reconnect = false