#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.
| 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. |
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.
Signal< void(ServerPeer &)> PeerConnectedPeer authenticated and online.
Signal< void(ServerPeer &)> PeerDisconnectedPeer disconnected.
| 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(uv::Loop * loop)Constructs a server using the given event loop.
loop libuv event loop; defaults to uv::defaultLoop().Server(const Server &) = deleteDeleted constructor.
void start(const Options & opts)Starts the server with the given options. Begins accepting WebSocket connections on opts.host:opts.port.
opts Server configuration options.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.
opts Server configuration options.
httpFactory Factory for HTTP responders; may be nullptr.
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(const std::string & room, const json::Value & msg, const std::string & excludeId)Broadcast a message to all peers in a room (excluding sender).
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.
bool sendTo(const std::string & peerId, const json::Value & msg)Send a message to a specific peer by session ID.
bool sendToUser(const std::string & user, const json::Value & msg)Send a message to any peer with the given user name.
ServerPeer * getPeer(const std::string & id)Get a connected peer by session ID.
std::vector< ServerPeer * > getPeersInRoom(const std::string & room)Get all peers in a room.
const
size_t peerCount() constNumber of connected, authenticated peers.
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.
peer Peer data (must have "id", "user", "name" fields).
rooms Rooms the virtual peer joins.
handler Called when a message is routed to this peer.
void removeVirtualPeer(const std::string & peerId)Remove a virtual peer by session ID.
inline
inline http::Server & httpServer()Access the underlying HTTP server (e.g. to serve static files).
const inline
inline uv::Loop * loop() constEvent loop that owns the Symple server and all peer connections.
| 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. |
Options _optsuv::Loop * _loop = nullptrstd::unique_ptr< http::Server > _httpstd::unique_ptr< PeerRegistry > _peerRegistrystd::unique_ptr< RoomIndex > _roomIndexstd::mutex _mutexstd::atomic< bool > _shuttingDown {false}std::unique_ptr< http::ServerConnectionFactory > _httpFallbackFallback factory for non-WebSocket HTTP requests.
| Return | Name | Description |
|---|---|---|
void | onAuth | |
void | onMessage | |
void | onJoin | |
void | onLeave | |
void | onDisconnect | |
void | route | |
bool | deliver | |
bool | deliverSerialized | |
void | sendPresenceSnapshot |
void onAuth(ServerPeer & peer, const json::Value & msg, std::unique_lock< std::mutex > & lock)void onMessage(ServerPeer & peer, json::Value msg)void onJoin(ServerPeer & peer, const std::string & room)void onLeave(ServerPeer & peer, const std::string & room)void onDisconnect(ServerPeer & peer, std::unique_lock< std::mutex > & lock)void route(ServerPeer & sender, const json::Value & msg)bool deliver(const std::string & peerId, const json::Value & msg)bool deliverSerialized(const std::string & peerId, const char * data, size_t len, const json::Value & msg)void sendPresenceSnapshot(ServerPeer & recipient, const std::unordered_set< std::string > & rooms, std::string_view excludeId)