@subiz/wsclient 4.0.6 → 4.0.8

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 +2 -2
  2. package/webrtc.js +30 -17
package/package.json CHANGED
@@ -1,6 +1,6 @@
1
1
  {
2
2
  "name": "@subiz/wsclient",
3
- "version": "4.0.6",
3
+ "version": "4.0.8",
4
4
  "main": "realtime.js",
5
5
  "scripts": {
6
6
  "test": "node realtime.test.js"
@@ -14,7 +14,7 @@
14
14
  "homepage": "https://github.com/subiz/wsclient#readme",
15
15
  "description": "",
16
16
  "devDependencies": {
17
- "tape": "^5.6.1"
17
+ "tape": "^5.6.3"
18
18
  },
19
19
  "dependencies": {
20
20
  "@subiz/flow": "^1.0.9",
package/webrtc.js CHANGED
@@ -15,6 +15,7 @@ function WebRTCConn(options) {
15
15
  }
16
16
 
17
17
  callAPI = callAPI || xhrsend // allow hook
18
+ let callStream = {} // callid => stream
18
19
 
19
20
  let parseJSON = (str) => {
20
21
  try {
@@ -89,13 +90,12 @@ function WebRTCConn(options) {
89
90
  console.log('NEW PERR')
90
91
  }
91
92
 
92
- let stopMic = () => {
93
- console.log('00000000STOP TRACK', currentStream)
94
- return
95
- currentStream &&
96
- currentStream.getTracks().forEach((track) => {
97
- track.enabled = false
98
- })
93
+ let stopStream = (stream) => {
94
+ if (!stream) return
95
+ stream.getTracks().forEach((track) => {
96
+ track.stop()
97
+ track.enabled = false
98
+ })
99
99
  }
100
100
 
101
101
  // auto tear down when byte received is zero
@@ -106,8 +106,16 @@ function WebRTCConn(options) {
106
106
  callAPI('get', url, undefined, function (body, code) {
107
107
  if (code == 200) {
108
108
  body = parseJSON(body) || []
109
- activeCalls = {}
110
- body.map && body.map((call) => (activeCalls[call.call_id] = call))
109
+ let serverCall = {}
110
+ body.map && body.map((call) => (serverCall[call.call_id] = call))
111
+
112
+ // not found
113
+ Object.keys(activeCalls).map((callid) => {
114
+ let call = activeCalls[callid]
115
+ if (!serverCall[callid] && call && call.status != 'ended')
116
+ publish({type: 'call_ended', data: {call_info: {hanup_code: 'outdated', call_id: callid}}})
117
+ })
118
+ activeCalls = serverCall
111
119
  }
112
120
 
113
121
  if (!peer) return
@@ -126,7 +134,6 @@ function WebRTCConn(options) {
126
134
  while (receivedBytes.length > 10) receivedBytes.shift()
127
135
  let diff = receivedBytes.filter((n) => n != receivedByte)
128
136
  if (diff.length == 0 && receivedBytes.length >= 10 && Object.keys(activeCalls).length == 0) {
129
- if (Object.keys(activeCalls).length == 0) stopMic()
130
137
  // could tear down
131
138
  peer.close()
132
139
  peer = undefined
@@ -137,7 +144,14 @@ function WebRTCConn(options) {
137
144
  check()
138
145
  env.setInterval(check, 30000)
139
146
 
140
- let publish = (ev) => onEvent && onEvent(ev)
147
+ let publish = (ev) => {
148
+ if (ev.type == 'call_ended') {
149
+ let callid = lo.get(ev, 'data.call_info.call_id')
150
+ stopStream(callStream[callid])
151
+ delete callStream[callid]
152
+ }
153
+ onEvent && onEvent(ev)
154
+ }
141
155
 
142
156
  this.matchCall = (callid) => {
143
157
  if (!callid || callid == '*' || callid == '-') {
@@ -243,8 +257,8 @@ function WebRTCConn(options) {
243
257
  }
244
258
  })
245
259
 
246
- this.makeCall = (number, fromnumber, stream) => {
247
- let callid = 'webcall-' + randomString(30)
260
+ this.makeCall = (number, fromnumber, stream, callid) => {
261
+ if (!callid) callid = 'webcall-' + randomString(30)
248
262
  let now = env.Date.now()
249
263
  let call = {
250
264
  account_id: accid,
@@ -294,8 +308,9 @@ function WebRTCConn(options) {
294
308
  }
295
309
  call.status = 'ended'
296
310
  call.ended = env.Date.now()
297
- callAPI('post', `${apiUrl}hangup?x-access-token=${access_token}&connection_id=${connId}&call_id=${callid}`)
298
311
  publish({type: 'call_ended', data: {call_info: {call_id: callid}}})
312
+
313
+ callAPI('post', `${apiUrl}hangup?x-access-token=${access_token}&connection_id=${connId}&call_id=${callid}`)
299
314
  }
300
315
 
301
316
  this.listenCall = (callid, cb) => {
@@ -304,6 +319,7 @@ function WebRTCConn(options) {
304
319
  }
305
320
 
306
321
  this.joinCall = (callid, stream, cb) => {
322
+ callStream[callid] = stream
307
323
  console.time('makecall' + callid)
308
324
  makeSure(() => {
309
325
  pendingStream = stream
@@ -318,13 +334,10 @@ function WebRTCConn(options) {
318
334
  }
319
335
 
320
336
  this.answerCall = (callid, stream) => {
321
- console.log('1')
322
337
  let call = activeCalls[callid]
323
338
  if (call && call.status == 'ended') return Promise.resolve({error: 'call_ended'})
324
339
  let now = env.Date.now()
325
- console.log('2', answerRequest[callid])
326
340
  if (answerRequest[callid]) return Promise.resolve({body: call})
327
- console.log('3')
328
341
  answerRequest[callid] = now
329
342
  publish({type: 'call_ringing', data: {call_info: {call_id: callid}}}) // fake dialing
330
343
  return new env.Promise((rs) => {