diff options
| author | Per Larsson <[email protected]> | 2022-02-18 06:56:20 +0100 |
|---|---|---|
| committer | Per Larsson <[email protected]> | 2022-02-18 06:56:20 +0100 |
| commit | 4b9bac3c5baf7633cd51cffcf8e63cb5527ddb36 (patch) | |
| tree | 9d2f5e83679c0eea5de63b129eb1a2779501b28b /zenhttp/include | |
| parent | Renamed file. (diff) | |
| download | zen-4b9bac3c5baf7633cd51cffcf8e63cb5527ddb36.tar.xz zen-4b9bac3c5baf7633cd51cffcf8e63cb5527ddb36.zip | |
Simple websocket client/server test.
Diffstat (limited to 'zenhttp/include')
| -rw-r--r-- | zenhttp/include/zenhttp/websocket.h | 48 |
1 files changed, 48 insertions, 0 deletions
diff --git a/zenhttp/include/zenhttp/websocket.h b/zenhttp/include/zenhttp/websocket.h index 336d0bde5..eb4920f17 100644 --- a/zenhttp/include/zenhttp/websocket.h +++ b/zenhttp/include/zenhttp/websocket.h @@ -2,8 +2,13 @@ #include <zencore/memory.h> +#include <functional> #include <memory> +namespace asio { +class io_context; +} + namespace zen { struct WebSocketServerOptions @@ -23,6 +28,49 @@ public: static std::unique_ptr<WebSocketServer> Create(); }; +enum class WebSocketState : uint32_t +{ + kNone, + kConnecting, + kConnected, + kDisconnected, + kFailedToConnect +}; + +enum class WebSocketEvent : uint32_t +{ + kInvalid, + kConnected, + kDisconnected, + kError +}; + +struct WebSocketConnectInfo +{ + std::string Host; + int16_t Port{8848}; + std::string Endpoint; + std::vector<std::string> Protocols; + uint16_t Version{13}; +}; + +class WebSocketClient +{ +public: + using EventCallback = std::function<void()>; + + virtual ~WebSocketClient() = default; + + virtual bool Connect(const WebSocketConnectInfo& Info) = 0; + virtual void Disconnect() = 0; + virtual bool IsConnected() const = 0; + virtual WebSocketState State() const = 0; + + virtual void On(WebSocketEvent Evt, EventCallback&& Cb) = 0; + + static std::unique_ptr<WebSocketClient> Create(asio::io_context& IoCtx); +}; + struct WebSocketMessageHeader { static constexpr uint32_t ExpectedMagic = 0x7a776d68; // zwmh |