yaze 0.3.2
Link to the Past ROM Editor
 
Loading...
Searching...
No Matches
yaze::net::IWebSocket Class Referenceabstract

Abstract interface for WebSocket client implementations. More...

#include <websocket_interface.h>

Inheritance diagram for yaze::net::IWebSocket:

Public Types

using MessageCallback = std::function<void(const std::string& message)>
 
using BinaryMessageCallback = std::function<void(const uint8_t* data, size_t length)>
 
using OpenCallback = std::function<void()>
 
using CloseCallback = std::function<void(int code, const std::string& reason)>
 
using ErrorCallback = std::function<void(const std::string& error)>
 

Public Member Functions

virtual ~IWebSocket ()=default
 
virtual absl::Status Connect (const std::string &url)=0
 Connect to a WebSocket server.
 
virtual absl::Status Send (const std::string &message)=0
 Send a text message.
 
virtual absl::Status SendBinary (const uint8_t *data, size_t length)
 Send a binary message.
 
virtual absl::Status Close (int code=1000, const std::string &reason="")=0
 Close the WebSocket connection.
 
virtual WebSocketState GetState () const =0
 Get the current connection state.
 
virtual bool IsConnected () const
 Check if the WebSocket is connected.
 
virtual void OnMessage (MessageCallback callback)=0
 Set callback for text message events.
 
virtual void OnBinaryMessage (BinaryMessageCallback callback)
 Set callback for binary message events.
 
virtual void OnOpen (OpenCallback callback)=0
 Set callback for connection open events.
 
virtual void OnClose (CloseCallback callback)=0
 Set callback for connection close events.
 
virtual void OnError (ErrorCallback callback)=0
 Set callback for error events.
 
virtual std::string GetUrl () const
 Get the WebSocket URL.
 
virtual void SetAutoReconnect (bool enable, int delay_seconds=5)
 Set automatic reconnection.
 

Protected Attributes

std::string url_
 
WebSocketState state_ = WebSocketState::kDisconnected
 
MessageCallback message_callback_
 
BinaryMessageCallback binary_message_callback_
 
OpenCallback open_callback_
 
CloseCallback close_callback_
 
ErrorCallback error_callback_
 
bool auto_reconnect_ = false
 
int reconnect_delay_seconds_ = 5
 

Detailed Description

Abstract interface for WebSocket client implementations.

This interface abstracts WebSocket operations to support both native (using various libraries) and WASM (using emscripten WebSocket) implementations. All methods use absl::Status for consistent error handling.

Definition at line 33 of file websocket_interface.h.

Member Typedef Documentation

◆ MessageCallback

using yaze::net::IWebSocket::MessageCallback = std::function<void(const std::string& message)>

Definition at line 36 of file websocket_interface.h.

◆ BinaryMessageCallback

using yaze::net::IWebSocket::BinaryMessageCallback = std::function<void(const uint8_t* data, size_t length)>

Definition at line 37 of file websocket_interface.h.

◆ OpenCallback

using yaze::net::IWebSocket::OpenCallback = std::function<void()>

Definition at line 38 of file websocket_interface.h.

◆ CloseCallback

using yaze::net::IWebSocket::CloseCallback = std::function<void(int code, const std::string& reason)>

Definition at line 39 of file websocket_interface.h.

◆ ErrorCallback

using yaze::net::IWebSocket::ErrorCallback = std::function<void(const std::string& error)>

Definition at line 40 of file websocket_interface.h.

Constructor & Destructor Documentation

◆ ~IWebSocket()

virtual yaze::net::IWebSocket::~IWebSocket ( )
virtualdefault

Member Function Documentation

◆ Connect()

virtual absl::Status yaze::net::IWebSocket::Connect ( const std::string & url)
pure virtual

Connect to a WebSocket server.

