9 #ifndef CPPSERVER_HTTP_WS_CLIENT_H
10 #define CPPSERVER_HTTP_WS_CLIENT_H
29 using HTTPClient::HTTPClient;
40 bool Connect(
const std::shared_ptr<Asio::TCPResolver>& resolver)
override;
42 bool ConnectAsync(
const std::shared_ptr<Asio::TCPResolver>& resolver)
override;
44 virtual bool Close(
int status) {
return Close(status,
nullptr, 0); }
45 virtual bool Close(
int status,
const void* buffer,
size_t size) {
SendClose(status, buffer, size); HTTPClient::Disconnect();
return true; }
46 virtual bool Close(
int status, std::string_view text) {
SendClose(status, text); HTTPClient::Disconnect();
return true; }
49 virtual bool CloseAsync(
int status,
const void* buffer,
size_t size) {
SendCloseAsync(status, buffer, size); HTTPClient::DisconnectAsync();
return true; }
50 virtual bool CloseAsync(
int status, std::string_view text) {
SendCloseAsync(status, text); HTTPClient::DisconnectAsync();
return true; }
94 std::string
ReceiveText(
const CppCommon::Timespan& timeout);
96 std::vector<uint8_t>
ReceiveBinary(
const CppCommon::Timespan& timeout);
101 void onReceived(
const void* buffer,
size_t size)
override;
111 void onWSError(
const std::string& message)
override {
onError(asio::error::fault,
"WebSocket error", message); }
virtual void onError(int error, const std::string &category, const std::string &message)
Handle error notification.
virtual bool Close(int status, const void *buffer, size_t size)
bool SendCloseAsync(int status, std::string_view text)
virtual bool CloseAsync(int status, const void *buffer, size_t size)
virtual bool CloseAsync()
virtual bool Close(int status, std::string_view text)
bool SendPingAsync(std::string_view text)
WSClient & operator=(const WSClient &)=delete
virtual ~WSClient()=default
size_t SendClose(int status, const void *buffer, size_t size)
bool SendBinaryAsync(std::string_view text)
bool ConnectAsync() override
Connect the client (asynchronous)
bool SendTextAsync(const void *buffer, size_t size)
size_t SendText(std::string_view text, const CppCommon::Timespan &timeout)
void onWSPing(const void *buffer, size_t size) override
Handle WebSocket ping notification.
size_t SendPong(const void *buffer, size_t size, const CppCommon::Timespan &timeout)
size_t SendPong(const void *buffer, size_t size)
std::string ReceiveText()
WSClient(const WSClient &)=delete
void onReceivedResponse(const HTTP::HTTPResponse &response) override
Handle HTTP response received notification.
size_t SendPing(const void *buffer, size_t size)
virtual bool CloseAsync(int status, std::string_view text)
void onReceivedResponseHeader(const HTTP::HTTPResponse &response) override
Handle HTTP response header received notification.
size_t SendClose(int status, std::string_view text)
size_t SendPing(std::string_view text)
void onDisconnected() override
Handle client disconnected notification.
bool Connect() override
Connect the client (synchronous)
size_t SendBinary(std::string_view text, const CppCommon::Timespan &timeout)
void onReceivedResponseError(const HTTP::HTTPResponse &response, const std::string &error) override
Handle HTTP response error notification.
size_t SendPong(std::string_view text, const CppCommon::Timespan &timeout)
bool SendCloseAsync(int status, const void *buffer, size_t size)
std::vector< uint8_t > ReceiveBinary()
void onConnected() override
Handle client connected notification.
size_t SendText(const void *buffer, size_t size)
virtual bool Close(int status)
virtual bool CloseAsync(int status)
bool SendPingAsync(const void *buffer, size_t size)
size_t SendClose(int status, std::string_view text, const CppCommon::Timespan &timeout)
void onWSError(const std::string &message) override
Handle WebSocket error notification.
bool SendBinaryAsync(const void *buffer, size_t size)
WSClient(WSClient &&)=delete
size_t SendPong(std::string_view text)
size_t SendClose(int status, const void *buffer, size_t size, const CppCommon::Timespan &timeout)
size_t SendPing(const void *buffer, size_t size, const CppCommon::Timespan &timeout)
size_t SendBinary(std::string_view text)
size_t SendText(const void *buffer, size_t size, const CppCommon::Timespan &timeout)
bool SendPongAsync(std::string_view text)
size_t SendBinary(const void *buffer, size_t size, const CppCommon::Timespan &timeout)
size_t SendBinary(const void *buffer, size_t size)
size_t SendText(std::string_view text)
bool SendPongAsync(const void *buffer, size_t size)
bool SendTextAsync(std::string_view text)
void onReceived(const void *buffer, size_t size) override
Handle buffer received notification.
void onWSClose(const void *buffer, size_t size, int status=1000) override
Handle WebSocket close notification.
size_t SendPing(std::string_view text, const CppCommon::Timespan &timeout)
WSClient & operator=(WSClient &&)=delete
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.