-
Notifications
You must be signed in to change notification settings - Fork 0
/
index.html
106 lines (87 loc) · 3.4 KB
/
index.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
63
64
65
66
67
68
69
70
71
72
73
74
75
76
77
78
79
80
81
82
83
84
85
86
87
88
89
90
91
92
93
94
95
96
97
98
99
100
101
102
103
104
105
106
<!doctype html>
<html lang="en">
<head>
<title>Browser-Network Db</title>
</head>
<body style="background-color: black; color: white;">
<code>
<h2><code id='client-id'></code></h2>
<h3>connections</h3>
<code id='connections'></code>
<h3>states</h3>
<code id='states'></code>
<h3>messages</h3>
<code id='messages'></code>
<h3>switchboard request interval</h3>
<code id='switchboard-request-interval'></code>
<button onclick='sendMessage(db.network.address + ": Hello there!")'>Say Hi</button>
<div>
<button onclick='db.network.startSwitchboardRequestInterval()'>Start Switch Interval</button>
<button onclick='db.network.startOfferBroadcastInterval()'>Start Offer Broadcast</button>
<button onclick='db.network.startGarbageCollectionInterval()'>Start Garbage Collection</button>
</div>
<div>
<button onclick='db.network.stopSwitchboardRequestInterval()'>Stop Switch Interval</button>
<button onclick='db.network.stopOfferBroadcastInterval()'>Stop Offer Broadcast</button>
<button onclick='db.network.stopGarbageCollectionInterval()'>Stop Garbage Collection</button>
</div>
</code>
<script src="//unpkg.com/@browser-network/crypto/umd/crypto.js"></script>
<script src="/umd/db.js"></script>
<script src="//unpkg.com/@browser-network/network/umd/network.js"></script>
<script>
window.db = new Db({
secret: '7e842370a488733a1f226e9686f37d8817f8960c2edfc21eddda51f6a9d7c4c4',
appId: '[db-test-app-id]',
network: new Network({
switchAddress: 'http://localhost:5678',
secret: Bnc.generateSecret(),
networkId: '[db-test-network-id]',
config:{
offerBroadcastInterval: 5 * 1000,
switchboardRequestInterval: 5 * 1000,
respectSwitchboardVolunteerMessages: false
}
})
})
function sendMessage(message) {
db.network.broadcast({
id: crypto.randomUUID(),
type: 'wahooooooo',
data: message,
ttl: 1,
destination: '*',
timestamp: Date.now(),
address: db.network.address
})
}
db.network.on('message', ({ appId, message}) => {
switch (message.type) {
case 'wahooooooo': {
const innerHTML = document.querySelector('#messages').innerHTML
document.querySelector('#messages').innerHTML = message.data + '</br>' + innerHTML
break;
}
default: {
// console.log('got some kinda crazy thing:', message)
}
}
})
db.onChange(() => {
console.log('got a change!', db.getAll())
})
setInterval(function() {
document.querySelector('#client-id').innerHTML = db.network.address
document.querySelector('#connections').innerHTML = db.network.connections.bind(db.network)().map(c => {
return '<br>' + JSON.stringify({
connectionId: c.id,
address: c.address
})
})
document.querySelector('#switchboard-request-interval').innerHTML = JSON.stringify(db.network._switchboardRequestInterval, 0, 2)
document.querySelector('#states').innerHTML = db.getAll().map(w => '<br>' + JSON.stringify([w.id, w.timestamp, w.state]))
}, 500)
window.DEBUG = false
</script>
</body>
</html>