Browse Source

add websocket event listener by kerry

张泳健 5 years ago
parent
commit
f5e1ffddd7
4 changed files with 148 additions and 102 deletions
  1. 1 1
      jsmpeg.min.js
  2. 4 0
      src/player.js
  3. 109 82
      src/websocket.js
  4. 34 19
      view-stream.html

File diff suppressed because it is too large
+ 1 - 1
jsmpeg.min.js


+ 4 - 0
src/player.js View File

@@ -11,6 +11,10 @@ var Player = function(url, options) {
11 11
 		this.source = new JSMpeg.Source.WebSocket(url, options);
12 12
 		options.streaming = true;
13 13
 	}
14
+	else if (url.toLowerCase() === 'dynamicwebsocketurl') {
15
+		this.source = new JSMpeg.Source.WebSocket(url, options);
16
+		options.streaming = true;
17
+	}
14 18
 	else if (options.progressive !== false) {
15 19
 		this.source = new JSMpeg.Source.AjaxProgressive(url, options);
16 20
 		options.streaming = false;

+ 109 - 82
src/websocket.js View File

@@ -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
-

+ 34 - 19
view-stream.html View File

@@ -1,22 +1,37 @@
1 1
 <!DOCTYPE html>
2 2
 <html>
3
-<head>
4
-	<title>JSMpeg Stream Client</title>
5
-	<style type="text/css">
6
-		html, body {
7
-			background-color: #111;
8
-			text-align: center;
9
-		}
10
-	</style>
11
-	
12
-</head>
13
-<body>
14
-	<canvas id="video-canvas"></canvas>
15
-	<script type="text/javascript" src="jsmpeg.min.js"></script>
16
-	<script type="text/javascript">
17
-		var canvas = document.getElementById('video-canvas');
18
-		var url = 'ws://'+document.location.hostname+':8082/';
19
-		var player = new JSMpeg.Player(url, {canvas: canvas});
20
-	</script>
21
-</body>
3
+  <head>
4
+    <title>JSMpeg Stream Client</title>
5
+    <style type="text/css">
6
+      html,
7
+      body {
8
+        background-color: #111;
9
+        text-align: center;
10
+      }
11
+    </style>
12
+  </head>
13
+  <body>
14
+    <canvas id="video-canvas"></canvas>
15
+    <script type="text/javascript" src="jsmpeg.min.js"></script>
16
+    <script type="text/javascript">
17
+      var canvas = document.getElementById("video-canvas");
18
+      //var url = "ws://127.0.0.1:8082/live/test";
19
+      var player = new JSMpeg.Player("DynamicWebsocketUrl", {
20
+        canvas: canvas,
21
+        wsId: "websocketID",
22
+        onWsPreConnection: function (wsId, url) {
23
+          console.log(wsId);
24
+          console.log(url);
25
+          return "ws://127.0.0.1:8082/live/test";
26
+        },
27
+        onWsOpen: function () {
28
+          console.log("connection success!!");
29
+        },
30
+        onWsClose: function (isReconnect) {
31
+          console.log("close is reconnect");
32
+          console.log(isReconnect);
33
+        },
34
+      });
35
+    </script>
36
+  </body>
22 37
 </html>