broadcast server now has a javascript admin console

This commit is contained in:
Peter Thorson
2011-12-10 07:15:43 -06:00
parent e6f1ae286f
commit 649bd4ca36
2 changed files with 75 additions and 10 deletions

View File

@@ -34,17 +34,30 @@ function connect() {
ws.onclose = function(e) {
document.getElementById("messages").innerHTML += "Client: The connection to "+url+" was closed.<br />";
clear_hud()
};
ws.onmessage = function(e) {
document.getElementById("messages").innerHTML += "Server: "+e.data+"<br />";
};
foo = JSON.parse(e.data);
if (foo.type == "message") {
document.getElementById("messages").innerHTML += "Broadcasted Message: "+foo.value+"<br />";
} else if (foo.type == "con") {
document.getElementById("connected_clients").innerHTML = foo.value;
} else {
document.getElementById("messages").innerHTML += "Unrecognized Server Command.<br />";
}
}
}
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;
}
</style>
<div id="controls">
<div id="server">
<input type="text" name="server_url" id="server_url" value="ws://localhost:5000" />
<input type="text" name="server_url" id="server_url" value="ws://localhost:9002/admin" />
<button id="toggle_connect" onclick="toggle_connect();">Connect</button>
</div>
<div id="message_input"><input type="text" name="msg" id="msg" value="Hello World!" />
<button onclick="send();">Send</button></div>
<button onclick="send();">Broadcast</button></div>
<h2>Stats</h2>
<div>Connected Clients: <span id="connected_clients">N/A</span></div>
</div>
<div id="messages"></div>

View File

@@ -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<connection_ptr>::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<connection_ptr>::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<connection_ptr>::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<connection_ptr> m_connections;
std::set<connection_ptr> m_admin_connections;
};
int main(int argc, char* argv[]) {