@subiz/wsclient 4.0.19 → 4.0.21

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 (3) hide show
  1. package/common.js +39 -1
  2. package/package.json +1 -1
  3. package/webrtc.js +23 -10
package/common.js CHANGED
@@ -1,3 +1,41 @@
1
+ function logError(accid, userid, message, line) {
2
+ // 24-01-11 04:07:05 app ERROR api-8948967b6-qtpfx subiz /app/vendor/github.com/subiz/log/error.go:271 {"id":141534190473829746,"class":500,"code":"locked_account,internal","number":"8f2b97e9","fields":{"ip":"\"171.224.84.93\"","no_report":"true","user_agent":"\"Mozilla/5.0 (Linux; Android 10; K) AppleWebKit/537.36 (KHTML, like Gecko) Chrome/120.0.0.0 Mobile Safari/537.36\""}}
3
+ let ts = displayUTCLogTime(new Date())
4
+ let msg = JSON.stringify({
5
+ id: Date.now(),
6
+ class: 400,
7
+ code: 'frontend_error',
8
+ number: line
9
+ .split('')
10
+ .map((c) => c.charCodeAt(0).toString(16).padStart(2, '0'))
11
+ .join(''),
12
+ fields: {
13
+ message: JSON.stringify(message),
14
+ user_id: userid,
15
+ url: JSON.stringify(location.href),
16
+ },
17
+ })
18
+
19
+ xhrsend('POST', 'https://track.subiz.net/collect', `${ts} app ERROR dashboard ${accid} ${line} ${msg}`)
20
+ }
21
+
22
+ function displayUTCLogTime(date) {
23
+ let yyyy = date.getUTCFullYear().toString()
24
+ let yy = yyyy.substr(2, 2)
25
+ let mm = date.getUTCMonth() + 1
26
+ let dd = date.getUTCDate()
27
+ let hh = date.getUTCHours()
28
+ let MM = date.getUTCMinutes()
29
+ let ss = date.getUTCSeconds()
30
+
31
+ return `${yy}-${pad(mm)}-${pad(dd)} ${pad(hh)}:${pad(MM)}:${pad(ss)}`
32
+ }
33
+
34
+ function pad(v) {
35
+ if (v < 10) return `0${v}`
36
+ return `${v}`
37
+ }
38
+
1
39
  function dofetch(method, url, body, cb) {
2
40
  let headers = {}
3
41
  if (body) headers['content-type'] = 'text/plain'
@@ -78,4 +116,4 @@ function randomString(len, base) {
78
116
  return str
79
117
  }
80
118
 
81
- module.exports = {xhrsend, parseJSON, Pubsub, randomString}
119
+ module.exports = {xhrsend, parseJSON, Pubsub, randomString, logError}
package/package.json CHANGED
@@ -1,6 +1,6 @@
1
1
  {
2
2
  "name": "@subiz/wsclient",
3
- "version": "4.0.19",
3
+ "version": "4.0.21",
4
4
  "main": "realtime.js",
5
5
  "scripts": {
6
6
  "test": "node realtime.test.js"
package/webrtc.js CHANGED
@@ -1,5 +1,5 @@
1
1
  var flow = require('@subiz/flow')
2
- var {xhrsend, randomString} = require('./common.js')
2
+ var {logError, xhrsend, randomString} = require('./common.js')
3
3
  const servers = {
4
4
  iceServers: [{urls: ['stun:stun.l.google.com:19302']}],
5
5
  }
@@ -8,7 +8,8 @@ const servers = {
8
8
  // env = {RTCPeerConnection, RTCIceCandidate, RTCSessionDescription, setTimeout, setInterval, jsonify, parseJSON}
9
9
  // set env._is_webrtc_local to true for local testing
10
10
  function WebRTCConn(options) {
11
- let {apiUrl, access_token, accid, agid, realtime, callAPI, env, onTrack, onEvent} = options
11
+ let {apiUrl, access_token, accid, agid, realtime, callAPI, env, onTrack, onEvent, collect} = options
12
+ if (!collect) collect = function () {}
12
13
  if (!apiUrl) {
13
14
  apiUrl = 'https://callcenter.subiz.com.vn/wc/'
14
15
  if (env._is_webrtc_local) apiUrl = 'http://localhost:8008/'
@@ -290,7 +291,10 @@ function WebRTCConn(options) {
290
291
  peer.setLocalDescription(answer).then(() => {
291
292
  let url = `${apiUrl}finish?x-access-token=${access_token}&connection_id=${connId}&version=2&negotiation_id=${negid}`
292
293
  callAPI('post', url, jsonify(answer), function (body, code) {
293
- if (code !== 200) return console.error('CANNOT FINISH', body)
294
+ if (code !== 200) {
295
+ logError(accid, agid, body, 'finish:295')
296
+ return console.error('CANNOT FINISH', body)
297
+ }
294
298
  return
295
299
  })
296
300
  }),
@@ -302,7 +306,7 @@ function WebRTCConn(options) {
302
306
  if (!callid) return
303
307
  let type = ev.type
304
308
  if (type === 'call_invited') {
305
- if (ev.type === 'call_invited' && agid == call_info.from_number) return // ignore our own invite
309
+ if (agid == call_info.from_number) return // ignore our own invite
306
310
  serverCalls[callid] = call_info
307
311
  return publish(ev)
308
312
  }
@@ -358,6 +362,7 @@ function WebRTCConn(options) {
358
362
  if (campaignid) url += `&campaign_id=${campaignid}`
359
363
  callAPI('post', url, undefined, (body, code) => {
360
364
  if (code != 200) {
365
+ logError(accid, agid, body, 'makeCall:365')
361
366
  delete dialingRequest[callid]
362
367
  endRequest[callid] = env.Date.now()
363
368
  publish({created: env.Date.now(), type: 'call_ended', data: {call_info: {call_id: callid}}})
@@ -390,21 +395,26 @@ function WebRTCConn(options) {
390
395
  }
391
396
 
392
397
  this.listenCall = (callid, cb) => {
398
+ let start = env.Date.now()
393
399
  initSession((err, connId) => {
394
400
  let url = `${apiUrl}listen?x-access-token=${access_token}&connection_id=${connId}&call_id=${callid}`
395
- callAPI('post', url, undefined, (body, code) => (code != 200 ? cb(body) : cb()))
401
+ callAPI('post', url, undefined, (body, code) => {
402
+ if (code != 200) logError(accid, agid, body, 'listenCall:399')
403
+ collect('listen_call', callid, env.Date.now() - start)
404
+ return code != 200 ? cb(body) : cb()
405
+ })
396
406
  })
397
407
  }
398
408
 
399
409
  this.joinCall = (callid, stream, cb) => {
400
- console.time('makecall' + callid)
410
+ let start = env.Date.now()
401
411
  initSession((err, connId) => {
402
412
  call2Connection[callid] = connId
403
413
  streams[connId] = stream
404
414
  let url = `${apiUrl}listen?x-access-token=${access_token}&connection_id=${connId}&call_id=${callid}&talk=true`
405
415
  callAPI('post', url, undefined, (body, code) => {
406
- if (code != 200) return cb(body, connId)
407
- console.timeEnd('makecall' + callid)
416
+ if (code != 200) logError(accid, agid, body, 'joinCall:413')
417
+ collect('join_call', callid, env.Date.now() - start)
408
418
  if (code != 200) return cb(body, connId)
409
419
  checkIceConnected(peers[connId], () => cb(undefined, connId))
410
420
  })
@@ -430,6 +440,7 @@ function WebRTCConn(options) {
430
440
  callAPI('post', url, undefined, (body, code) => {
431
441
  body = parseJSON(body)
432
442
  if (code != 200) {
443
+ logError(accid, agid, body, 'answerCall:442')
433
444
  delete answerRequest[callid]
434
445
  endRequest[callid] = now
435
446
  publish({created: env.Date.now(), type: 'call_ended', data: {call_info: {call_id: callid}}})
@@ -446,10 +457,10 @@ function WebRTCConn(options) {
446
457
  if (!callid) return rs({body: {}})
447
458
  let connId = call2Connection[callid]
448
459
  if (!connId) return rs({body: {}})
449
-
450
460
  let url = `${apiUrl}dtml?x-access-token=${access_token}&connection_id=${connId}&call_id=${callid}&key={key}`
451
461
  initSession(() =>
452
- callAPI('post', url, undefined, (body, bode) => {
462
+ callAPI('post', url, undefined, (body, code) => {
463
+ if (code != 200) logError(accid, agid, body, 'sendDtmf:463')
453
464
  body = parseJSON(body)
454
465
  return code != 200 ? rs({error: body}) : rs({body})
455
466
  }),
@@ -464,6 +475,8 @@ function WebRTCConn(options) {
464
475
  let url = `${apiUrl}refer?x-access-token=${access_token}&connection_id=${connId}&call_id=${callid}&number={tonumber}`
465
476
  initSession(() =>
466
477
  callAPI('post', url, undefined, (body, code) => {
478
+ if (code != 200) logError(accid, agid, body, 'transferCall:478')
479
+ collect('transfer_call', callid, env.Date.now() - start)
467
480
  body = parseJSON(body)
468
481
  return code != 200 ? rs({error: body}) : rs({body})
469
482
  }),