-
Notifications
You must be signed in to change notification settings - Fork 0
/
webrtcnegotiator.js
40 lines (33 loc) · 1.02 KB
/
webrtcnegotiator.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
const RPC = require('./rpc')
module.exports = class WebRTCNegotiator extends RPC {
constructor(options = {}) {
options.discoverable = false
super(options)
}
handleReady() {
super.handleReady()
this.transport = this.network.transport.webrtc
this.transport.on('signal', (...args) => this.dispatch(...args))
}
dispatch(data, connection) {
const id = connection.id
const origin = this.network.id
const receiver = connection.peer
const signal = { id, origin, receiver, data }
this.debug('dispatch %s', signal.id)
this.remotes.forEach(negotiator => negotiator.dispatch(signal))
}
receive(signal) {
this.debug('received a signal %s', signal.id)
this.transport.handle(signal)
}
handleIncomingConnection() {}
handleOutgoingConnection(connection) {
if (connection.peer !== 'tracker') return
super.handleOutgoingConnection(connection)
}
getProtocol(methods = {}) {
methods.receive = (...args) => this.receive(...args)
return super.getProtocol(methods)
}
}