|
@@ -1,83 +1,110 @@
|
1
|
|
-JSMpeg.Source.WebSocket = (function(){ "use strict";
|
2
|
|
-
|
3
|
|
-var WSSource = function(url, options) {
|
4
|
|
- this.url = url;
|
5
|
|
- this.options = options;
|
6
|
|
- this.socket = null;
|
7
|
|
- this.streaming = true;
|
8
|
|
-
|
9
|
|
- this.callbacks = {connect: [], data: []};
|
10
|
|
- this.destination = null;
|
11
|
|
-
|
12
|
|
- this.reconnectInterval = options.reconnectInterval !== undefined
|
13
|
|
- ? options.reconnectInterval
|
14
|
|
- : 5;
|
15
|
|
- this.shouldAttemptReconnect = !!this.reconnectInterval;
|
16
|
|
-
|
17
|
|
- this.completed = false;
|
18
|
|
- this.established = false;
|
19
|
|
- this.progress = 0;
|
20
|
|
-
|
21
|
|
- this.reconnectTimeoutId = 0;
|
22
|
|
-
|
23
|
|
- this.onEstablishedCallback = options.onSourceEstablished;
|
24
|
|
- this.onCompletedCallback = options.onSourceCompleted; // Never used
|
25
|
|
-};
|
26
|
|
-
|
27
|
|
-WSSource.prototype.connect = function(destination) {
|
28
|
|
- this.destination = destination;
|
29
|
|
-};
|
30
|
|
-
|
31
|
|
-WSSource.prototype.destroy = function() {
|
32
|
|
- clearTimeout(this.reconnectTimeoutId);
|
33
|
|
- this.shouldAttemptReconnect = false;
|
34
|
|
- this.socket.close();
|
35
|
|
-};
|
36
|
|
-
|
37
|
|
-WSSource.prototype.start = function() {
|
38
|
|
- this.shouldAttemptReconnect = !!this.reconnectInterval;
|
39
|
|
- this.progress = 0;
|
40
|
|
- this.established = false;
|
41
|
|
-
|
42
|
|
- this.socket = new WebSocket(this.url, this.options.protocols || null);
|
43
|
|
- this.socket.binaryType = 'arraybuffer';
|
44
|
|
- this.socket.onmessage = this.onMessage.bind(this);
|
45
|
|
- this.socket.onopen = this.onOpen.bind(this);
|
46
|
|
- this.socket.onerror = this.onClose.bind(this);
|
47
|
|
- this.socket.onclose = this.onClose.bind(this);
|
48
|
|
-};
|
49
|
|
-
|
50
|
|
-WSSource.prototype.resume = function(secondsHeadroom) {
|
51
|
|
- // Nothing to do here
|
52
|
|
-};
|
53
|
|
-
|
54
|
|
-WSSource.prototype.onOpen = function() {
|
55
|
|
- this.progress = 1;
|
56
|
|
-};
|
57
|
|
-
|
58
|
|
-WSSource.prototype.onClose = function() {
|
59
|
|
- if (this.shouldAttemptReconnect) {
|
60
|
|
- clearTimeout(this.reconnectTimeoutId);
|
61
|
|
- this.reconnectTimeoutId = setTimeout(function(){
|
62
|
|
- this.start();
|
63
|
|
- }.bind(this), this.reconnectInterval*1000);
|
64
|
|
- }
|
65
|
|
-};
|
66
|
|
-
|
67
|
|
-WSSource.prototype.onMessage = function(ev) {
|
68
|
|
- var isFirstChunk = !this.established;
|
69
|
|
- this.established = true;
|
70
|
|
-
|
71
|
|
- if (isFirstChunk && this.onEstablishedCallback) {
|
72
|
|
- this.onEstablishedCallback(this);
|
73
|
|
- }
|
74
|
|
-
|
75
|
|
- if (this.destination) {
|
76
|
|
- this.destination.write(ev.data);
|
77
|
|
- }
|
78
|
|
-};
|
79
|
|
-
|
80
|
|
-return WSSource;
|
81
|
|
-
|
|
1
|
+JSMpeg.Source.WebSocket = (function () {
|
|
2
|
+ "use strict";
|
|
3
|
+
|
|
4
|
+ var WSSource = function (url, options) {
|
|
5
|
+ this.url = url;
|
|
6
|
+ this.options = options;
|
|
7
|
+ this.socket = null;
|
|
8
|
+ this.streaming = true;
|
|
9
|
+
|
|
10
|
+ this.callbacks = { connect: [], data: [] };
|
|
11
|
+ this.destination = null;
|
|
12
|
+
|
|
13
|
+ this.reconnectInterval =
|
|
14
|
+ options.reconnectInterval !== undefined ? options.reconnectInterval : 5;
|
|
15
|
+ this.shouldAttemptReconnect = !!this.reconnectInterval;
|
|
16
|
+
|
|
17
|
+ this.completed = false;
|
|
18
|
+ this.established = false;
|
|
19
|
+ this.progress = 0;
|
|
20
|
+
|
|
21
|
+ this.reconnectTimeoutId = 0;
|
|
22
|
+
|
|
23
|
+ this.onEstablishedCallback = options.onSourceEstablished;
|
|
24
|
+ this.onCompletedCallback = options.onSourceCompleted; // Never used
|
|
25
|
+
|
|
26
|
+ // by kerry
|
|
27
|
+ this.wsId = options.wsId;
|
|
28
|
+ this.onWsPreConnection =
|
|
29
|
+ options.onWsPreConnection !== undefined
|
|
30
|
+ ? options.onWsPreConnection
|
|
31
|
+ : function (wsId, url) {
|
|
32
|
+ return url;
|
|
33
|
+ };
|
|
34
|
+ this.onWsOpen =
|
|
35
|
+ options.onWsOpen === undefined ? function () {} : options.onWsOpen;
|
|
36
|
+ this.onWsClose =
|
|
37
|
+ options.onWsClose !== undefined
|
|
38
|
+ ? options.onWsClose
|
|
39
|
+ : function (isReconnect) {};
|
|
40
|
+ };
|
|
41
|
+
|
|
42
|
+ WSSource.prototype.connect = function (destination) {
|
|
43
|
+ this.destination = destination;
|
|
44
|
+ };
|
|
45
|
+
|
|
46
|
+ WSSource.prototype.destroy = function () {
|
|
47
|
+ clearTimeout(this.reconnectTimeoutId);
|
|
48
|
+ this.shouldAttemptReconnect = false;
|
|
49
|
+ this.socket.close();
|
|
50
|
+ };
|
|
51
|
+
|
|
52
|
+ WSSource.prototype.start = function () {
|
|
53
|
+ // update kerry
|
|
54
|
+ var temp = this.onWsPreConnection(this.wsId, this.url);
|
|
55
|
+ if (temp !== undefined) {
|
|
56
|
+ this.url = temp;
|
|
57
|
+ }
|
|
58
|
+
|
|
59
|
+ this.shouldAttemptReconnect = !!this.reconnectInterval;
|
|
60
|
+ this.progress = 0;
|
|
61
|
+ this.established = false;
|
|
62
|
+
|
|
63
|
+ this.socket = new WebSocket(this.url, this.options.protocols || null);
|
|
64
|
+ this.socket.binaryType = "arraybuffer";
|
|
65
|
+ this.socket.onmessage = this.onMessage.bind(this);
|
|
66
|
+ this.socket.onopen = this.onOpen.bind(this);
|
|
67
|
+ this.socket.onerror = this.onClose.bind(this);
|
|
68
|
+ this.socket.onclose = this.onClose.bind(this);
|
|
69
|
+ };
|
|
70
|
+
|
|
71
|
+ WSSource.prototype.resume = function (secondsHeadroom) {
|
|
72
|
+ // Nothing to do here
|
|
73
|
+ };
|
|
74
|
+
|
|
75
|
+ WSSource.prototype.onOpen = function () {
|
|
76
|
+ // update kerry
|
|
77
|
+ this.onWsOpen();
|
|
78
|
+ this.progress = 1;
|
|
79
|
+ };
|
|
80
|
+
|
|
81
|
+ WSSource.prototype.onClose = function () {
|
|
82
|
+ // update kerry
|
|
83
|
+ this.onWsClose(this.shouldAttemptReconnect);
|
|
84
|
+
|
|
85
|
+ if (this.shouldAttemptReconnect) {
|
|
86
|
+ clearTimeout(this.reconnectTimeoutId);
|
|
87
|
+ this.reconnectTimeoutId = setTimeout(
|
|
88
|
+ function () {
|
|
89
|
+ this.start();
|
|
90
|
+ }.bind(this),
|
|
91
|
+ this.reconnectInterval * 1000
|
|
92
|
+ );
|
|
93
|
+ }
|
|
94
|
+ };
|
|
95
|
+
|
|
96
|
+ WSSource.prototype.onMessage = function (ev) {
|
|
97
|
+ var isFirstChunk = !this.established;
|
|
98
|
+ this.established = true;
|
|
99
|
+
|
|
100
|
+ if (isFirstChunk && this.onEstablishedCallback) {
|
|
101
|
+ this.onEstablishedCallback(this);
|
|
102
|
+ }
|
|
103
|
+
|
|
104
|
+ if (this.destination) {
|
|
105
|
+ this.destination.write(ev.data);
|
|
106
|
+ }
|
|
107
|
+ };
|
|
108
|
+
|
|
109
|
+ return WSSource;
|
82
|
110
|
})();
|
83
|
|
-
|