-
Notifications
You must be signed in to change notification settings - Fork 59
/
server-boilerplate.html
62 lines (51 loc) · 1.78 KB
/
server-boilerplate.html
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
61
62
<!DOCTYPE html>
<html>
<head>
<meta charset="utf-8">
<meta content="width=device-width, initial-scale=1" name="viewport">
<title>Bugout server boilerplate</title>
<script src="https://chr15m.github.io/bugout/bugout.min.js" type="application/javascript"></script>
</head>
<body><pre id="log"></pre></body>
<script>
// instantiate our bugout instance with previously saved seed (if any)
var b = new Bugout({seed: localStorage["bugout-demo-seed"]});
// save the seed for next time
localStorage["bugout-demo-seed"] = b.seed;
// log this server's address
log("address:", b.address());
log("announcing...");
/*** rpc calls ***/
// simple "ping" rpc call
b.register("ping", function(address, args, cb) {
args["pong"] = Math.random();
cb(args);
});
// register your RPC calls for clients here
/*** logging ***/
// log when network connectivity changes
b.on("connections", function(c) {
log("connections:", c);
if (c == 0) {
log("ready");
}
});
// log when a client sends a message
b.on("message", function(address, msg) { log("message:", address, msg); });
// log when a client makes an rpc call
b.on("rpc", function(address, call, args) { log("rpc:", address, call, args); });
// log when we see a new client address
b.on("seen", function(address) { log("seen:", address); });
// simple logging function
function log() {
var args = Array.prototype.slice.call(arguments);
console.log.apply(null, args);
args = args.map(function(a) { if (typeof(a) == "string") return a; else return JSON.stringify(a); });
document.getElementById("log").textContent += args.join(" ") + "\n";
}
</script>
<style>
body { background-color: #333; }
pre { color: #def; white-space: pre-wrap; word-wrap: break-word; }
</style>
</html>