forked from hypercore-protocol/hypercore-protocol
-
Notifications
You must be signed in to change notification settings - Fork 0
/
example.js
67 lines (53 loc) · 1.15 KB
/
example.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
65
66
67
var protocol = require('./')
var a = protocol({id: 'a'})
var b = protocol({id: 'b'})
a.pipe(b).pipe(a)
var key = new Buffer('This is a 32 byte key, 012345678')
var missing = 5
var channel = a.open(key)
var remoteChannel = b.open(key)
a.on('end', function () {
console.log('peer a ended')
})
b.on('end', function () {
console.log('peer b ended')
})
channel.on('have', function (have) {
console.log('channel.onhave()', have)
for (var i = 0; i < 5; i++) {
channel.request({
index: i
})
}
})
channel.on('data', function (data) {
console.log('channel.ondata()', data)
if (!--missing) {
channel.info({
uploading: false,
download: false
})
}
})
remoteChannel.on('request', function (request) {
console.log('remoteChannel.onrequest()', request)
remoteChannel.data({
index: request.index,
value: 'sup'
})
})
remoteChannel.on('want', function (want) {
console.log('remoteChannel.onwant()', want)
remoteChannel.have({
start: 0,
length: 1000
})
})
remoteChannel.on('info', function (info) {
console.log('remoteChannel.oninfo', info)
b.finalize()
})
channel.want({
start: 0,
length: 1000
})