@subiz/wsclient 4.0.3 → 4.0.5

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 +52 -31
package/package.json CHANGED
@@ -1,6 +1,6 @@
1
1
  {
2
2
  "name": "@subiz/wsclient",
3
- "version": "4.0.3",
3
+ "version": "4.0.5",
4
4
  "main": "realtime.js",
5
5
  "scripts": {
6
6
  "test": "node realtime.test.js"
package/webrtc.js CHANGED
@@ -31,6 +31,7 @@ function WebRTCConn(options) {
31
31
  let activeCalls = {}
32
32
 
33
33
  let dialingRequest = {}
34
+ let answerRequest = {}
34
35
  let endRequest = {}
35
36
 
36
37
  let subscribe = realtime.subscribe([
@@ -70,10 +71,8 @@ function WebRTCConn(options) {
70
71
  if (!needConnect) return
71
72
  peer = new env.RTCPeerConnection(servers)
72
73
  let mypeer = peer
73
- window.peer = peer
74
74
 
75
75
  readyToSendCandidate = new env.Promise((rs) => (readyToSendCandidateResolve = rs))
76
-
77
76
  peer.ontrack = (event) => {
78
77
  if (peer != mypeer) return // outdated
79
78
  onTrack && onTrack(event)
@@ -161,19 +160,26 @@ function WebRTCConn(options) {
161
160
  hangup_code: 'cancel',
162
161
  })
163
162
  })
163
+
164
+ Object.keys(answerRequest).map((callid) => {
165
+ let call = calls[callid]
166
+ if (!call) return
167
+ call.answer_requested = answerRequest[callid]
168
+ })
164
169
  return calls
165
170
  }
166
171
 
167
172
  let call = activeCalls[callid]
168
173
  if (!call && dialingRequest[callid]) call = dialingRequest[callid]
169
174
  if (endRequest[callid] && call && call.status != 'ended') {
170
- return Object.assign({}, call, {
175
+ call = Object.assign({}, call, {
171
176
  call_id: callid,
172
177
  ended: endRequest[callid],
173
178
  status: 'ended',
174
179
  hangup_code: 'cancel',
175
180
  })
176
181
  }
182
+ if (answerRequest[callid]) call = Object.assign({}, call, {answer_requested: answerRequest[callid]})
177
183
  return call
178
184
  }
179
185
 
@@ -237,40 +243,36 @@ function WebRTCConn(options) {
237
243
  }
238
244
  })
239
245
 
240
- this.makeCall = (number, fromnumber, stream) =>
241
- new env.Promise((rs) => {
242
- let callid = 'webcall-' + randomString(20)
243
- let call = {
244
- account_id: accid,
245
- call_id: callid,
246
- device_id: connId,
247
- direction: 'outbound',
248
- member_id: agid,
249
- started: env.Date.now(),
250
- status: 'dialing',
251
- to_number: number,
252
- }
253
- dialingRequest[callid] = call
254
- publish({created: env.Date.now(), type: 'call_ringing', data: {call_info: call}}) // fake dialing
246
+ this.makeCall = (number, fromnumber, stream) => {
247
+ let callid = 'webcall-' + randomString(30)
248
+ let now = env.Date.now()
249
+ let call = {
250
+ account_id: accid,
251
+ call_id: callid,
252
+ device_id: connId,
253
+ direction: 'outbound',
254
+ member_id: agid,
255
+ started: now,
256
+ status: 'dialing',
257
+ to_number: number,
258
+ }
259
+ dialingRequest[callid] = call
260
+ publish({type: 'call_ringing', data: {call_info: {call_id: callid}}}) // fake dialing
261
+ return new env.Promise((rs) => {
255
262
  this.joinCall(callid, stream, (err) => {
256
263
  if (err) {
257
264
  delete dialingRequest[callid]
258
265
  endRequest[callid] = env.Date.now()
259
- publish({
260
- type: 'call_ended',
261
- data: {call_info: {call_id: callid, ended: endRequest[callid], status: 'ended', hangup_code: 'error'}},
262
- })
266
+ publish({type: 'call_ended', data: {call_info: {call_id: callid}}})
263
267
  return rs({error: err})
264
268
  }
269
+
265
270
  let url = `${apiUrl}call?x-access-token=${access_token}&connection_id=${connId}&call_id=${callid}&number=${number}&from_number=${fromnumber}`
266
271
  callAPI('post', url, undefined, (body, code) => {
267
272
  if (code != 200) {
268
273
  delete dialingRequest[callid]
269
274
  endRequest[callid] = env.Date.now()
270
- publish({
271
- type: 'call_ended',
272
- data: {call_info: {call_id: callid, ended: endRequest[callid], status: 'ended', hangup_code: 'error'}},
273
- })
275
+ publish({type: 'call_ended', data: {call_info: {call_id: callid}}})
274
276
  return rs({error: err})
275
277
  }
276
278
  body = parseJSON(body)
@@ -279,6 +281,7 @@ function WebRTCConn(options) {
279
281
  })
280
282
  })
281
283
  })
284
+ }
282
285
 
283
286
  this.hangupCall = (callid) => {
284
287
  if (!callid) return
@@ -292,7 +295,7 @@ function WebRTCConn(options) {
292
295
  call.status = 'ended'
293
296
  call.ended = env.Date.now()
294
297
  callAPI('post', `${apiUrl}hangup?x-access-token=${access_token}&connection_id=${connId}&call_id=${callid}`)
295
- publish({type: 'call_ended', data: {call_info: call}})
298
+ publish({type: 'call_ended', data: {call_info: {call_id: callid}}})
296
299
  }
297
300
 
298
301
  this.listenCall = (callid, cb) => {
@@ -314,17 +317,35 @@ function WebRTCConn(options) {
314
317
  })
315
318
  }
316
319
 
317
- this.answerCall = (callid, stream) =>
318
- new env.Promise((rs) => {
320
+ this.answerCall = (callid, stream) => {
321
+ let call = activeCalls[callid]
322
+ if (!call || call.status == 'ended') return Promise.resolve({error: 'call_ended'})
323
+ let now = env.Date.now()
324
+ if (answerRequest[callid]) return Promise.resolve({body: call})
325
+ answerRequest[callid] = now
326
+ publish({type: 'call_ringing', data: {call_info: {call_id: callid}}}) // fake dialing
327
+ return new env.Promise((rs) => {
319
328
  this.joinCall(callid, stream, (err) => {
320
- if (err) return rs({error: err})
329
+ if (err) {
330
+ delete answerRequest[callid]
331
+ endRequest[callid] = now
332
+ publish({type: 'call_ended', data: {call_info: {call_id: callid}}})
333
+ return rs({error: err})
334
+ }
321
335
  let url = `${apiUrl}answer?x-access-token=${access_token}&connection_id=${connId}&call_id=${callid}`
322
336
  callAPI('post', url, undefined, (body, code) => {
323
337
  body = parseJSON(body)
324
- return code != 200 ? rs({error: body}) : rs({body})
338
+ if (code != 200) {
339
+ delete answerRequest[callid]
340
+ endRequest[callid] = now
341
+ publish({type: 'call_ended', data: {call_info: {call_id: callid}}})
342
+ return rs({error: body})
343
+ }
344
+ return rs({body})
325
345
  })
326
346
  })
327
347
  })
348
+ }
328
349
 
329
350
  this.sendDtmf = (key, callid) =>
330
351
  new env.Promise((rs) => {