forked from natevw/PeerPouch
-
Notifications
You must be signed in to change notification settings - Fork 0
/
webrtc_test.html
76 lines (64 loc) · 2.41 KB
/
webrtc_test.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
<!doctype html>
<html>
<head>
<meta charset="utf-8">
<title>WebRTC test stub</title>
<script src="http://download.pouchdb.com/pouchdb-nightly.js"></script>
<script src="pouch.webrtc.js"></script>
<script src="http://d3js.org/queue.v1.min.js"></script>
<script src="http://d3js.org/d3.v3.min.js"></script>
<style>
a:not([href]):not(.action) { text-decoration: underline; color: lightgrey; }
a.action { text-decoration: underline; cursor: pointer; }
.dangerous { color: red; }
#hubStatus { color: grey; }
#hubStatus.ready { color: green; }
#hubStatus.error { color: orange; }
</style>
</head>
<body>
<h1>PeerPouch: PouchDB-over-WebRTC</h1>
<pre>TBD: log to here / simple demo</pre>
<script>
var NAME = location.hash.slice(1) || "noname",
HUB_URL = "http://peerpouch-test.ipcalf.com",
SHARE_URL = "idb://peerpouch-test-"+NAME;
queue()
.defer(PouchDB, HUB_URL)
.defer(PouchDB, SHARE_URL)
.await(function (e, hub, local) {
if (e) throw e;
window.dbgLocal = local; // expose for console inspection
setInterval(function () {
var id = NAME+'_at_'+Date.now();
local.put({_id:id});
}, 15e3);
local.changes({since:'latest', continuous:true, onChange:function (chg) {
var src = chg.id.split('_at_')[0];
if (src !== NAME) console.log("Got non-local change", chg.id);
}});
// share our local database
hub.shareDatabase(local, function (e,d) {
if (e) throw e;
});
// and open whichever other database gets shared next
console.log("Waiting for a new share to appear.");
var listener = hub.getSharedDatabases({onChange:function (d) {
listener.cancel();
console.log("Trying to connect to", d.dbname);
PouchDB(d.dbname, function (e,remote) {
if (e) throw e;
window.dbgRemote = remote; // expose for console inspection
console.log("Opened remote", remote);
PouchDB.replicate(remote, local, {continuous:true, onChange:function (change) {
console.log("RX CHANGE FROM REMOTE", change);
}});
PouchDB.replicate(local, remote, {continuous:true, onChange:function (change) {
console.log("TX CHANGE TO REMOTE", change);
}});
});
}}, function (){/*ignore existing*/});
});
</script>
</body>
</html>