-
Notifications
You must be signed in to change notification settings - Fork 0
/
Copy pathindex.js
109 lines (82 loc) · 2.61 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
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
107
108
109
const EventEmitter = require('events').EventEmitter;
const hyperlog = require('hyperlog');
const sub = require('subleveldown');
const shasum = require('shasum');
const signalhub = require('signalhub');
const wswarm = require('webrtc-swarm');
const debounce = require('lodash.debounce');
class Chat extends EventEmitter {
constructor(nym, db) {
super();
this.db = db;
this.logs = {};
this.swarms = {};
this.peers = {};
this.onpeer = {};
this.ondisconnect = {};
this.onclose = {};
this.nym = nym;
// this.trackers = ['https://swarmcast-signalhub.herokuapp.com/'];
this.trackers = ['localhost:8080'];
}
join(channel) {
const { db, logs, onclose, ondisconnect, onpeer, peers, swarms, trackers } = this;
if (swarms.hasOwnProperty(channel)) {
console.log('you are already in the channel ', channel);
return;
}
logs[channel] = hyperlog(sub(db, channel), { valueEncoding: 'json' });
logs[channel].createReadStream({ live: true }).on('data', row => {
this.emit('say', channel, row);
});
this.emit('join', channel);
const hub = signalhub('swarmcast.' + channel, trackers);
const swarm = wswarm(hub);
swarms[channel] = swarm;
peers[channel] = {};
onpeer[channel] = (peer, id) => {
this.emit('peer', peer, id);
peers[channel][id] = peer;
peer.pipe(logs[channel].replicate({ live: true })).pipe(peer);
};
ondisconnect[channel] = (peer, id) => {
if (peers[channel][id]) {
this.emit('disconnect', peer, id);
delete peers[channel][id];
}
};
swarm.on('peer', onpeer[channel]);
swarm.on('disconnect', ondisconnect[channel]);
}
leave(channel) {
const { logs, onclose, ondisconnect, onpeer, peers, swarms } = this;
if (!swarms.hasOwnProperty(channel)) {
console.log('you are not in this channel');
return;
}
delete logs[channel];
swarms[channel].removeListener('peer', onpeer[channel]);
swarms[channel].removeListener('disconnect', ondisconnect[channel]);
delete onpeer[channel];
delete ondisconnect[channel];
Object.keys(peers[channel]).forEach(id => peers[channel][id].destroy());
delete peers[channel];
swarms[channel].close();
delete swarms[channel];
this.emit('leave', channel);
}
say(channel, message) {
const { logs, nym } = this;
if (!logs.hasOwnProperty(channel)) {
console.log('you need to join the channel first');
return;
}
const data = {
time: Date.now(),
who: nym,
message: message
};
logs[channel].append(data);
}
}
module.exports = Chat;