fix: reconnect matchmaking event stream

This commit is contained in:
Josh Creek
2026-08-31 23:08:24 +01:00
parent a16db39884
commit 0be51ab902
2 changed files with 24 additions and 1 deletions
+23
View File
@@ -28,6 +28,8 @@ var _operation := ""
var _last_queue_create: Dictionary = {}
var _websocket: WebSocketPeer
var _websocket_status := "DISCONNECTED"
var _websocket_retry_seconds := 0.0
var _websocket_backoff := 1.0
func _ready() -> void:
@@ -50,6 +52,8 @@ func _process(_delta: float) -> void:
_websocket.poll()
var ready_state := _websocket.get_ready_state()
if ready_state == WebSocketPeer.STATE_OPEN:
_websocket_retry_seconds = 0.0
_websocket_backoff = 1.0
_set_websocket_status("CONNECTED")
while _websocket.get_available_packet_count() > 0:
_handle_websocket_packet(_websocket.get_packet())
@@ -57,6 +61,12 @@ func _process(_delta: float) -> void:
_set_websocket_status("CONNECTING")
elif ready_state == WebSocketPeer.STATE_CLOSED:
_set_websocket_status("DISCONNECTED")
if not auth_expired and is_valid_access_token(access_token):
_websocket_retry_seconds -= _delta
if _websocket_retry_seconds <= 0.0:
_websocket_retry_seconds = _websocket_backoff
_websocket_backoff = minf(_websocket_backoff * 2.0, 30.0)
connect_event_stream()
func configure(url: String, token: String) -> bool:
@@ -67,6 +77,8 @@ func configure(url: String, token: String) -> bool:
base_url = normalized
access_token = normalized_token
auth_expired = false
if _websocket != null:
connect_event_stream()
return true
@@ -79,6 +91,7 @@ func connect_event_stream() -> Error:
if err != OK:
_set_websocket_status("DISCONNECTED")
return err
_websocket_retry_seconds = 0.0
_set_websocket_status("CONNECTING")
return OK
@@ -86,6 +99,8 @@ func connect_event_stream() -> Error:
func disconnect_event_stream() -> void:
if _websocket != null:
_websocket.close()
_websocket_retry_seconds = 0.0
_websocket_backoff = 1.0
_set_websocket_status("DISCONNECTED")
@@ -244,6 +259,7 @@ func _on_request_completed(result: HTTPRequest.Result, response_code: int, _head
if response_code == HTTPClient.RESPONSE_UNAUTHORIZED:
access_token = ""
auth_expired = true
disconnect_event_stream()
state.fail("Session expired; sign in again")
ranked_profile.set_error("Session expired; sign in again")
session_expired.emit()
@@ -272,6 +288,7 @@ func _on_request_completed(result: HTTPRequest.Result, response_code: int, _head
access_token = returned_token
auth_expired = false
session_expires_at = String(payload.get("expires_at", ""))
connect_event_stream()
session_changed.emit(player_id)
elif operation == "queue_create":
state.begin_queue(String(payload.get("ticket_id", "")), String(payload.get("playlist", "")))
@@ -334,6 +351,12 @@ func _set_websocket_status(status: String) -> void:
return
_websocket_status = status
websocket_status_changed.emit(status)
if status == "CONNECTED":
if not state.ticket_id.is_empty() and state.phase not in [MatchmakingState.CANCELLED, MatchmakingState.EXPIRED, MatchmakingState.FAILED, MatchmakingState.LIVE]:
if not state.proposal_id.is_empty():
recover_proposal(state.proposal_id)
else:
recover_queue(state.ticket_id)
func _idempotency_key(prefix: String) -> String: