Send the play queue ID to the server
Build / test (push) Successful in 45s
Build / build (push) Successful in 1m12s

The server keeps a play queue per client, identified by the X-Queue-Id
header. The client sends the ID it has for the selected server with every
request and keeps the one in each response, which is new after a reset or a
sync. The IDs are saved per server in the [queues] section of settings.cfg,
so the game goes on with the same queue after a restart.

HttpTransport now passes the response headers on to the client.
This commit is contained in:
Sansan committed 2026-10-07 21:23:37 +02:00
1 parent d21b3e2ce0
commit 4a45e8516d
8 files changed
+212 -16

No files matched your search

+18 -5
View File
@@ -12,6 +12,9 @@ extends HttpTransport
## Like the real server it hands out tokens on [code]POST /token[/code] and answers 401
## to requests without a valid one. Token requests are counted in
## [member token_requests], not in [member requested_paths].
##
## Set [member queue_id] to answer with an [code]X-Queue-Id[/code] header, like the
## server's music endpoints.
## Paths that need no token, like on the real server.
const OPEN_PATHS: Array[String] = ["/token", "/health"]
@@ -26,11 +29,14 @@ var requested_headers: Array[PackedStringArray] = []
var token_requests: int = 0
## The JSON body of the last token request.
var last_token_request: Dictionary = {}
## Sent in an X-Queue-Id header with every answer that is not about a token, unless empty.
var queue_id: String = ""
var _responses: Dictionary = {}
var _successes_left: Dictionary = {}
var _held_paths: Dictionary = {}
var _previous_transport: HttpTransport
var _previous_queue_ids: Dictionary[String, String]
var _valid_tokens: Array[String] = []
@@ -43,6 +49,8 @@ func install() -> FakeHttpTransport:
_previous_transport = MusicServerClient.transport
MusicServerClient.transport = self
MusicServerClient.clear_token()
_previous_queue_ids = Settings.queue_ids.duplicate()
Settings.queue_ids.clear()
return self
@@ -50,6 +58,7 @@ func install() -> FakeHttpTransport:
func uninstall() -> void:
MusicServerClient.transport = _previous_transport
MusicServerClient.clear_token()
Settings.queue_ids = _previous_queue_ids
## Answers requests to [param path] with [param data] encoded as JSON.
@@ -112,7 +121,8 @@ func request(
requested_methods.append(method)
requested_headers.append(headers)
if !OPEN_PATHS.has(path.get_slice("?", 0)) && !_has_valid_token(headers):
on_complete.call(401, JSON.stringify({"error": "Unauthorized"}).to_utf8_buffer())
on_complete.call(401, JSON.stringify({"error": "Unauthorized"}).to_utf8_buffer(),
PackedStringArray())
return
if _held_paths.has(path):
_held_paths[path].append(_answer.bind(path, on_complete))
@@ -121,15 +131,18 @@ func request(
func _answer(path: String, on_complete: Callable) -> void:
var headers: PackedStringArray = []
if !queue_id.is_empty() && path != "/token":
headers.append("X-Queue-Id: " + queue_id)
if _successes_left.has(path):
if _successes_left[path] == 0:
on_complete.call(503, PackedByteArray())
on_complete.call(503, PackedByteArray(), headers)
return
_successes_left[path] -= 1
if _responses.has(path):
on_complete.call(_responses[path].code, _responses[path].body)
on_complete.call(_responses[path].code, _responses[path].body, headers)
else:
on_complete.call(404, PackedByteArray())
on_complete.call(404, PackedByteArray(), headers)
func _answer_token_request(body: String, on_complete: Callable) -> void:
@@ -147,7 +160,7 @@ func _answer_token_request(body: String, on_complete: Callable) -> void:
_valid_tokens.append(token)
on_complete.call(200, JSON.stringify({
"token": token, "expires_at": "2026-10-07T12:00:00Z", "client_type": "godot",
}).to_utf8_buffer())
}).to_utf8_buffer(), PackedStringArray())
func _has_valid_token(headers: PackedStringArray) -> bool: