张泳健 5 лет назад
Родитель
Сommit
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
 ## Linux 编译
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
 cd emsdk
7
 cd emsdk
8
 # 下面的版本查看 build.sh 中的注释查看
8
 # 下面的版本查看 build.sh 中的注释查看
9
 ./emsdk install 1.38.47
9
 ./emsdk install 1.38.47
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
   - wsId: 手动指定的.
31
   - wsId: 手动指定的.
32
   - oldUrl: 旧的URL
32
   - oldUrl: 旧的URL
33
+  - callback(newUrl): 原型: function(newUrl){},在websocket 连接之前调用,回调计算后的新 `URL`
34
+- onWsPreConnection: function(wsId, oldUrl) { return newUrl; } 在websocket 连接之前调用,返回return oldUrl ; } 在websocket 连接之前调用,返回计算后的新 `URL` ,同步方式 
33
 - onWsOpen: function(){}  websocket连接成功后,调用
35
 - onWsOpen: function(){}  websocket连接成功后,调用
34
 - onWsClose: function (isReconnect) : websocket 断开后调用,
36
 - onWsClose: function (isReconnect) : websocket 断开后调用,
35
   - isReconnect: 为是否会重新连接
37
   - isReconnect: 为是否会重新连接

Разница между файлами не показана из-за своего большого размера
+ 1 - 1
jsmpeg.min.js


+ 54 - 20
src/websocket.js Просмотреть файл

25
 
25
 
26
     // by kerry
26
     // by kerry
27
     this.wsId = options.wsId;
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
     this.onWsOpen =
32
     this.onWsOpen =
35
       options.onWsOpen === undefined ? function () {} : options.onWsOpen;
33
       options.onWsOpen === undefined ? function () {} : options.onWsOpen;
34
+    this.onWsError =
35
+      options.onWsError === undefined
36
+        ? function (isReconnect) {}
37
+        : options.onWsError;
36
     this.onWsClose =
38
     this.onWsClose =
37
       options.onWsClose !== undefined
39
       options.onWsClose !== undefined
38
         ? options.onWsClose
40
         ? options.onWsClose
51
 
53
 
52
   WSSource.prototype.start = function () {
54
   WSSource.prototype.start = function () {
53
     // update kerry
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
   WSSource.prototype.resume = function (secondsHeadroom) {
90
   WSSource.prototype.resume = function (secondsHeadroom) {
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
   WSSource.prototype.onMessage = function (ev) {
130
   WSSource.prototype.onMessage = function (ev) {
97
     var isFirstChunk = !this.established;
131
     var isFirstChunk = !this.established;
98
     this.established = true;
132
     this.established = true;

+ 3 - 2
view-stream.html Просмотреть файл

19
       var player = new JSMpeg.Player("DynamicWebsocketUrl", {
19
       var player = new JSMpeg.Player("DynamicWebsocketUrl", {
20
         canvas: canvas,
20
         canvas: canvas,
21
         wsId: "websocketID",
21
         wsId: "websocketID",
22
-        onWsPreConnection: function (wsId, url) {
22
+        reconnectInterval: 10,
23
+        onWsPreConnection: function (wsId, url, callback) {
23
           console.log(wsId);
24
           console.log(wsId);
24
           console.log(url);
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
         onWsOpen: function () {
28
         onWsOpen: function () {
28
           console.log("connection success!!");
29
           console.log("connection success!!");