ImFusion SDK 4.3
WebsocketServer< Payload > Class Template Reference

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

Websocket server implementation providing bidirectional communication. More...

+ Inheritance diagram for WebsocketServer< Payload >:

Detailed Description

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

Websocket server implementation providing bidirectional communication.

Implements a WebSocket server that:

  • Handles connection requests from clients
  • Runs an read loop for incoming messages
  • Emits signals when new messages are received
  • Provides methods for sending messages to the clients
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

 WebsocketServer (const std::string &ip, int port, int threads=1)
 Initializes a WebSocket server that listens on the specified address {ip}:{port}.
 
 ~WebsocketServer () override
 Cleans up resources. If the server has not been stopped yet, returns after stopping is complete.
 
bool run () override
 Starts listening for incoming connections.
 
bool stop () override
 Stops listening and terminates all open connections.
 
bool isRunning () const override
 Returns whether the server is actively listening for connections.
 
bool isReadyToRun () const
 Returns true if the server was instantiated correctly and could be started, false otherwise, e.g if the selected combination {ip}:{port} is already in use by another connection.
 
size_t connectedClients () const override
 Returns the number of currently connected clients.
 
void sendMessage (Payload message) override
 Sends a message to all connected clients.
 
std::string address () const override
 Returns the address of the server in the format {ip}:{port} (e.g., 127.0.0.1:5678).
 
- 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 server configuration.
 

Public Attributes

ProtectedSignal< bool, std::string, int, bool * > signalClientConnected
 Signal triggered when a client connection is established or closed.
 
- Public Attributes inherited from ServerConnectionBase< std::string >
ProtectedSignal< bool > signalClientConnected
 Signal emitted when a new client connects (true) or when a client disconnects (false)
 
- 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

◆ WebsocketServer()

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

Initializes a WebSocket server that listens on the specified address {ip}:{port}.

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 on which the server listens.
threadsThe number of threads allocated for the I/O context. The number determines how many messages can be processed simultaneously from different clients.
  • A value of 1 is typically sufficient for a small number of clients or when messages are infrequent and small.
  • Increase this value if a connection demands significant resources, such as streaming images, to ensure responsiveness to other connections with minimal latency.

Member Function Documentation

◆ run()

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

Starts listening for incoming connections.

Returns
true if successfully started, false otherwise.

Implements ServerConnectionBase< std::string >.

Examples
NetworkCommunication.cpp.

◆ stop()

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

Stops listening and terminates all open connections.

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

Returns
true if successfully stopped, false otherwise.

Implements ServerConnectionBase< std::string >.

◆ isRunning()

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

Returns whether the server is actively listening for connections.

Implements ServerConnectionBase< std::string >.

◆ connectedClients()

template<typename Payload = std::string>
size_t connectedClients ( ) const
overridevirtual

Returns the number of currently connected clients.

Implements ServerConnectionBase< std::string >.

◆ address()

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

Returns the address of the server in the format {ip}:{port} (e.g., 127.0.0.1:5678).

Implements ServerConnectionBase< std::string >.

◆ blueprint()

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

Creates a blueprint for WebSocket server 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.

Member Data Documentation

◆ signalClientConnected

template<typename Payload = std::string>
ProtectedSignal<bool, std::string, int, bool*> signalClientConnected

Signal triggered when a client connection is established or closed.

Parameters
stateThe connection state (true if connected, false if disconnected).
hostPortHost and port information for the connection in the form {ip}:{port}.
refuseA pointer to a boolean flag that can be modified by the receiver.
  • Set to true to refuse the connection.
  • Set to false to allow the connection.
Note
Multiple receivers connected to this signal may overwrite the refuse flag, potentially negating changes made by earlier receivers.

Example usage to refuse the connection:

signalClientConnected.connect([](bool state, const std::string& hostPort, bool* refuse) {
refuse = true; // Refuses the connection
});
ProtectedSignal< bool, std::string, int, bool * > signalClientConnected
Signal triggered when a client connection is established or closed.
Definition WebsocketServer.h:93

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