nanoMMO 0.97.8
Building an MMO: The Engineering Series
Loading...
Searching...
No Matches
websocket_link.h
Go to the documentation of this file.
1#ifndef NANOMMO_CLIENT_WEBSOCKET_LINK_H_
2#define NANOMMO_CLIENT_WEBSOCKET_LINK_H_
3
4#include <emscripten/websocket.h>
5
6#include <cstdint>
7#include <deque>
8#include <memory>
9#include <optional>
10#include <string>
11#include <string_view>
12#include <utility>
13#include <vector>
14
15#include "absl/status/status.h"
16#include "absl/status/statusor.h"
17#include "client/inbox.h"
18#include "client/link.h"
19#include "protocol/framing.h"
20#include "protocol/nanommo.pb.h"
21
22namespace nanommo {
23
24/// A browser's WebSocket, to the gateway: the bytes it's sent, as they come,
25/// for whoever reads them. In a browser.
26class WebSocket {
27 public:
28 /// Opens the url, and waits for it to open, letting the page carry on
29 /// meanwhile, or returns why it didn't.
30 static absl::StatusOr<std::unique_ptr<WebSocket>> Open(
31 const std::string& url);
32
33 WebSocket(const WebSocket&) = delete;
34 WebSocket& operator=(const WebSocket&) = delete;
35 ~WebSocket();
36
37 absl::Status Send(std::string_view bytes);
38
39 /// What's come since the last time.
40 std::string Take();
41
42 bool open() const { return opened_ && !closed_; }
43
44 /// Waits for something to come, or the socket to close, letting the page
45 /// carry on meanwhile, for up to the milliseconds.
46 void Wait(int milliseconds);
47
48 private:
49 explicit WebSocket(EMSCRIPTEN_WEBSOCKET_T socket) : socket_(socket) {}
50
51 static bool OnOpen(int type, const EmscriptenWebSocketOpenEvent* event,
52 void* self);
53 static bool OnMessage(int type, const EmscriptenWebSocketMessageEvent* event,
54 void* self);
55 static bool OnError(int type, const EmscriptenWebSocketErrorEvent* event,
56 void* self);
57 static bool OnClose(int type, const EmscriptenWebSocketCloseEvent* event,
58 void* self);
59
60 EMSCRIPTEN_WEBSOCKET_T socket_;
61 bool opened_ = false;
62 bool closed_ = false;
63 std::string received_;
64};
65
66/// The login server's answer to the request, through the gateway at url.
67absl::StatusOr<protocol::LoginReply> AskOverWebSocket(
68 const std::string& url, const protocol::LoginRequest& request);
69
70/// A world server, through the gateway, over a browser's WebSocket: the same
71/// messages as over TLS on a computer, with the world over the same stream.
72class WebSocketLink final : public Link {
73 public:
74 /// Opens the world's url at the gateway, and says hello with the token.
75 static absl::StatusOr<std::unique_ptr<WebSocketLink>> Open(
76 const std::string& url, const std::string& token);
77
78 void Poll() override;
79 void Send(const protocol::Input& input) override;
80 bool connected() const override { return connected_; }
81 const protocol::World& world() const override { return inbox_.world(); }
82 const std::deque<protocol::Chat>& chat() const override {
83 return inbox_.chat();
84 }
85 int64_t whole() const override { return inbox_.whole(); }
86 const std::optional<protocol::Move>& move() const override {
87 return inbox_.move();
88 }
89 const protocol::Inventory& inventory() const override {
90 return inbox_.inventory();
91 }
92 const protocol::Quests& quests() const override { return inbox_.quests(); }
93 const protocol::Guild& guild() const override { return inbox_.guild(); }
94 const protocol::Trade& trade() const override { return inbox_.trade(); }
95 const protocol::Duel& duel() const override { return inbox_.duel(); }
96 std::vector<protocol::Event> TakeEvents() override {
97 return inbox_.TakeEvents();
98 }
99 std::optional<protocol::GuildInvite> TakeInvite() override {
100 return inbox_.TakeInvite();
101 }
102
103 private:
104 explicit WebSocketLink(std::unique_ptr<WebSocket> socket)
105 : socket_(std::move(socket)) {}
106
107 std::unique_ptr<WebSocket> socket_;
108 FrameReader reader_;
109 Inbox inbox_;
110 bool connected_ = true;
111};
112
113} // namespace nanommo
114
115#endif // NANOMMO_CLIENT_WEBSOCKET_LINK_H_
WebSocket(const WebSocket &)=delete
std::string Take()
What's come since the last time.
absl::Status Send(std::string_view bytes)
void Wait(int milliseconds)
WebSocket & operator=(const WebSocket &)=delete
static absl::StatusOr< std::unique_ptr< WebSocket > > Open(const std::string &url)
Definition chat.cc:9
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.