9 #ifndef CPPSERVER_HTTP_WS_SESSION_H
10 #define CPPSERVER_HTTP_WS_SESSION_H
41 virtual bool Close(
int status) {
return Close(status,
nullptr, 0); }
42 virtual bool Close(
int status,
const void* buffer,
size_t size) {
SendCloseAsync(status, buffer, size); HTTPSession::Disconnect();
return true; }
43 virtual bool Close(
int status, std::string_view text) {
SendCloseAsync(status, text); HTTPSession::Disconnect();
return true; }
87 std::string
ReceiveText(
const CppCommon::Timespan& timeout);
89 std::vector<uint8_t>
ReceiveBinary(
const CppCommon::Timespan& timeout);
93 void onReceived(
const void* buffer,
size_t size)
override;
99 void onWSClose(
const void* buffer,
size_t size,
int status = 1000)
override {
Close(); }
103 void onWSError(
const std::string& message)
override {
onError(asio::error::fault,
"WebSocket error", message); }
std::shared_ptr< TCPServer > & server() noexcept
Get the server.
virtual void onError(int error, const std::string &category, const std::string &message)
Handle error notification.
HTTPResponse & response() noexcept
Get the HTTP response.
bool SendResponseAsync()
Send the current HTTP response (asynchronous)
size_t SendResponse()
Send the current HTTP response (synchronous)
size_t SendPong(const void *buffer, size_t size)
size_t SendPing(std::string_view text)
size_t SendPing(const void *buffer, size_t size, const CppCommon::Timespan &timeout)
void onWSError(const std::string &message) override
Handle WebSocket error notification.
void onReceived(const void *buffer, size_t size) override
Handle buffer received notification.
std::vector< uint8_t > ReceiveBinary()
size_t SendClose(int status, const void *buffer, size_t size)
size_t SendBinary(const void *buffer, size_t size, const CppCommon::Timespan &timeout)
size_t SendPong(const void *buffer, size_t size, const CppCommon::Timespan &timeout)
WSSession & operator=(WSSession &&)=delete
void onReceivedRequest(const HTTP::HTTPRequest &request) override
Handle HTTP request received notification.
WSSession(const WSSession &)=delete
bool SendCloseAsync(int status, const void *buffer, size_t size)
size_t SendClose(int status, std::string_view text, const CppCommon::Timespan &timeout)
size_t SendPing(std::string_view text, const CppCommon::Timespan &timeout)
size_t SendText(std::string_view text, const CppCommon::Timespan &timeout)
virtual ~WSSession()=default
virtual bool Close(int status)
void onReceivedRequestHeader(const HTTP::HTTPRequest &request) override
Handle HTTP request header received notification.
virtual bool Close(int status, std::string_view text)
size_t SendText(std::string_view text)
bool SendTextAsync(const void *buffer, size_t size)
size_t SendBinary(std::string_view text, const CppCommon::Timespan &timeout)
virtual bool Close(int status, const void *buffer, size_t size)
void onWSPing(const void *buffer, size_t size) override
Handle WebSocket ping notification.
bool SendPongAsync(std::string_view text)
size_t SendText(const void *buffer, size_t size, const CppCommon::Timespan &timeout)
void onWSClose(const void *buffer, size_t size, int status=1000) override
Handle WebSocket close notification.
size_t SendBinary(const void *buffer, size_t size)
bool SendBinaryAsync(std::string_view text)
bool SendBinaryAsync(const void *buffer, size_t size)
size_t SendPong(std::string_view text)
size_t SendPong(std::string_view text, const CppCommon::Timespan &timeout)
void onReceivedRequestError(const HTTP::HTTPRequest &request, const std::string &error) override
Handle HTTP request error notification.
size_t SendClose(int status, const void *buffer, size_t size, const CppCommon::Timespan &timeout)
bool SendPongAsync(const void *buffer, size_t size)
bool SendTextAsync(std::string_view text)
WSSession(WSSession &&)=delete
size_t SendText(const void *buffer, size_t size)
bool SendPingAsync(std::string_view text)
size_t SendBinary(std::string_view text)
WSSession & operator=(const WSSession &)=delete
size_t SendPing(const void *buffer, size_t size)
bool SendCloseAsync(int status, std::string_view text)
void onDisconnected() override
Handle session disconnected notification.
size_t SendClose(int status, std::string_view text)
WSSession(const std::shared_ptr< WSServer > &server)
bool SendPingAsync(const void *buffer, size_t size)
std::string ReceiveText()
static const uint8_t WS_CLOSE
Close frame.
void PrepareSendFrame(uint8_t opcode, bool mask, const void *buffer, size_t size, int status=0)
Prepare WebSocket send frame.
static const uint8_t WS_TEXT
Text frame.
static const uint8_t WS_BINARY
Binary frame.
static const uint8_t WS_PING
Ping frame.
static const uint8_t WS_FIN
Final frame.
std::mutex _ws_send_lock
Send buffer lock.
static const uint8_t WS_PONG
Pong frame.
std::vector< uint8_t > _ws_send_buffer
Send buffer.
C++ Server project definitions.
WebSocket C++ Library definition.