Skip to content

Commit 6d29c51

Browse files
authored
feat: realtime:ping(cb) + drop default ports from URLs (#20)
realtime:ping(callback) times a session.heartbeat round-trip and reports (rtt_ms, err). Lets games surface live RTT in HUDs without poking the private _send_with_callback. send_heartbeat (fire-and- forget) is unchanged. client.lua's URL builders drop the explicit port when it matches the scheme default. Defold's extension-websocket fails to connect to wss://host:443/path even though the URL is technically valid; tested against barrow.fly.dev. Reviewed by asobi-qa-lua (quick): GREEN, 0 issues introduced.
1 parent 35e70dc commit 6d29c51

2 files changed

Lines changed: 39 additions & 2 deletions

File tree

asobi/client.lua

Lines changed: 14 additions & 2 deletions
Original file line numberDiff line numberDiff line change
@@ -18,19 +18,31 @@ local dm = require("asobi.api.dm")
1818

1919
local M = {}
2020

21+
-- Drop the explicit port when it matches the scheme default. Some WS
22+
-- clients (notably Defold's extension-websocket) fail to connect to
23+
-- wss://host:443/path even though the URL is technically valid.
24+
local function authority(host, port, default_port)
25+
if port == default_port then
26+
return host
27+
end
28+
return host .. ":" .. tostring(port)
29+
end
30+
2131
function M.create(host, port, use_ssl)
2232
port = port or 8084
2333
use_ssl = use_ssl or false
2434

2535
local scheme = use_ssl and "https" or "http"
2636
local ws_scheme = use_ssl and "wss" or "ws"
37+
local http_default = use_ssl and 443 or 80
38+
local ws_default = use_ssl and 443 or 80
2739

2840
local client = {
2941
host = host,
3042
port = port,
3143
use_ssl = use_ssl,
32-
base_url = scheme .. "://" .. host .. ":" .. tostring(port),
33-
ws_url = ws_scheme .. "://" .. host .. ":" .. tostring(port) .. "/ws",
44+
base_url = scheme .. "://" .. authority(host, port, http_default),
45+
ws_url = ws_scheme .. "://" .. authority(host, port, ws_default) .. "/ws",
3446
session_token = nil,
3547
player_id = nil,
3648
}

asobi/realtime.lua

Lines changed: 25 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -145,6 +145,31 @@ function M:send_heartbeat()
145145
self:_send_fire_and_forget("session.heartbeat", {})
146146
end
147147

148+
-- Round-trip a session.heartbeat and report the elapsed time. The server
149+
-- echoes the heartbeat with the same cid, so this is the cheapest way for
150+
-- a game to surface live RTT (e.g. in a HUD).
151+
function M:ping(callback)
152+
if not self.connection then
153+
if callback then
154+
callback(nil, "not connected")
155+
end
156+
return
157+
end
158+
local t0 = socket.gettime()
159+
self:_send_with_callback("session.heartbeat", {}, function(_payload, err)
160+
if err then
161+
if callback then
162+
callback(nil, err)
163+
end
164+
return
165+
end
166+
local rtt_ms = math.floor((socket.gettime() - t0) * 1000 + 0.5)
167+
if callback then
168+
callback(rtt_ms, nil)
169+
end
170+
end)
171+
end
172+
148173
function M:list_worlds(opts, callback)
149174
local payload = {}
150175
if type(opts) == "string" then

0 commit comments

Comments
 (0)