-
-
Notifications
You must be signed in to change notification settings - Fork 60
/
index.js
182 lines (153 loc) · 4.7 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
110
111
112
113
114
115
116
117
118
119
120
121
122
123
124
125
126
127
128
129
130
131
132
133
134
135
136
137
138
139
140
141
142
143
144
145
146
147
148
149
150
151
152
153
154
155
156
157
158
159
160
161
162
163
164
165
166
167
168
169
170
171
172
173
174
175
176
177
178
179
180
181
182
var SimplePeer = require('simple-peer')
var inherits = require('inherits')
var events = require('events')
var through = require('through2')
var cuid = require('cuid')
var once = require('once')
var debug = require('debug')('webrtc-swarm')
module.exports = WebRTCSwarm
function WebRTCSwarm (hub, opts) {
if (!(this instanceof WebRTCSwarm)) return new WebRTCSwarm(hub, opts)
if (!hub) throw new Error('SignalHub instance required')
if (!opts) opts = {}
events.EventEmitter.call(this)
this.setMaxListeners(0)
this.hub = hub
this.wrtc = opts.wrtc
this.channelConfig = opts.channelConfig
this.config = opts.config
this.stream = opts.stream
this.wrap = opts.wrap || function (data) { return data }
this.unwrap = opts.unwrap || function (data) { return data }
this.offerConstraints = opts.offerConstraints || {}
this.maxPeers = opts.maxPeers || Infinity
this.me = opts.uuid || cuid()
debug('my uuid:', this.me)
this.remotes = {}
this.peers = []
this.closed = false
subscribe(this, hub)
}
inherits(WebRTCSwarm, events.EventEmitter)
WebRTCSwarm.WEBRTC_SUPPORT = SimplePeer.WEBRTC_SUPPORT
WebRTCSwarm.prototype.close = function (cb) {
if (this.closed) return
this.closed = true
if (cb) this.once('close', cb)
var self = this
this.hub.close(function () {
var len = self.peers.length
if (len > 0) {
var closed = 0
self.peers.forEach(function (peer) {
peer.once('close', function () {
if (++closed === len) {
self.emit('close')
}
})
process.nextTick(function () {
peer.destroy()
})
})
} else {
self.emit('close')
}
})
}
function setup (swarm, peer, id) {
peer.on('connect', function () {
debug('connected to peer', id)
swarm.peers.push(peer)
swarm.emit('peer', peer, id)
swarm.emit('connect', peer, id)
})
var onclose = once(function (err) {
debug('disconnected from peer', id, err)
if (swarm.remotes[id] === peer) delete swarm.remotes[id]
var i = swarm.peers.indexOf(peer)
if (i > -1) swarm.peers.splice(i, 1)
swarm.emit('disconnect', peer, id)
})
var signals = []
var sending = false
function kick () {
if (swarm.closed || sending || !signals.length) return
sending = true
var data = {from: swarm.me, signal: signals.shift()}
data = swarm.wrap(data, id)
swarm.hub.broadcast(id, data, function () {
sending = false
kick()
})
}
peer.on('signal', function (sig) {
signals.push(sig)
kick()
})
peer.on('error', onclose)
peer.once('close', onclose)
}
function subscribe (swarm, hub) {
hub.subscribe('all').pipe(through.obj(function (data, enc, cb) {
data = swarm.unwrap(data, 'all')
if (swarm.closed || !data) return cb()
debug('/all', data)
if (data.from === swarm.me) {
debug('skipping self', data.from)
return cb()
}
if (data.type === 'connect') {
if (swarm.peers.length >= swarm.maxPeers) {
debug('skipping because maxPeers is met', data.from)
return cb()
}
if (swarm.remotes[data.from]) {
debug('skipping existing remote', data.from)
return cb()
}
debug('connecting to new peer (as initiator)', data.from)
var peer = new SimplePeer({
wrtc: swarm.wrtc,
initiator: true,
channelConfig: swarm.channelConfig,
config: swarm.config,
stream: swarm.stream,
offerConstraints: swarm.offerConstraints
})
setup(swarm, peer, data.from)
swarm.remotes[data.from] = peer
}
cb()
}))
hub.subscribe(swarm.me).once('open', connect.bind(null, swarm, hub)).pipe(through.obj(function (data, enc, cb) {
data = swarm.unwrap(data, swarm.me)
if (swarm.closed || !data) return cb()
var peer = swarm.remotes[data.from]
if (!peer) {
if (!data.signal || data.signal.type !== 'offer') {
debug('skipping non-offer', data)
return cb()
}
debug('connecting to new peer (as not initiator)', data.from)
peer = swarm.remotes[data.from] = new SimplePeer({
wrtc: swarm.wrtc,
channelConfig: swarm.channelConfig,
config: swarm.config,
stream: swarm.stream,
offerConstraints: swarm.offerConstraints
})
setup(swarm, peer, data.from)
}
debug('signalling', data.from, data.signal)
peer.signal(data.signal)
cb()
}))
}
function connect (swarm, hub) {
if (swarm.closed || swarm.peers.length >= swarm.maxPeers) return
var data = {type: 'connect', from: swarm.me}
data = swarm.wrap(data, 'all')
hub.broadcast('all', data, function () {
setTimeout(connect.bind(null, swarm, hub), Math.floor(Math.random() * 2000) + (swarm.peers.length ? 13000 : 3000))
})
}