@subiz/wsclient 4.0.4 → 4.0.6

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 +48 -30
package/package.json CHANGED
@@ -1,6 +1,6 @@
1
1
  {
2
2
  "name": "@subiz/wsclient",
3
- "version": "4.0.4",
3
+ "version": "4.0.6",
4
4
  "main": "realtime.js",
5
5
  "scripts": {
6
6
  "test": "node realtime.test.js"
package/webrtc.js CHANGED
@@ -4,9 +4,9 @@ const servers = {
4
4
  iceServers: [{urls: ['stun:stun.l.google.com:19302?transport=tcp']}],
5
5
  }
6
6
 
7
- // let connId = 'webrtc' + sb.randomString(20)
7
+ // let connId = 'webrtc' + randomString(20)
8
8
  // env = {RTCPeerConnection, RTCIceCandidate, RTCSessionDescription, setTimeout, setInterval, jsonify, parseJSON}
9
- // setting env._is_webrtc_local to true for local testing
9
+ // set env._is_webrtc_local to true for local testing
10
10
  function WebRTCConn(options) {
11
11
  let {apiUrl, access_token, accid, agid, connId, realtime, callAPI, env, onTrack, onEvent} = options
12
12
  if (!apiUrl) {
@@ -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,38 @@ 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
+ console.log('1')
322
+ let call = activeCalls[callid]
323
+ if (call && call.status == 'ended') return Promise.resolve({error: 'call_ended'})
324
+ let now = env.Date.now()
325
+ console.log('2', answerRequest[callid])
326
+ if (answerRequest[callid]) return Promise.resolve({body: call})
327
+ console.log('3')
328
+ answerRequest[callid] = now
329
+ publish({type: 'call_ringing', data: {call_info: {call_id: callid}}}) // fake dialing
330
+ return new env.Promise((rs) => {
325
331
  this.joinCall(callid, stream, (err) => {
326
- if (err) return rs({error: err})
332
+ if (err) {
333
+ delete answerRequest[callid]
334
+ endRequest[callid] = now
335
+ publish({type: 'call_ended', data: {call_info: {call_id: callid}}})
336
+ return rs({error: err})
337
+ }
327
338
  let url = `${apiUrl}answer?x-access-token=${access_token}&connection_id=${connId}&call_id=${callid}`
328
339
  callAPI('post', url, undefined, (body, code) => {
329
340
  body = parseJSON(body)
330
- return code != 200 ? rs({error: body}) : rs({body})
341
+ if (code != 200) {
342
+ delete answerRequest[callid]
343
+ endRequest[callid] = now
344
+ publish({type: 'call_ended', data: {call_info: {call_id: callid}}})
345
+ return rs({error: body})
346
+ }
347
+ return rs({body})
331
348
  })
332
349
  })
333
350
  })
351
+ }
334
352
 
335
353
  this.sendDtmf = (key, callid) =>
336
354
  new env.Promise((rs) => {