#include <ImFusion/Stream/Network/WebsocketServer.h>
Websocket server implementation providing bidirectional communication.
More...
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
-
- Note
- Messages are sent and received asynchronously.
- Examples
- NetworkCommunication.cpp.
|
| | 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).
|
| |
| virtual void | sendMessage (std::string message)=0 |
| | Sends a message over the network connection.
|
| |
◆ WebsocketServer()
template<typename Payload = std::string>
Initializes a WebSocket server that listens on the specified address {ip}:{port}.
- Parameters
-
| ip | The IP address of the WebSocket server. Must be an IPv4 address in the form "xxx.xxx.xxx.xxx" or "localhost". |
| port | The port number on which the server listens. |
| threads | The 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.
|
◆ run()
template<typename Payload = std::string>
◆ stop()
template<typename Payload = std::string>
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>
◆ connectedClients()
template<typename Payload = std::string>
| size_t connectedClients |
( |
| ) |
const |
|
overridevirtual |
◆ address()
template<typename Payload = std::string>
◆ blueprint()
template<typename Payload = std::string>
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.
◆ signalClientConnected
template<typename Payload = std::string>
Signal triggered when a client connection is established or closed.
- Parameters
-
| state | The connection state (true if connected, false if disconnected). |
| hostPort | Host and port information for the connection in the form {ip}:{port}. |
| refuse | A 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:
refuse = true;
});
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:
- ImFusion/Stream/Network/WebsocketServer.h