@osimatic/helpers-js 1.0.51 → 1.0.54
This diff represents the content of publicly available package versions that have been released to one of the supported registries. The information contained in this diff is provided for informational purposes only and reflects changes between package versions as they appear in their respective public registries.
- package/event_bus.js +3 -1
- package/package.json +1 -1
- package/web_rtc.js +34 -14
- package/web_socket.js +61 -24
package/event_bus.js
CHANGED
package/package.json
CHANGED
package/web_rtc.js
CHANGED
|
@@ -4,14 +4,14 @@ class WebRTC {
|
|
|
4
4
|
this.stunUrl = stunUrl;
|
|
5
5
|
}
|
|
6
6
|
|
|
7
|
-
static
|
|
8
|
-
this.
|
|
7
|
+
static setTurnSecret(turnSecret) {
|
|
8
|
+
this.turnSecret = turnSecret;
|
|
9
9
|
}
|
|
10
10
|
|
|
11
11
|
static offer(stream, iceCandidateCallback) {
|
|
12
|
-
return new Promise((resolve, reject) => {
|
|
12
|
+
return new Promise(async (resolve, reject) => {
|
|
13
13
|
try {
|
|
14
|
-
let { username, password } = this.
|
|
14
|
+
let { username, password } = this.getTurnCredentials();
|
|
15
15
|
let peerConn = new RTCPeerConnection(
|
|
16
16
|
{
|
|
17
17
|
iceServers: [
|
|
@@ -28,19 +28,20 @@ class WebRTC {
|
|
|
28
28
|
}
|
|
29
29
|
});
|
|
30
30
|
|
|
31
|
-
peerConn.createOffer()
|
|
32
|
-
|
|
33
|
-
|
|
31
|
+
const offer = await peerConn.createOffer();
|
|
32
|
+
await peerConn.setLocalDescription(offer);
|
|
33
|
+
|
|
34
|
+
resolve(peerConn);
|
|
34
35
|
} catch (error) {
|
|
35
36
|
reject(error);
|
|
36
37
|
}
|
|
37
38
|
});
|
|
38
39
|
}
|
|
39
40
|
|
|
40
|
-
static answer
|
|
41
|
-
return new Promise((resolve, reject) => {
|
|
41
|
+
static answer(remoteDescription, onTrackCallback, iceCandidateCallback) {
|
|
42
|
+
return new Promise(async (resolve, reject) => {
|
|
42
43
|
try {
|
|
43
|
-
let { username, password } = this.
|
|
44
|
+
let { username, password } = this.getTurnCredentials();
|
|
44
45
|
let peerConn = new RTCPeerConnection(
|
|
45
46
|
{
|
|
46
47
|
iceServers: [
|
|
@@ -57,10 +58,12 @@ class WebRTC {
|
|
|
57
58
|
}
|
|
58
59
|
});
|
|
59
60
|
|
|
60
|
-
peerConn.setRemoteDescription(remoteDescription)
|
|
61
|
-
|
|
62
|
-
|
|
63
|
-
.
|
|
61
|
+
peerConn.setRemoteDescription(new RTCSessionDescription(remoteDescription))
|
|
62
|
+
|
|
63
|
+
const answer = await peerConn.createAnswer();
|
|
64
|
+
await peerConn.setLocalDescription(answer);
|
|
65
|
+
|
|
66
|
+
resolve(peerConn);
|
|
64
67
|
} catch (error) {
|
|
65
68
|
reject(error);
|
|
66
69
|
}
|
|
@@ -82,6 +85,23 @@ class WebRTC {
|
|
|
82
85
|
|
|
83
86
|
return peerConn;
|
|
84
87
|
}
|
|
88
|
+
|
|
89
|
+
/*
|
|
90
|
+
static-auth credentials
|
|
91
|
+
https://eturnal.net/documentation/
|
|
92
|
+
https://datatracker.ietf.org/doc/html/draft-uberti-behave-turn-rest-00
|
|
93
|
+
*/
|
|
94
|
+
static getTurnCredentials() {
|
|
95
|
+
let crypto = require('crypto');
|
|
96
|
+
let username = String(parseInt(Date.now() / 1000) + 24 * 3600); //ttl: 24h
|
|
97
|
+
let hmac = crypto.createHmac('sha1', this.turnSecret);
|
|
98
|
+
|
|
99
|
+
hmac.setEncoding('base64');
|
|
100
|
+
hmac.write(username);
|
|
101
|
+
hmac.end();
|
|
102
|
+
|
|
103
|
+
return { username: username, password: hmac.read() };
|
|
104
|
+
}
|
|
85
105
|
}
|
|
86
106
|
|
|
87
107
|
module.exports = { WebRTC };
|
package/web_socket.js
CHANGED
|
@@ -8,34 +8,82 @@ class WebSocket {
|
|
|
8
8
|
const { EventBus } = require('./event_bus');
|
|
9
9
|
|
|
10
10
|
this.eventBus = new EventBus();
|
|
11
|
+
this.socketConnectionErrors = ['connect_error', 'connect_timeout', 'reconnect_error', 'reconnect_failed'];
|
|
11
12
|
this.socketEvents = socketEvents;
|
|
12
13
|
this.busEvents = busEvents;
|
|
13
14
|
this.customBusEvents = customBusEvents;
|
|
14
15
|
}
|
|
15
16
|
|
|
16
17
|
static connect(url, options, connectInitPayload, onUnavailableCallback, onConnectionAckCallback) {
|
|
17
|
-
|
|
18
|
-
|
|
19
|
-
|
|
18
|
+
const { io } = require('socket.io-client');
|
|
19
|
+
|
|
20
|
+
this.logged = false;
|
|
20
21
|
this.instance = io(url, options);
|
|
21
22
|
|
|
22
|
-
this.
|
|
23
|
-
this.
|
|
23
|
+
this.registerErrorListeners(onUnavailableCallback);
|
|
24
|
+
this.registerEventListeners(connectInitPayload, onConnectionAckCallback, onUnavailableCallback);
|
|
25
|
+
}
|
|
26
|
+
|
|
27
|
+
static registerErrorListeners(onUnavailableCallback) {
|
|
28
|
+
var self = this;
|
|
29
|
+
|
|
30
|
+
self.socketConnectionErrors.forEach(function(event) {
|
|
31
|
+
self.instance.on(event, function() {
|
|
32
|
+
self.clear(onUnavailableCallback, event);
|
|
33
|
+
});
|
|
34
|
+
});
|
|
35
|
+
|
|
36
|
+
self.instance.on('error', function(error) {
|
|
37
|
+
self.clear(onUnavailableCallback, error);
|
|
38
|
+
});
|
|
39
|
+
}
|
|
40
|
+
|
|
41
|
+
static registerEventListeners(connectInitPayload, onConnectionAckCallback, onUnavailableCallback) {
|
|
42
|
+
var self = this;
|
|
24
43
|
|
|
25
|
-
|
|
26
|
-
if (
|
|
27
|
-
|
|
44
|
+
self.instance.on('connect', function() {
|
|
45
|
+
if (self.logged) { //reconnexion avant timeout : on supprime les listeners de l'event bus
|
|
46
|
+
self.clear();
|
|
28
47
|
}
|
|
29
48
|
|
|
30
|
-
|
|
49
|
+
self.logged = true;
|
|
50
|
+
|
|
51
|
+
self.instance.emit('connect_init', connectInitPayload, onConnectionAckCallback);
|
|
52
|
+
});
|
|
31
53
|
|
|
32
|
-
|
|
54
|
+
self.instance.on('disconnect', function() {
|
|
55
|
+
self.clear(onUnavailableCallback);
|
|
56
|
+
});
|
|
57
|
+
|
|
58
|
+
self.socketEvents.forEach(function (event) {
|
|
59
|
+
self.instance.on(event, function(payload) {
|
|
60
|
+
self.eventBus.publish(event, payload);
|
|
61
|
+
});
|
|
62
|
+
});
|
|
63
|
+
|
|
64
|
+
self.busEvents.forEach(function(event) {
|
|
65
|
+
self.eventBus.subscribe(event, function(payload) {
|
|
66
|
+
self.instance.emit(event, payload);
|
|
67
|
+
});
|
|
68
|
+
});
|
|
69
|
+
|
|
70
|
+
self.customBusEvents.forEach(function(object) {
|
|
71
|
+
self.eventBus.subscribe(object.custom_name, function(payload) {
|
|
72
|
+
self.instance.emit(object.event_name, payload)
|
|
73
|
+
});
|
|
33
74
|
});
|
|
34
75
|
}
|
|
35
76
|
|
|
36
|
-
static
|
|
37
|
-
|
|
38
|
-
|
|
77
|
+
static unregisterEventListener() {
|
|
78
|
+
var self = this;
|
|
79
|
+
|
|
80
|
+
self.busEvents.forEach(function (event) {
|
|
81
|
+
self.eventBus.unsubscribe(event, {});
|
|
82
|
+
});
|
|
83
|
+
|
|
84
|
+
self.customBusEvents.forEach(function(object) {
|
|
85
|
+
self.eventBus.unsubscribe(object.custom_name, {});
|
|
86
|
+
});
|
|
39
87
|
}
|
|
40
88
|
|
|
41
89
|
static clear(onUnavailableCallback, error) {
|
|
@@ -45,17 +93,6 @@ class WebSocket {
|
|
|
45
93
|
onUnavailableCallback(error);
|
|
46
94
|
}
|
|
47
95
|
}
|
|
48
|
-
|
|
49
|
-
static initEventListeners() {
|
|
50
|
-
this.socketEvents.forEach(event => this.instance.on(event, (payload) => this.eventBus.publish(event, payload)));
|
|
51
|
-
this.busEvents.forEach(event => this.eventBus.subscribe(event, (payload) => this.instance.emit(event, payload)));
|
|
52
|
-
this.customBusEvents.forEach((object) => this.eventBus.subscribe(object.custom_name, (payload) => this.instance.emit(object.event_name, payload)));
|
|
53
|
-
}
|
|
54
|
-
|
|
55
|
-
static unregisterEventListener() {
|
|
56
|
-
this.busEvents.forEach(event => this.eventBus.unsubscribe(event, {}));
|
|
57
|
-
this.customBusEvents.forEach((object) => this.eventBus.unsubscribe(object.custom_name, {}));
|
|
58
|
-
}
|
|
59
96
|
}
|
|
60
97
|
|
|
61
98
|
module.exports = { WebSocket };
|