-
Notifications
You must be signed in to change notification settings - Fork 45
/
connection-helper.js
42 lines (42 loc) · 1.38 KB
/
connection-helper.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
class Connection {
constructor(remoteClient, AblyRealtime, initiator) {
console.log(`Opening connection to ${remoteClient}`);
this._remoteClient = remoteClient;
this.isConnected = false;
this._p2pConnection = new SimplePeer({
initiator: initiator,
trickle: false,
});
this._p2pConnection.on('signal', this._onSignal.bind(this));
this._p2pConnection.on('error', this._onError.bind(this));
this._p2pConnection.on('connect', this._onConnect.bind(this));
this._p2pConnection.on('close', this._onClose.bind(this));
this._p2pConnection.on('data', this._onData.bind(this));
}
handleSignal(signal) {
this._p2pConnection.signal(signal);
}
send(msg) {
this._p2pConnection.send(msg);
}
_onSignal(signal) {
AblyRealtime.publish(`ablywebrtc-signal/${this._remoteClient}`, {
user: clientId,
signal: signal,
});
}
_onConnect() {
this.isConnected = true;
console.log('connected to ' + this._remoteClient);
}
_onClose() {
console.log(`connection to ${this._remoteClient} closed`);
delete connections[this._remoteClient];
}
_onData(data) {
receiveMessage(this._remoteClient, data)
}
_onError(error) {
console.log(`an error occured ${error.toString()}`);
}
}