diff --git a/examples/broadcast_server_tls/broadcast_admin.html b/examples/broadcast_server_tls/broadcast_admin.html index cd46a17a72..464fda238c 100644 --- a/examples/broadcast_server_tls/broadcast_admin.html +++ b/examples/broadcast_server_tls/broadcast_admin.html @@ -34,17 +34,30 @@ function connect() { ws.onclose = function(e) { document.getElementById("messages").innerHTML += "Client: The connection to "+url+" was closed.
"; + clear_hud() }; ws.onmessage = function(e) { - document.getElementById("messages").innerHTML += "Server: "+e.data+"
"; - }; + foo = JSON.parse(e.data); + + if (foo.type == "message") { + document.getElementById("messages").innerHTML += "Broadcasted Message: "+foo.value+"
"; + } else if (foo.type == "con") { + document.getElementById("connected_clients").innerHTML = foo.value; + } else { + document.getElementById("messages").innerHTML += "Unrecognized Server Command.
"; + } + } } -function disconnect() { - ws.close(); +function clear_hud() { document.getElementById("server_url").disabled = false; document.getElementById("toggle_connect").innerHTML = "Connect"; + document.getElementById("connected_clients").innerHTML = "N/A"; +} + +function disconnect() { + ws.close(); } function toggle_connect() { @@ -74,19 +87,28 @@ body,html { } #controls { float:right; - background-color: #999; + background-color: #333; + color: #fff; + padding: 5px; } - +#controls h2 { + font-family: sans-serif; + font-size: 16px; +} +
- +
-
+
+ +

Stats

+
Connected Clients: N/A
diff --git a/examples/broadcast_server_tls/broadcast_server_tls.cpp b/examples/broadcast_server_tls/broadcast_server_tls.cpp index 490d2b2913..8d47bae711 100644 --- a/examples/broadcast_server_tls/broadcast_server_tls.cpp +++ b/examples/broadcast_server_tls/broadcast_server_tls.cpp @@ -72,22 +72,64 @@ public: } void on_open(connection_ptr connection) { - //std::cout << "connection opened" << std::endl; - m_connections.insert(connection); + + + if (connection->get_resource() == "/admin") { + m_admin_connections.insert(connection); + } else { + m_connections.insert(connection); + } + + typename std::set::iterator it; + + std::stringstream foo; + foo << "{\"type\":\"con\",\"value\":" << m_connections.size() << "}"; + + for (it = m_admin_connections.begin(); it != m_admin_connections.end(); it++) { + (*it)->send(foo.str(),false); + } } void on_close(connection_ptr connection) { //std::cout << "connection closed" << std::endl; m_connections.erase(connection); + m_admin_connections.erase(connection); + + typename std::set::iterator it; + + std::stringstream foo; + foo << "{\"type\":\"con\",\"value\":" << m_connections.size() << "}"; + + for (it = m_admin_connections.begin(); it != m_admin_connections.end(); it++) { + (*it)->send(foo.str(),false); + } + } void on_message(connection_ptr connection,websocketpp::message::data_ptr msg) { typename std::set::iterator it; + // broadcast to clients for (it = m_connections.begin(); it != m_connections.end(); it++) { (*it)->send(msg->get_payload(),(msg->get_opcode() == websocketpp::frame::opcode::BINARY)); } + // broadcast to admins + std::stringstream foo; + foo << "{\"type\":\"message\",\"value\":\""; + + if (msg->get_opcode() == websocketpp::frame::opcode::BINARY) { + foo << "[Binary Message length: " << msg->get_payload().size() << "]"; + } else { + foo << msg->get_payload(); + } + + foo << "\"}"; + + for (it = m_admin_connections.begin(); it != m_admin_connections.end(); it++) { + (*it)->send(foo.str(),false); + } + connection->recycle(msg); } @@ -104,6 +146,7 @@ public: } private: std::set m_connections; + std::set m_admin_connections; }; int main(int argc, char* argv[]) {