Server
Server
#include <icy/symple/server.h>Symple v4 server.
Accepts WebSocket connections, authenticates peers, manages rooms, and routes messages. Implements the Symple v4 protocol over native WebSocket.
Usage: smpl::Server server; server.start({.port = 4500});
// Optional: custom authentication server.Authenticate += & peer, const json::Value& auth, bool& allowed, std::vectorstd::string& rooms) { allowed = (auth.value("token", "") == "secret"); rooms.push_back("team-a"); };
The server also serves as an HTTP server, so you can serve static files (e.g. a web UI) on the same port.
Public Attributes
| Return | Name | Description |
|---|---|---|
Signal< void(ServerPeer &, const json::Value &auth, bool &allowed, std::vector< std::string > &rooms)> | Authenticate | Custom authentication hook. Set allowed to false to reject the peer. Append to rooms to assign team/group memberships. If not connected, all peers with a valid user field are accepted. |
Signal< void(ServerPeer &)> | PeerConnected | Peer authenticated and online. |
Signal< void(ServerPeer &)> | PeerDisconnected | Peer disconnected. |
Authenticate
Signal< void(ServerPeer &, const json::Value &auth, bool &allowed, std::vector< std::string > &rooms)> AuthenticateCustom authentication hook. Set allowed to false to reject the peer. Append to rooms to assign team/group memberships. If not connected, all peers with a valid user field are accepted.
PeerConnected
Signal< void(ServerPeer &)> PeerConnectedPeer authenticated and online.
PeerDisconnected
Signal< void(ServerPeer &)> PeerDisconnectedPeer disconnected.
Public Methods
| Return | Name | Description |
|---|---|---|
Server | Constructs a server using the given event loop. | |
Server | Deleted constructor. | |
void | start | Starts the server with the given options. Begins accepting WebSocket connections on opts.host:opts.port. |
void | start | Starts the server with a custom HTTP factory for non-WebSocket requests. The Symple server handles WebSocket upgrades internally; any other HTTP request (e.g. static files, REST API) is delegated to this factory. |
void | stop | Broadcasts a shutdown notice to all peers, closes the listen socket, and releases all internal state. Safe to call more than once. |
void | broadcast | Broadcast a message to all peers in a room (excluding sender). |
void | broadcastRooms | Broadcast to multiple rooms with per-recipient dedup. |
bool | sendTo | Send a message to a specific peer by session ID. |
bool | sendToUser | Send a message to any peer with the given user name. |
ServerPeer * | getPeer | Get a connected peer by session ID. |
std::vector< ServerPeer * > | getPeersInRoom | Get all peers in a room. |
size_t | peerCount const | Number of connected, authenticated peers. |
void | addVirtualPeer | Register a virtual peer that receives messages via callback. |
void | removeVirtualPeer | Remove a virtual peer by session ID. |
http::Server & | httpServer inline | Access the underlying HTTP server (e.g. to serve static files). |
uv::Loop * | loop const inline | Event loop that owns the Symple server and all peer connections. |
Server
Server(uv::Loop * loop)Constructs a server using the given event loop.
Parameters
looplibuv event loop; defaults to uv::defaultLoop().
Server
Server(const Server &) = deleteDeleted constructor.
start
void start(const Options & opts)Starts the server with the given options. Begins accepting WebSocket connections on opts.host:opts.port.
Parameters
optsServer configuration options.
start
void start(const Options & opts, std::unique_ptr< http::ServerConnectionFactory > httpFactory)Starts the server with a custom HTTP factory for non-WebSocket requests. The Symple server handles WebSocket upgrades internally; any other HTTP request (e.g. static files, REST API) is delegated to this factory.
Parameters
optsServer configuration options.httpFactoryFactory for HTTP responders; may be nullptr.
stop
void stop()Broadcasts a shutdown notice to all peers, closes the listen socket, and releases all internal state. Safe to call more than once.
broadcast
void broadcast(const std::string & room, const json::Value & msg, const std::string & excludeId)Broadcast a message to all peers in a room (excluding sender).
broadcastRooms
void broadcastRooms(const std::unordered_set< std::string > & rooms, const json::Value & msg, const std::string & excludeId)Broadcast to multiple rooms with per-recipient dedup.
sendTo
bool sendTo(const std::string & peerId, const json::Value & msg)Send a message to a specific peer by session ID.
sendToUser
bool sendToUser(const std::string & user, const json::Value & msg)Send a message to any peer with the given user name.
getPeer
ServerPeer * getPeer(const std::string & id)Get a connected peer by session ID.
getPeersInRoom
std::vector< ServerPeer * > getPeersInRoom(const std::string & room)Get all peers in a room.
peerCount
const
size_t peerCount() constNumber of connected, authenticated peers.
addVirtualPeer
void addVirtualPeer(const Peer & peer, const std::vector< std::string > & rooms, std::function< void(const json::Value &)> handler)Register a virtual peer that receives messages via callback.
The virtual peer appears in presence broadcasts and is routable like any WebSocket-connected peer. Messages addressed to it are delivered via the callback instead of a WebSocket connection.
Parameters
peerPeer data (must have "id", "user", "name" fields).roomsRooms the virtual peer joins.handlerCalled when a message is routed to this peer.
removeVirtualPeer
void removeVirtualPeer(const std::string & peerId)Remove a virtual peer by session ID.
httpServer
inline
inline http::Server & httpServer()Access the underlying HTTP server (e.g. to serve static files).
loop
const inline
inline uv::Loop * loop() constEvent loop that owns the Symple server and all peer connections.
Private Attributes
| Return | Name | Description |
|---|---|---|
Options | _opts | |
uv::Loop * | _loop | |
std::unique_ptr< http::Server > | _http | |
std::unique_ptr< PeerRegistry > | _peerRegistry | |
std::unique_ptr< RoomIndex > | _roomIndex | |
std::mutex | _mutex | |
std::atomic< bool > | _shuttingDown | |
std::unique_ptr< http::ServerConnectionFactory > | _httpFallback | Fallback factory for non-WebSocket HTTP requests. |
_opts
Options _opts_loop
uv::Loop * _loop = nullptr_http
std::unique_ptr< http::Server > _http_peerRegistry
std::unique_ptr< PeerRegistry > _peerRegistry_roomIndex
std::unique_ptr< RoomIndex > _roomIndex_mutex
std::mutex _mutex_shuttingDown
std::atomic< bool > _shuttingDown {false}_httpFallback
std::unique_ptr< http::ServerConnectionFactory > _httpFallbackFallback factory for non-WebSocket HTTP requests.
Private Methods
| Return | Name | Description |
|---|---|---|
void | onAuth | |
void | onMessage | |
void | onJoin | |
void | onLeave | |
void | onDisconnect | |
void | route | |
bool | deliver | |
bool | deliverSerialized | |
void | sendPresenceSnapshot |
onAuth
void onAuth(ServerPeer & peer, const json::Value & msg, std::unique_lock< std::mutex > & lock)onMessage
void onMessage(ServerPeer & peer, json::Value msg)onJoin
void onJoin(ServerPeer & peer, const std::string & room)onLeave
void onLeave(ServerPeer & peer, const std::string & room)onDisconnect
void onDisconnect(ServerPeer & peer, std::unique_lock< std::mutex > & lock)route
void route(ServerPeer & sender, const json::Value & msg)deliver
bool deliver(const std::string & peerId, const json::Value & msg)deliverSerialized
bool deliverSerialized(const std::string & peerId, const char * data, size_t len, const json::Value & msg)sendPresenceSnapshot
void sendPresenceSnapshot(ServerPeer & recipient, const std::unordered_set< std::string > & rooms, std::string_view excludeId)