|
|
@@ -25,14 +25,16 @@ JSMpeg.Source.WebSocket = (function () {
|
|
25
|
25
|
|
|
26
|
26
|
// by kerry
|
|
27
|
27
|
this.wsId = options.wsId;
|
|
28
|
|
- this.onWsPreConnection =
|
|
29
|
|
- options.onWsPreConnection !== undefined
|
|
30
|
|
- ? options.onWsPreConnection
|
|
31
|
|
- : function (wsId, url) {
|
|
32
|
|
- return url;
|
|
33
|
|
- };
|
|
|
28
|
+ this.onWsPreConnection = options.onWsPreConnection;
|
|
|
29
|
+ if (options.onWsPreConnection === undefined) {
|
|
|
30
|
+ this.onWsPreConnectionAsync = options.onWsPreConnectionAsync;
|
|
|
31
|
+ }
|
|
34
|
32
|
this.onWsOpen =
|
|
35
|
33
|
options.onWsOpen === undefined ? function () {} : options.onWsOpen;
|
|
|
34
|
+ this.onWsError =
|
|
|
35
|
+ options.onWsError === undefined
|
|
|
36
|
+ ? function (isReconnect) {}
|
|
|
37
|
+ : options.onWsError;
|
|
36
|
38
|
this.onWsClose =
|
|
37
|
39
|
options.onWsClose !== undefined
|
|
38
|
40
|
? options.onWsClose
|
|
|
@@ -51,21 +53,38 @@ JSMpeg.Source.WebSocket = (function () {
|
|
51
|
53
|
|
|
52
|
54
|
WSSource.prototype.start = function () {
|
|
53
|
55
|
// update kerry
|
|
54
|
|
- var temp = this.onWsPreConnection(this.wsId, this.url);
|
|
55
|
|
- if (temp !== undefined) {
|
|
56
|
|
- this.url = temp;
|
|
|
56
|
+ if (this.onWsPreConnectionAsync !== undefined) {
|
|
|
57
|
+ // this.url = this.onWsPreConnection(this.wsId, this.url);
|
|
|
58
|
+ var that = this;
|
|
|
59
|
+ that.onWsPreConnectionAsync(that.wsId, that.url, function (newUrl) {
|
|
|
60
|
+ that.url = newUrl;
|
|
|
61
|
+
|
|
|
62
|
+ that.shouldAttemptReconnect = !!that.reconnectInterval;
|
|
|
63
|
+ that.progress = 0;
|
|
|
64
|
+ that.established = false;
|
|
|
65
|
+
|
|
|
66
|
+ that.socket = new WebSocket(that.url, that.options.protocols || null);
|
|
|
67
|
+ that.socket.binaryType = "arraybuffer";
|
|
|
68
|
+ that.socket.onmessage = that.onMessage.bind(that);
|
|
|
69
|
+ that.socket.onopen = that.onOpen.bind(that);
|
|
|
70
|
+ that.socket.onerror = that.onError.bind(that);
|
|
|
71
|
+ that.socket.onclose = that.onClose.bind(that);
|
|
|
72
|
+ });
|
|
|
73
|
+ } else {
|
|
|
74
|
+ if (this.onWsPreConnection !== undefined) {
|
|
|
75
|
+ this.url = this.onWsPreConnection(this.wsId, this.url);
|
|
|
76
|
+ }
|
|
|
77
|
+ this.shouldAttemptReconnect = !!this.reconnectInterval;
|
|
|
78
|
+ this.progress = 0;
|
|
|
79
|
+ this.established = false;
|
|
|
80
|
+
|
|
|
81
|
+ this.socket = new WebSocket(this.url, this.options.protocols || null);
|
|
|
82
|
+ this.socket.binaryType = "arraybuffer";
|
|
|
83
|
+ this.socket.onmessage = this.onMessage.bind(this);
|
|
|
84
|
+ this.socket.onopen = this.onOpen.bind(this);
|
|
|
85
|
+ this.socket.onerror = this.onError.bind(this);
|
|
|
86
|
+ this.socket.onclose = this.onClose.bind(this);
|
|
57
|
87
|
}
|
|
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
|
88
|
};
|
|
70
|
89
|
|
|
71
|
90
|
WSSource.prototype.resume = function (secondsHeadroom) {
|
|
|
@@ -93,6 +112,21 @@ JSMpeg.Source.WebSocket = (function () {
|
|
93
|
112
|
}
|
|
94
|
113
|
};
|
|
95
|
114
|
|
|
|
115
|
+ WSSource.prototype.onError = function () {
|
|
|
116
|
+ // update kerry
|
|
|
117
|
+ this.onWsError(this.shouldAttemptReconnect);
|
|
|
118
|
+
|
|
|
119
|
+ if (this.shouldAttemptReconnect) {
|
|
|
120
|
+ clearTimeout(this.reconnectTimeoutId);
|
|
|
121
|
+ this.reconnectTimeoutId = setTimeout(
|
|
|
122
|
+ function () {
|
|
|
123
|
+ this.start();
|
|
|
124
|
+ }.bind(this),
|
|
|
125
|
+ this.reconnectInterval * 1000
|
|
|
126
|
+ );
|
|
|
127
|
+ }
|
|
|
128
|
+ };
|
|
|
129
|
+
|
|
96
|
130
|
WSSource.prototype.onMessage = function (ev) {
|
|
97
|
131
|
var isFirstChunk = !this.established;
|
|
98
|
132
|
this.established = true;
|