Преглед на файлове

add sync and async preconnect

张泳健 преди 5 години
родител
ревизия
5516a95260
променени са 4 файла, в които са добавени 62 реда и са изтрити 25 реда
  1. 4 2
      KERRY.md
  2. 1 1
      jsmpeg.min.js
  3. 54 20
      src/websocket.js
  4. 3 2
      view-stream.html

+ 4 - 2
KERRY.md Целия файл

@@ -3,7 +3,7 @@
3 3
 ## Linux 编译
4 4
 
5 5
 ```
6
-https://github.com/emscripten-core/emsdk.git
6
+git clone https://github.com/emscripten-core/emsdk.git
7 7
 cd emsdk
8 8
 # 下面的版本查看 build.sh 中的注释查看
9 9
 ./emsdk install 1.38.47
@@ -27,9 +27,11 @@ npm install uglify-js -g
27 27
 
28 28
 #### 添加函数
29 29
 
30
-- onWsPreConnection: function(wsId, oldUrl) { return oldUrl ; } 在websocket 连接之前调用,返回return oldUrl ; } 在websocket 连接之前调用,返回计算后的新 `URL`
30
+- onWsPreConnectionAsync: function(wsId, oldUrl, callback) { callback(newUrl); } 在websocket 连接之前调用, 在websocket 连接之前调用,回调计算后的新 `URL` ,异步方式 
31 31
   - wsId: 手动指定的.
32 32
   - oldUrl: 旧的URL
33
+  - callback(newUrl): 原型: function(newUrl){},在websocket 连接之前调用,回调计算后的新 `URL`
34
+- onWsPreConnection: function(wsId, oldUrl) { return newUrl; } 在websocket 连接之前调用,返回return oldUrl ; } 在websocket 连接之前调用,返回计算后的新 `URL` ,同步方式 
33 35
 - onWsOpen: function(){}  websocket连接成功后,调用
34 36
 - onWsClose: function (isReconnect) : websocket 断开后调用,
35 37
   - isReconnect: 为是否会重新连接

Файловите разлики са ограничени, защото са твърде много
+ 1 - 1
jsmpeg.min.js


+ 54 - 20
src/websocket.js Целия файл

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

+ 3 - 2
view-stream.html Целия файл

@@ -19,10 +19,11 @@
19 19
       var player = new JSMpeg.Player("DynamicWebsocketUrl", {
20 20
         canvas: canvas,
21 21
         wsId: "websocketID",
22
-        onWsPreConnection: function (wsId, url) {
22
+        reconnectInterval: 10,
23
+        onWsPreConnection: function (wsId, url, callback) {
23 24
           console.log(wsId);
24 25
           console.log(url);
25
-          return "ws://127.0.0.1:8082/live/test";
26
+          callback("ws://127.0.0.1:8082/live/test");
26 27
         },
27 28
         onWsOpen: function () {
28 29
           console.log("connection success!!");