beamable / BeamableProduct

The beamable product suite including com.beamable, com.beamable.server, microservice base image, portal, the installer, and build scripts
Other
5 stars 0 forks source link

Requester should expose `Date:` header from HTTP response, as a source of server-authoritative time #3395

Open allister-beamable opened 4 months ago

allister-beamable commented 4 months ago

Is your feature request related to a problem? Please describe.

At present if a Game Maker wants to get authoritative server time, they have to implement a C# Microservice method that uses DateTime.UtcNow or something equivalent. However, every HTTP response from the Beamable backend includes a Date: header that could be used as a source of authoritative server time (assuming the server-to-client latency is small enough to be negligible).

Describe the solution you'd like

It would be nice to have a way to directly query Requester for the server time reported by the most recent API response. Ideally, this should be anchored to device time in some way. That is, it should allow me to answer two questions:

Describe alternatives you've considered

While it is possible to work around this issue by implementing my own [ClientCallable]public Promise<string> GetServerTime() or the like, that is non-ideal for a couple of reasons: first off it requires me to have a C#MS at all (I might want an otherwise client-oriented game that doesn't use C#MS), and secondly it adds unnecessary network round trips to get information that already exists.

cdhanna commented 4 months ago

in the HandleResponse function of the PlatformRequester, we could do something like this,

if (request.GetResponseHeaders().TryGetValue("Date", out var dateHeader))
{
    // unknown format of dateHeader
}
allister-beamable commented 4 months ago

Here is an example of the response headers:

amacleod@valjean ~/s/e/AllisterTest> http POST https://api.beamable.com/basic/auth/token X-BEAM-SCOPE:$CID.$PID grant_type=guest
HTTP/1.1 200 OK
Access-Control-Allow-Origin: *
Connection: keep-alive
Content-Length: 155
Content-Type: application/json
Date: Tue, 21 May 2024 13:42:05 GMT
Server: akka-http/10.1.15
cdhanna commented 4 months ago
var ctx = await BeamContext.Default.Instance;
long latestServerTime = ctx.Requester.GetLatestServerTime(); // maybe try DateTimeOffset

// note the diff between server time and device time at the time of receiving the server timestamp
long latestDiff = ctx.Requester.GetLatestServerTimeDifference(); 

In the event the request fails, use -1 to indicate no value exists.

we can add the overrides to provide additional return types (later).

cdhanna commented 4 months ago

the Date header result from proto-actor may be different!

cdhanna commented 4 months ago

We should reach out to Justin and ask if we can change the header result for Date

allister-beamable commented 4 months ago

the Date header result from proto-actor may be different!

I did a quick check and it was the same as from the main stack: <DoW>, DD <Mon> YYYY HH:MM:SS GMT

There is probably not a huge amount of leeway in changing the output, unless we want to break compliance with HTTP standards (https://www.rfc-editor.org/rfc/rfc7231#section-7.1.1.1). It is a crying shame it cannot be ISO 8601 (https://xkcd.com/1179/), but specs gonna specify.

cdhanna commented 3 months ago

hint, line 606 in PlatformRequester could extract header like this,


if (request.GetResponseHeaders().TryGetValue("Date", out var dateHeader))
{

}