-
Notifications
You must be signed in to change notification settings - Fork 56
Expand file tree
/
Copy pathexample.js
More file actions
52 lines (45 loc) · 1.48 KB
/
example.js
File metadata and controls
52 lines (45 loc) · 1.48 KB
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
import P2PT from "./src/p2pt"
let logElem = document.getElementById('log');
const log = msg => {
if (typeof msg == 'object') {
logElem.innerHTML += (JSON && JSON.stringify ? JSON.stringify(msg, undefined, 2) : msg) + '<br />';
} else {
logElem.innerHTML += msg + '<br />';
}
}
// Find public WebTorrent tracker URLs here : https://github.com/ngosang/trackerslist/blob/master/trackers_all_ws.txt
var trackersAnnounceURLs = [
'wss://tracker.openwebtorrent.com',
'wss://tracker.webtorrent.dev',
'wss://tracker.files.fm:7073/announce',
'wss://tracker.btorrent.xyz/',
]
// This 'myApp' is called identifier and should be unique to your app
var p2pt = new P2PT(trackersAnnounceURLs, 'myApp')
// If a tracker connection was successful
p2pt.on('trackerconnect', (tracker, stats) => {
log('Connected to tracker : ' + tracker.announceUrl)
log('Tracker stats : ' + JSON.stringify(stats))
log('')
})
// If a new peer, send message
p2pt.on('peerconnect', (peer) => {
log('New Peer ! : ' + peer.id + '. Sending Hi')
p2pt.send(peer, 'Hi').then(([peer, msg]) => {
log('Got response : ' + msg)
return peer.respond('Bye')
}).then(([peer, msg]) => {
log('Got response2 : ' + msg)
})
})
// If message received from peer
p2pt.on('msg', (peer, msg) => {
log(`Got message from ${peer.id} : ${msg}`)
if (msg === 'Hi') {
peer.respond('Hello !').then(([peer, msg]) => {
peer.respond('Bye !')
})
}
})
log('P2PT started. My peer id : ' + p2pt._peerId)
p2pt.start()