-
Notifications
You must be signed in to change notification settings - Fork 3
/
Copy pathnode_helper.js
55 lines (48 loc) · 1.6 KB
/
node_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
43
44
45
46
47
48
49
50
51
52
53
54
55
const NodeHelper = require('node_helper');
const fetch = require('node-fetch');
const { URLSearchParams } = require('url');
module.exports = NodeHelper.create({
start: function() {
console.log('Starting node_helper for module [' + this.name + ']');
},
sendOffer: function(data) {
const url = this.buildUrl(data.config);
const headers = {};
if (data.config.token) {
headers['Authorization'] = 'Bearer ' + data.config.token;
}
const params = new URLSearchParams();
params.append('url', data.config.url);
params.append('sdp', data.sdp);
fetch(url, {method: 'POST', body: params, headers})
.then((response) => {
if (response.ok) {
response.json().then((data) => this.sendSocketNotification('ANSWER', data));
} else {
throw new Error('Response is not ok');
}
})
.catch((error) => {
console.error(this.name + ' ERROR:', error);
setTimeout(() => this.sendOffer(data), 3000);
});
},
buildUrl: function(config) {
let url = config.host;
if (config.port) {
url += ':' + config.port;
}
url += '/api/webrtc/stream';
if (config.https) {
url = 'https://' + url;
} else {
url = 'http://' + url;
}
return url;
},
socketNotificationReceived: function(notification, payload) {
if (notification === 'OFFER') {
this.sendOffer(payload);
}
}
});