Server
Server
#include <icy/symple/server.h>class ServerDefined in src/symple/include/icy/symple/server.h:136
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 += ServerPeer& 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.
List of all members
| Name | Kind | Owner |
|---|---|---|
Factory | friend | Declared here |
Authenticate | variable | Declared here |
PeerConnected | variable | Declared here |
PeerDisconnected | variable | Declared here |
Server | function | Declared here |
~Server | function | Declared here |
Server | function | Declared here |
operator= | function | Declared here |
start | function | Declared here |
start | function | Declared here |
stop | function | Declared here |
broadcast | function | Declared here |
broadcastRooms | function | Declared here |
sendTo | function | Declared here |
sendToUser | function | Declared here |
getPeer | function | Declared here |
getPeersInRoom | function | Declared here |
peerCount | function | Declared here |
addVirtualPeer | function | Declared here |
removeVirtualPeer | function | Declared here |
httpServer | function | Declared here |
loop | function | Declared here |
_opts | variable | Declared here |
_loop | variable | Declared here |
_http | variable | Declared here |
_peerRegistry | variable | Declared here |
_roomIndex | variable | Declared here |
_mutex | variable | Declared here |
_shuttingDown | variable | Declared here |
_httpFallback | variable | Declared here |
onAuth | function | Declared here |
onMessage | function | Declared here |
onJoin | function | Declared here |
onLeave | function | Declared here |
onDisconnect | function | Declared here |
route | function | Declared here |
deliver | function | Declared here |
deliverSerialized | function | Declared here |
sendPresenceSnapshot | function | Declared here |
Friends
| Name | Description |
|---|---|
Factory |
Factory
friend class FactoryDefined in src/symple/include/icy/symple/server.h:279
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)> AuthenticateDefined in src/symple/include/icy/symple/server.h:213
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.
PeerConnected
Signal< void(ServerPeer &)> PeerConnectedDefined in src/symple/include/icy/symple/server.h:216
Peer authenticated and online.
PeerDisconnected
Signal< void(ServerPeer &)> PeerDisconnectedDefined in src/symple/include/icy/symple/server.h:219
Peer 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 = uv::defaultLoop())Defined in src/symple/include/icy/symple/server.h:157
Constructs a server using the given event loop.
Parameters
looplibuv event loop; defaults to uv::defaultLoop().
Server
Server(const Server &) = deleteDefined in src/symple/include/icy/symple/server.h:160
Deleted constructor.
start
void start(const Options & opts)Defined in src/symple/include/icy/symple/server.h:166
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)Defined in src/symple/include/icy/symple/server.h:173
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()Defined in src/symple/include/icy/symple/server.h:178
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 = {})Defined in src/symple/include/icy/symple/server.h:182
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 = {})Defined in src/symple/include/icy/symple/server.h:186
Broadcast to multiple rooms with per-recipient dedup.
sendTo
bool sendTo(const std::string & peerId, const json::Value & msg)Defined in src/symple/include/icy/symple/server.h:191
Send a message to a specific peer by session ID.
sendToUser
bool sendToUser(const std::string & user, const json::Value & msg)Defined in src/symple/include/icy/symple/server.h:194
Send a message to any peer with the given user name.
getPeer
ServerPeer * getPeer(const std::string & id)Defined in src/symple/include/icy/symple/server.h:197
Get a connected peer by session ID.
getPeersInRoom
std::vector< ServerPeer * > getPeersInRoom(const std::string & room)Defined in src/symple/include/icy/symple/server.h:200
Get all peers in a room.
peerCount
const
size_t peerCount() constDefined in src/symple/include/icy/symple/server.h:203
Number of connected, authenticated peers.
addVirtualPeer
void addVirtualPeer(const Peer & peer, const std::vector< std::string > & rooms, std::function< void(const json::Value &)> handler)Defined in src/symple/include/icy/symple/server.h:230
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)Defined in src/symple/include/icy/symple/server.h:235
Remove a virtual peer by session ID.
httpServer
inline
inline http::Server & httpServer()Defined in src/symple/include/icy/symple/server.h:238
Access the underlying HTTP server (e.g. to serve static files).
loop
const inline
inline uv::Loop * loop() constDefined in src/symple/include/icy/symple/server.h:241
Event 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 _optsDefined in src/symple/include/icy/symple/server.h:267
_loop
uv::Loop * _loop = nullptrDefined in src/symple/include/icy/symple/server.h:268
_http
std::unique_ptr< http::Server > _httpDefined in src/symple/include/icy/symple/server.h:269
_peerRegistry
std::unique_ptr< PeerRegistry > _peerRegistryDefined in src/symple/include/icy/symple/server.h:270
_roomIndex
std::unique_ptr< RoomIndex > _roomIndexDefined in src/symple/include/icy/symple/server.h:271
_mutex
std::mutex _mutexDefined in src/symple/include/icy/symple/server.h:273
_shuttingDown
std::atomic< bool > _shuttingDown {false}Defined in src/symple/include/icy/symple/server.h:274
_httpFallback
std::unique_ptr< http::ServerConnectionFactory > _httpFallbackDefined in src/symple/include/icy/symple/server.h:277
Fallback 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)Defined in src/symple/include/icy/symple/server.h:252
onMessage
void onMessage(ServerPeer & peer, json::Value msg)Defined in src/symple/include/icy/symple/server.h:253
onJoin
void onJoin(ServerPeer & peer, const std::string & room)Defined in src/symple/include/icy/symple/server.h:254
onLeave
void onLeave(ServerPeer & peer, const std::string & room)Defined in src/symple/include/icy/symple/server.h:255
onDisconnect
void onDisconnect(ServerPeer & peer, std::unique_lock< std::mutex > & lock)Defined in src/symple/include/icy/symple/server.h:256
route
void route(ServerPeer & sender, const json::Value & msg)Defined in src/symple/include/icy/symple/server.h:257
deliver
bool deliver(const std::string & peerId, const json::Value & msg)Defined in src/symple/include/icy/symple/server.h:258
deliverSerialized
bool deliverSerialized(const std::string & peerId, const char * data, size_t len, const json::Value & msg)Defined in src/symple/include/icy/symple/server.h:259
sendPresenceSnapshot
void sendPresenceSnapshot(ServerPeer & recipient, const std::unordered_set< std::string > & rooms, std::string_view excludeId = {})