@subiz/wsclient 4.0.0 → 4.0.2

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 +78 -15
package/package.json CHANGED
@@ -1,6 +1,6 @@
1
1
  {
2
2
  "name": "@subiz/wsclient",
3
- "version": "4.0.0",
3
+ "version": "4.0.2",
4
4
  "main": "realtime.js",
5
5
  "scripts": {
6
6
  "test": "node realtime.test.js"
package/webrtc.js CHANGED
@@ -4,7 +4,6 @@ const servers = {
4
4
  iceServers: [{urls: ['stun:stun.l.google.com:19302?transport=tcp']}],
5
5
  }
6
6
 
7
- // import {RTCPeerConnection, RTCIceCandidate, RTCSessionDescription, mediaDevices} from 'react-native-webrtc'
8
7
  // let connId = 'webrtc' + sb.randomString(20)
9
8
  // env = {RTCPeerConnection, RTCIceCandidate, RTCSessionDescription, setTimeout, setInterval, jsonify, parseJSON}
10
9
  // setting env._is_webrtc_local to true for local testing
@@ -31,6 +30,9 @@ function WebRTCConn(options) {
31
30
  let pendingStream
32
31
  let activeCalls = {}
33
32
 
33
+ let dialingRequest = {}
34
+ let endRequest = {}
35
+
34
36
  let subscribe = realtime.subscribe([
35
37
  `webrtc.account.${accid}.agent.${agid}`,
36
38
  `webrtc.account.${accid}.agent.${agid}.webrtc_connection.${connId}`,
@@ -49,7 +51,7 @@ function WebRTCConn(options) {
49
51
  () =>
50
52
  new env.Promise((rs) => {
51
53
  let pass = peer && (peer.iceConnectionState == 'connected' || peer.iceConnectionState == 'completed')
52
- if (!pass) return env.setTimeout((_) => rs(true), 100) // keep looping ultil connected
54
+ if (!pass) return env.setTimeout((_) => rs(true), 100) // keep looping util connected
53
55
  cb && cb()
54
56
  rs(false)
55
57
  }),
@@ -138,7 +140,42 @@ function WebRTCConn(options) {
138
140
 
139
141
  let publish = (ev) => onEvent && onEvent(ev)
140
142
 
141
- this.matchCall = (callid) => (!callid ? activeCalls : activeCalls[callid])
143
+ this.matchCall = (callid) => {
144
+ if (!callid || callid == '*' || callid == '-') {
145
+ let calls = Object.assign({}, activeCalls)
146
+ Object.keys(dialingRequest).map((callid) => {
147
+ let call = calls[callid]
148
+ if (!call && dialingRequest[callid]) {
149
+ calls[callid] = dialingRequest[callid]
150
+ }
151
+ })
152
+
153
+ Object.keys(calls, (callid) => {
154
+ let call = calls[callid]
155
+ if (endRequest[callid] && call && call.status != 'ended') {
156
+ calls[callid] = Object.assign({}, call, {
157
+ call_id: callid,
158
+ ended: endRequest[callid],
159
+ status: 'ended',
160
+ hangup_code: 'cancel',
161
+ })
162
+ }
163
+ })
164
+ return calls
165
+ }
166
+
167
+ let call = activeCalls[callid]
168
+ if (endRequest[callid] && call && call.status != 'ended') {
169
+ return Object.assign({}, call, {
170
+ call_id: callid,
171
+ ended: endRequest[callid],
172
+ status: 'ended',
173
+ hangup_code: 'cancel',
174
+ })
175
+ }
176
+ if (!call && dialingRequest[callid]) return dialingRequest[callid]
177
+ return call
178
+ }
142
179
 
143
180
  realtime.onEvent((ev) => {
144
181
  if (!ev || !ev.type || !ev.data) return
@@ -186,17 +223,16 @@ function WebRTCConn(options) {
186
223
  if (!callid) return
187
224
 
188
225
  if (ev.type === 'call_invite_expired') {
189
- activeCalls[callid] = call_info
190
226
  let device_id = call_info.device_id
191
227
  if (device_id == connId) return // ignore since we have accepted the call
192
-
193
228
  publish(Object.assign({}, ev, {type: 'call_ended'})) // fake ended call
194
229
  delete activeCalls[callid]
195
230
  }
231
+
196
232
  let type = ev.type
197
233
  if (type == 'call_ended' || type == 'call_ringing' || type == 'call_joined' || type === 'call_invited') {
198
234
  if (ev.type === 'call_invited' && agid == call_info.from_number) return // ignore our own invite
199
- activeCalls[callid] = ev.data.call_info
235
+ activeCalls[callid] = call_info
200
236
  return publish(ev)
201
237
  }
202
238
  })
@@ -204,11 +240,39 @@ function WebRTCConn(options) {
204
240
  this.makeCall = (number, fromnumber, stream) =>
205
241
  new env.Promise((rs) => {
206
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
207
255
  this.joinCall(callid, stream, (err) => {
208
- if (err) return rs({error: err})
256
+ if (err) {
257
+ delete dialingRequest[callid]
258
+ 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
+ })
263
+ return rs({error: err})
264
+ }
209
265
  let url = `${apiUrl}call?x-access-token=${access_token}&connection_id=${connId}&call_id=${callid}&number=${number}&from_number=${fromnumber}`
210
266
  callAPI('post', url, undefined, (body, code) => {
211
- if (code != 200) return rs({error: body})
267
+ if (code != 200) {
268
+ delete dialingRequest[callid]
269
+ 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
+ })
274
+ return rs({error: err})
275
+ }
212
276
  body = parseJSON(body)
213
277
  if (body) activeCalls[callid] = body
214
278
  rs({body})
@@ -218,6 +282,8 @@ function WebRTCConn(options) {
218
282
 
219
283
  this.hangupCall = (callid) => {
220
284
  if (!callid) return
285
+ endRequest[callid] = env.Date.now()
286
+
221
287
  let call = activeCalls[callid]
222
288
  if (!call) {
223
289
  call = {call_id: callid}
@@ -238,15 +304,12 @@ function WebRTCConn(options) {
238
304
  console.time('makecall' + callid)
239
305
  makeSure(() => {
240
306
  pendingStream = stream
241
- let url = `${apiUrl}listen?x-access-token=${access_token}&connection_id=${connId}&call_id=${callid}`
307
+ let url = `${apiUrl}listen?x-access-token=${access_token}&connection_id=${connId}&call_id=${callid}&talk=true`
242
308
  callAPI('post', url, undefined, (body, code) => {
243
309
  if (code != 200) return cb(body)
244
- let url = `${apiUrl}join?x-access-token=${access_token}&connection_id=${connId}&call_id=${callid}`
245
- callAPI('post', url, undefined, (body, code) => {
246
- console.timeEnd('makecall' + callid)
247
- if (code != 200) return cb(body)
248
- checkIceConnected(() => cb())
249
- })
310
+ console.timeEnd('makecall' + callid)
311
+ if (code != 200) return cb(body)
312
+ checkIceConnected(() => cb())
250
313
  })
251
314
  })
252
315
  }