forked from rtc-io/rtc-tools
-
Notifications
You must be signed in to change notification settings - Fork 0
/
cleanup.js
51 lines (42 loc) · 1.07 KB
/
cleanup.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
/* jshint node: true */
'use strict';
var debug = require('cog/logger')('rtc/cleanup');
var CANNOT_CLOSE_STATES = [
'closed'
];
var EVENTNAMES = [
'addstream',
'datachannel',
'icecandidate',
'iceconnectionstatechange',
'negotiationneeded',
'removestream',
'signalingstatechange'
];
/**
### rtc/cleanup
```
cleanup(pc)
```
The `cleanup` function is used to ensure that a peer connection is properly
closed and ready to be cleaned up by the browser.
**/
module.exports = function(pc) {
// see if we can close the connection
var currentState = pc.iceConnectionState;
var canClose = CANNOT_CLOSE_STATES.indexOf(currentState) < 0;
if (canClose) {
debug('attempting connection close, current state: '+ pc.iceConnectionState);
pc.close();
}
// remove the event listeners
// after a short delay giving the connection time to trigger
// close and iceconnectionstatechange events
setTimeout(function() {
EVENTNAMES.forEach(function(evtName) {
if (pc['on' + evtName]) {
pc['on' + evtName] = null;
}
});
}, 100);
};