@subiz/wsclient 4.0.4 → 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 +43 -28
package/package.json CHANGED
@@ -1,6 +1,6 @@
1
1
  {
2
2
  "name": "@subiz/wsclient",
3
- "version": "4.0.4",
3
+ "version": "4.0.5",
4
4
  "main": "realtime.js",
5
5
  "scripts": {
6
6
  "test": "node realtime.test.js"
package/webrtc.js CHANGED
@@ -243,40 +243,36 @@ function WebRTCConn(options) {
243
243
  }
244
244
  })
245
245
 
246
- this.makeCall = (number, fromnumber, stream) =>
247
- new env.Promise((rs) => {
248
- let callid = 'webcall-' + randomString(20)
249
- let call = {
250
- account_id: accid,
251
- call_id: callid,
252
- device_id: connId,
253
- direction: 'outbound',
254
- member_id: agid,
255
- started: env.Date.now(),
256
- status: 'dialing',
257
- to_number: number,
258
- }
259
- dialingRequest[callid] = call
260
- 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) => {
261
262
  this.joinCall(callid, stream, (err) => {
262
263
  if (err) {
263
264
  delete dialingRequest[callid]
264
265
  endRequest[callid] = env.Date.now()
265
- publish({
266
- type: 'call_ended',
267
- data: {call_info: {call_id: callid, ended: endRequest[callid], status: 'ended', hangup_code: 'error'}},
268
- })
266
+ publish({type: 'call_ended', data: {call_info: {call_id: callid}}})
269
267
  return rs({error: err})
270
268
  }
269
+
271
270
  let url = `${apiUrl}call?x-access-token=${access_token}&connection_id=${connId}&call_id=${callid}&number=${number}&from_number=${fromnumber}`
272
271
  callAPI('post', url, undefined, (body, code) => {
273
272
  if (code != 200) {
274
273
  delete dialingRequest[callid]
275
274
  endRequest[callid] = env.Date.now()
276
- publish({
277
- type: 'call_ended',
278
- data: {call_info: {call_id: callid, ended: endRequest[callid], status: 'ended', hangup_code: 'error'}},
279
- })
275
+ publish({type: 'call_ended', data: {call_info: {call_id: callid}}})
280
276
  return rs({error: err})
281
277
  }
282
278
  body = parseJSON(body)
@@ -285,6 +281,7 @@ function WebRTCConn(options) {
285
281
  })
286
282
  })
287
283
  })
284
+ }
288
285
 
289
286
  this.hangupCall = (callid) => {
290
287
  if (!callid) return
@@ -298,7 +295,7 @@ function WebRTCConn(options) {
298
295
  call.status = 'ended'
299
296
  call.ended = env.Date.now()
300
297
  callAPI('post', `${apiUrl}hangup?x-access-token=${access_token}&connection_id=${connId}&call_id=${callid}`)
301
- publish({type: 'call_ended', data: {call_info: call}})
298
+ publish({type: 'call_ended', data: {call_info: {call_id: callid}}})
302
299
  }
303
300
 
304
301
  this.listenCall = (callid, cb) => {
@@ -320,17 +317,35 @@ function WebRTCConn(options) {
320
317
  })
321
318
  }
322
319
 
323
- this.answerCall = (callid, stream) =>
324
- 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) => {
325
328
  this.joinCall(callid, stream, (err) => {
326
- 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
+ }
327
335
  let url = `${apiUrl}answer?x-access-token=${access_token}&connection_id=${connId}&call_id=${callid}`
328
336
  callAPI('post', url, undefined, (body, code) => {
329
337
  body = parseJSON(body)
330
- 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})
331
345
  })
332
346
  })
333
347
  })
348
+ }
334
349
 
335
350
  this.sendDtmf = (key, callid) =>
336
351
  new env.Promise((rs) => {