@subiz/wsclient 4.0.26 → 4.0.27

This diff represents the content of publicly available package versions that have been released to one of the supported registries. The information contained in this diff is provided for informational purposes only and reflects changes between package versions as they appear in their respective public registries.
Files changed (2) hide show
  1. package/package.json +1 -1
  2. package/webrtc.js +12 -3
package/package.json CHANGED
@@ -1,6 +1,6 @@
1
1
  {
2
2
  "name": "@subiz/wsclient",
3
- "version": "4.0.26",
3
+ "version": "4.0.27",
4
4
  "main": "realtime.js",
5
5
  "scripts": {
6
6
  "dev": "vite",
package/webrtc.js CHANGED
@@ -5,7 +5,7 @@ const servers = {
5
5
  }
6
6
 
7
7
  // let connId = 'webrtc' + randomString(20)
8
- // env = {RTCPeerConnection, RTCIceCandidate, RTCSessionDescription, setTimeout, setInterval, jsonify, parseJSON}
8
+ // env = {RTCPeerConnection, RTCIceCandidate, RTCSessionDescription, setTimeout, setInterval, clearInterval, jsonify, parseJSON}
9
9
  // set env._is_webrtc_local to true for local testing
10
10
  function WebRTCConn(options) {
11
11
  let {apiUrl, access_token, accid, agid, realtime, callAPI, env, onTrack, onEvent, collect} = options
@@ -15,6 +15,7 @@ function WebRTCConn(options) {
15
15
  if (env._is_webrtc_local) apiUrl = 'http://localhost:8008/'
16
16
  }
17
17
 
18
+ dead = false
18
19
  callAPI = callAPI || xhrsend // allow hook
19
20
  let parseJSON = (str) => {
20
21
  try {
@@ -48,6 +49,7 @@ function WebRTCConn(options) {
48
49
 
49
50
  peer.onicecandidate = (event) => {
50
51
  if (!event.candidate) return
52
+ if (dead) return
51
53
  let url = `${apiUrl}ice-candidates?x-access-token=${access_token}&connection_id=${connId}&account_id=${accid}`
52
54
  callAPI('post', url, jsonify(event.candidate))
53
55
  }
@@ -86,6 +88,7 @@ function WebRTCConn(options) {
86
88
  }
87
89
 
88
90
  let syncCallWithServer = () => {
91
+ if (dead) return env.clearInterval(_syncCallInterval)
89
92
  let url = `${apiUrl}calls?x-access-token=${access_token}&account_id=${accid}`
90
93
  callAPI('get', url, undefined, function (body, code) {
91
94
  if (code != 200) return
@@ -141,9 +144,10 @@ function WebRTCConn(options) {
141
144
  })
142
145
  }
143
146
  syncCallWithServer()
144
- env.setInterval(syncCallWithServer, 10000)
147
+ let _syncCallInterval = env.setInterval(syncCallWithServer, 10000)
145
148
 
146
149
  let cleanEndedCall = () => {
150
+ if (dead) return env.clearInterval(_endcalInterval)
147
151
  Object.keys(endRequest).map((callid) => {
148
152
  if (env.Date.now() - endRequest[callid] > 60000) delete endRequest[callid]
149
153
  })
@@ -166,7 +170,7 @@ function WebRTCConn(options) {
166
170
  if (env.Date.now() - call.ended > 60000) delete call2Connection[callid]
167
171
  })
168
172
  }
169
- env.setInterval(cleanEndedCall, 120000)
173
+ let _endcalInterval = env.setInterval(cleanEndedCall, 120000)
170
174
 
171
175
  let endcallCb = {}
172
176
  let publish = (ev) => {
@@ -197,6 +201,10 @@ function WebRTCConn(options) {
197
201
  onEvent && onEvent(ev)
198
202
  }
199
203
 
204
+ this.shutdown = () => {
205
+ dead = true
206
+ }
207
+
200
208
  this.matchCall = (callid) => {
201
209
  if (!callid || callid == '*' || callid == '-') {
202
210
  let calls = Object.assign({}, serverCalls)
@@ -252,6 +260,7 @@ function WebRTCConn(options) {
252
260
  }
253
261
 
254
262
  realtime.onEvent((ev) => {
263
+ if (dead) return
255
264
  if (!ev || !ev.type || !ev.data) return
256
265
  if (env._is_webrtc_local && ev.user_id != '8') return // local
257
266
  if (!env._is_webrtc_local && ev.user_id != '9') return // prod