Parameters
urlThe WebSocket URL (ws:// or wss://)
Returns
Status indicating success or failure

Implemented in yaze::net::HttpLibWebSocket.

◆ Send()

virtual absl::Status yaze::net::IWebSocket::Send ( const std::string & message)
pure virtual

Send a text message.

Parameters
messageThe text message to send
Returns
Status indicating success or failure

Implemented in yaze::net::HttpLibWebSocket.

◆ SendBinary()

virtual absl::Status yaze::net::IWebSocket::SendBinary ( const uint8_t * data,
size_t length )
inlinevirtual

Send a binary message.

Parameters
dataThe binary data to send
lengthThe length of the data
Returns
Status indicating success or failure

Reimplemented in yaze::net::HttpLibWebSocket.

Definition at line 64 of file websocket_interface.h.

◆ Close()

virtual absl::Status yaze::net::IWebSocket::Close ( int code = 1000,
const std::string & reason = "" )
pure virtual

Close the WebSocket connection.

Parameters
codeOptional close code (default: 1000 for normal closure)
reasonOptional close reason
Returns
Status indicating success or failure

Implemented in yaze::net::HttpLibWebSocket.

◆ GetState()

virtual WebSocketState yaze::net::IWebSocket::GetState ( ) const
pure virtual

Get the current connection state.

Returns
Current WebSocket state

Implemented in yaze::net::HttpLibWebSocket.

Referenced by IsConnected().

◆ IsConnected()

virtual bool yaze::net::IWebSocket::IsConnected ( ) const
inlinevirtual

Check if the WebSocket is connected.

Returns
true if connected, false otherwise

Definition at line 88 of file websocket_interface.h.

References GetState(), and yaze::net::kConnected.

Here is the call graph for this function:

◆ OnMessage()

virtual void yaze::net::IWebSocket::OnMessage ( MessageCallback callback)
pure virtual

Set callback for text message events.

Parameters
callbackFunction to call when a text message is received

Implemented in yaze::net::HttpLibWebSocket.

◆ OnBinaryMessage()

virtual void yaze::net::IWebSocket::OnBinaryMessage ( BinaryMessageCallback callback)
inlinevirtual

Set callback for binary message events.

Parameters
callbackFunction to call when binary data is received

Reimplemented in yaze::net::HttpLibWebSocket.

Definition at line 102 of file websocket_interface.h.

References binary_message_callback_.

◆ OnOpen()

virtual void yaze::net::IWebSocket::OnOpen ( OpenCallback callback)
pure virtual

Set callback for connection open events.

Parameters
callbackFunction to call when connection is established

Implemented in yaze::net::HttpLibWebSocket.

◆ OnClose()

virtual void yaze::net::IWebSocket::OnClose ( CloseCallback callback)
pure virtual

Set callback for connection close events.

Parameters
callbackFunction to call when connection is closed

Implemented in yaze::net::HttpLibWebSocket.

◆ OnError()

virtual void yaze::net::IWebSocket::OnError ( ErrorCallback callback)
pure virtual

Set callback for error events.

Parameters
callbackFunction to call when an error occurs

Implemented in yaze::net::HttpLibWebSocket.

◆ GetUrl()

virtual std::string yaze::net::IWebSocket::GetUrl ( ) const
inlinevirtual

Get the WebSocket URL.

Returns
The URL this socket is connected/connecting to

Definition at line 129 of file websocket_interface.h.

References url_.

◆ SetAutoReconnect()

virtual void yaze::net::IWebSocket::SetAutoReconnect ( bool enable,
int delay_seconds = 5 )
inlinevirtual

Set automatic reconnection.

Parameters
enableEnable or disable auto-reconnect
delay_secondsDelay between reconnection attempts

Definition at line 136 of file websocket_interface.h.

References auto_reconnect_, and reconnect_delay_seconds_.

Member Data Documentation

◆ url_

std::string yaze::net::IWebSocket::url_
protected

◆ state_

◆ message_callback_

MessageCallback yaze::net::IWebSocket::message_callback_
protected

◆ binary_message_callback_

BinaryMessageCallback yaze::net::IWebSocket::binary_message_callback_
protected

◆ open_callback_

OpenCallback yaze::net::IWebSocket::open_callback_
protected

◆ close_callback_

CloseCallback yaze::net::IWebSocket::close_callback_
protected

◆ error_callback_

ErrorCallback yaze::net::IWebSocket::error_callback_
protected

◆ auto_reconnect_

bool yaze::net::IWebSocket::auto_reconnect_ = false
protected

Definition at line 153 of file websocket_interface.h.

Referenced by SetAutoReconnect().

◆ reconnect_delay_seconds_

int yaze::net::IWebSocket::reconnect_delay_seconds_ = 5
protected

Definition at line 154 of file websocket_interface.h.

Referenced by SetAutoReconnect().


The documentation for this class was generated from the following file: