@subiz/wsclient 4.0.0 → 4.0.1

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 +66 -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.1",
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,30 @@ 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 = activeCalls[callid]
148
+ if (!call && dialingRequest[callid]) {
149
+ calls[callid] = dialingRequest[callid]
150
+ }
151
+ })
152
+ return calls
153
+ }
154
+
155
+ let call = activeCalls[callid]
156
+ if (endRequest[callid] && call && call.status != 'ended') {
157
+ return Object.assign({}, call, {
158
+ call_id: callid,
159
+ ended: endRequest[callid],
160
+ status: 'ended',
161
+ hangup_code: 'cancel',
162
+ })
163
+ }
164
+ if (!call && dialingRequest[callid]) return dialingRequest[callid]
165
+ return call
166
+ }
142
167
 
143
168
  realtime.onEvent((ev) => {
144
169
  if (!ev || !ev.type || !ev.data) return
@@ -186,17 +211,16 @@ function WebRTCConn(options) {
186
211
  if (!callid) return
187
212
 
188
213
  if (ev.type === 'call_invite_expired') {
189
- activeCalls[callid] = call_info
190
214
  let device_id = call_info.device_id
191
215
  if (device_id == connId) return // ignore since we have accepted the call
192
-
193
216
  publish(Object.assign({}, ev, {type: 'call_ended'})) // fake ended call
194
217
  delete activeCalls[callid]
195
218
  }
219
+
196
220
  let type = ev.type
197
221
  if (type == 'call_ended' || type == 'call_ringing' || type == 'call_joined' || type === 'call_invited') {
198
222
  if (ev.type === 'call_invited' && agid == call_info.from_number) return // ignore our own invite
199
- activeCalls[callid] = ev.data.call_info
223
+ activeCalls[callid] = call_info
200
224
  return publish(ev)
201
225
  }
202
226
  })
@@ -204,11 +228,39 @@ function WebRTCConn(options) {
204
228
  this.makeCall = (number, fromnumber, stream) =>
205
229
  new env.Promise((rs) => {
206
230
  let callid = 'webcall-' + randomString(20)
231
+ let call = {
232
+ account_id: accid,
233
+ call_id: callid,
234
+ device_id: connId,
235
+ direction: 'outbound',
236
+ member_id: agid,
237
+ started: env.Date.now(),
238
+ status: 'dialing',
239
+ to_number: number,
240
+ }
241
+ dialingRequest[callid] = call
242
+ publish({created: env.Date.now(), type: 'call_ringing', data: {call_info: call}}) // fake dialing
207
243
  this.joinCall(callid, stream, (err) => {
208
- if (err) return rs({error: err})
244
+ if (err) {
245
+ delete dialingRequest[callid]
246
+ endRequest[callid] = env.Date.now()
247
+ publish({
248
+ type: 'call_ended',
249
+ data: {call_info: {call_id: callid, ended: endRequest[callid], status: 'ended', hangup_code: 'error'}},
250
+ })
251
+ return rs({error: err})
252
+ }
209
253
  let url = `${apiUrl}call?x-access-token=${access_token}&connection_id=${connId}&call_id=${callid}&number=${number}&from_number=${fromnumber}`
210
254
  callAPI('post', url, undefined, (body, code) => {
211
- if (code != 200) return rs({error: body})
255
+ if (code != 200) {
256
+ delete dialingRequest[callid]
257
+ endRequest[callid] = env.Date.now()
258
+ publish({
259
+ type: 'call_ended',
260
+ data: {call_info: {call_id: callid, ended: endRequest[callid], status: 'ended', hangup_code: 'error'}},
261
+ })
262
+ return rs({error: err})
263
+ }
212
264
  body = parseJSON(body)
213
265
  if (body) activeCalls[callid] = body
214
266
  rs({body})
@@ -218,6 +270,8 @@ function WebRTCConn(options) {
218
270
 
219
271
  this.hangupCall = (callid) => {
220
272
  if (!callid) return
273
+ endRequest[callid] = env.Date.now()
274
+
221
275
  let call = activeCalls[callid]
222
276
  if (!call) {
223
277
  call = {call_id: callid}
@@ -238,15 +292,12 @@ function WebRTCConn(options) {
238
292
  console.time('makecall' + callid)
239
293
  makeSure(() => {
240
294
  pendingStream = stream
241
- let url = `${apiUrl}listen?x-access-token=${access_token}&connection_id=${connId}&call_id=${callid}`
295
+ let url = `${apiUrl}listen?x-access-token=${access_token}&connection_id=${connId}&call_id=${callid}&talk=true`
242
296
  callAPI('post', url, undefined, (body, code) => {
243
297
  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
- })
298
+ console.timeEnd('makecall' + callid)
299
+ if (code != 200) return cb(body)
300
+ checkIceConnected(() => cb())
250
301
  })
251
302
  })
252
303
  }