7#include "absl/status/status.h"
8#include "absl/strings/str_cat.h"
17std::string PageGateway() {
18 return emscripten_run_script_string(
19 "new URLSearchParams(window.location.search).get('gateway') || ''");
25class GatewayServers final :
public Servers {
27 explicit GatewayServers(std::string gateway) : gateway_(std::move(gateway)) {}
29 absl::StatusOr<protocol::LoginReply> Ask(
30 const protocol::LoginRequest& request)
override {
34 absl::StatusOr<std::unique_ptr<Link>> Enter(
35 const protocol::WorldListing& world,
const std::string& token)
override {
36 return Open(world.host(), world.port(), token);
39 absl::StatusOr<std::unique_ptr<Link>> Move(
40 const protocol::Move& move)
override {
41 return Open(move.host(), move.port(), move.token());
47 absl::StatusOr<std::unique_ptr<Link>> Open(
const std::string& host,
49 const std::string& token) {
51 absl::StrCat(gateway_,
"/world/", host,
"/", port), token);
55 return std::unique_ptr<Link>(*std::move(link));
63class NoServers final :
public Servers {
65 absl::StatusOr<protocol::LoginReply> Ask(
66 const protocol::LoginRequest&)
override {
70 absl::StatusOr<std::unique_ptr<Link>> Enter(
const protocol::WorldListing&,
71 const std::string&)
override {
75 absl::StatusOr<std::unique_ptr<Link>> Move(
const protocol::Move&)
override {
81 return absl::UnavailableError(
82 "the page was opened without a gateway to the servers");
90absl::StatusOr<std::unique_ptr<Servers>>
Servers::Create(std::string,
bool) {
91 std::string gateway = PageGateway();
92 if (gateway.empty()) {
93 return std::make_unique<NoServers>();
95 return std::make_unique<GatewayServers>(std::move(gateway));
static absl::StatusOr< std::unique_ptr< Servers > > Create(std::string host, bool udp)
static absl::StatusOr< std::unique_ptr< WebSocketLink > > Open(const std::string &url, const std::string &token)
Opens the world's url at the gateway, and says hello with the token.
bool CanReachServers()
Whether this build can reach the servers at all: a browser's can't.
absl::StatusOr< protocol::LoginReply > AskOverWebSocket(const std::string &url, const protocol::LoginRequest &request)
The login server's answer to the request, through the gateway at url.
absl::StatusOr< std::vector< std::string > > Unreachable(const smollergraph::Graph &graph)