-
-
Notifications
You must be signed in to change notification settings - Fork 5
/
Copy pathindex.js
64 lines (50 loc) · 1.57 KB
/
index.js
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
var hypercore = require('hypercore')
var level = require('level-browserify')
var swarm = require('webrtc-swarm')
var signalhub = require('signalhub')
var pump = require('pump')
var key = window.location.hash.slice(1)
var all = false
var cnt = 0
var db = level('hyperirc.db')
var core = hypercore(db)
var feed = core.createFeed(key, {sparse: true})
var $main = document.getElementById('main')
function log (msg) {
var pre = document.createElement('pre')
pre.innerText = msg
$main.appendChild(pre)
}
feed.get(0, function (err, channel) {
if (err) throw err
document.title = document.getElementById('channel').innerText = channel.toString()
var end = feed.blocks
if (!all) {
feed.get(feed.blocks, function () {
if (feed.blocks - end > 25) {
stream.destroy()
log('(skipping to latest messages)')
tail()
}
})
}
var stream = tail()
function tail () {
var stream = feed.createReadStream({live: true, start: all ? 0 : Math.max(feed.blocks - 25, 1)})
.on('data', function (data) {
log(data.toString())
})
return stream
}
})
var sw = swarm(signalhub('hyperirc-' + feed.discoveryKey.toString('hex'), 'https://signalhub.mafintosh.com'))
console.log('Waiting for peers...')
sw.on('peer', function (connection) {
console.log('(webrtc peer joined, %d total', ++cnt)
document.getElementById('count').innerText = '' + cnt
var peer = feed.replicate()
pump(peer, connection, peer, function () {
console.log('(webrtc peer left, %d total', --cnt)
document.getElementById('count').innerText = '' + cnt
})
})