ImFusion SDK 4.3
WebsocketClient< Payload > Class Template Reference

#include <ImFusion/Stream/Network/WebsocketClient.h>

WebSocket client implementation providing bidirectional communication. More...

+ Inheritance diagram for WebsocketClient< Payload >:

Detailed Description

template<typename Payload = std::string>
class ImFusion::Network::WebsocketClient< Payload >

WebSocket client implementation providing bidirectional communication.

Implements a WebSocket client that:

  • Maintains a persistent connection to a WebSocket server.
  • Runs a read loop to handle incoming messages.
  • Emits signals when new messages are received.
  • Provides methods for sending messages to the server.
Template Parameters
PayloadThe message type for the socket, either std::string (default) or ByteBuffer.
Note
Messages are sent and received asynchronously.
Examples
NetworkCommunication.cpp.

Public Member Functions

 WebsocketClient (const std::string &ip, int port)
 Constructs a WebSocket client.
 
bool connect (int connectionTimeoutMs=500) override
 Attempts to establish a connection to the TCP socket server with a predefined timeout when it aborts the attempt (500 milliseconds by default).
 
bool isConnected () const override
 Returns true if a handshake was successful and the connection is active, false otherwise.
 
void disconnect () override
 Terminates the connection and cancels the current session.
 
void sendMessage (Payload message) override
 Sends a message over the network connection.
 
- Public Member Functions inherited from TypedConnectionBase< std::string >
virtual void sendMessage (std::string message)=0
 Sends a message over the network connection.
 

Static Public Member Functions

static ConnectionBlueprint< Payload > blueprint ()
 Creates a blueprint for WebSocket configuration.
 

Additional Inherited Members

- Public Attributes inherited from TypedConnectionBase< std::string >
ProtectedSignal< std::shared_ptr< std::string > > signalNewMessage
 Signal emitted when a new message is received.
 
ProtectedSignal< std::shared_ptr< std::string > > signalMessageSent
 Signal emitted when a message is sent.
 

Constructor & Destructor Documentation

◆ WebsocketClient()

template<typename Payload = std::string>
WebsocketClient ( const std::string & ip,
int port )
explicit

Constructs a WebSocket client.

Parameters
ipThe IP address of the WebSocket server. Must be an IPv4 address in the form "xxx.xxx.xxx.xxx" or "localhost".
portThe port number of the WebSocket server.

Member Function Documentation

◆ connect()

template<typename Payload = std::string>
bool connect ( int connectionTimeoutMs = 500)
overridevirtual

Attempts to establish a connection to the TCP socket server with a predefined timeout when it aborts the attempt (500 milliseconds by default).

Returns
true if the connection is successfully established, false otherwise.

Implements ClientConnectionBase< std::string >.

◆ isConnected()

template<typename Payload = std::string>
bool isConnected ( ) const
overridevirtual

Returns true if a handshake was successful and the connection is active, false otherwise.

Implements ClientConnectionBase< std::string >.

◆ disconnect()

template<typename Payload = std::string>
void disconnect ( )
overridevirtual

Terminates the connection and cancels the current session.

Pending messages for sending are still being sent. Incoming messages that have not yet been processed are discarded.

Implements ClientConnectionBase< std::string >.

◆ sendMessage()

template<typename Payload = std::string>
void sendMessage ( Payload message)
override

Sends a message over the network connection.

Emits signalMessageSent after the message has been sent.

◆ blueprint()

template<typename Payload = std::string>
static ConnectionBlueprint< Payload > blueprint ( )
static

Creates a blueprint for WebSocket configuration.

Returns
A blueprint defining:
  • Required connection parameters.
  • Default property values.
  • Socket construction logic.
Note
The blueprint is registered with ConnectionFactory by default to facilitate WebSocket creation and configuration.

The documentation for this class was generated from the following file:
Search Tab / S to search, Esc to close