@subiz/wsclient 4.0.7 → 4.0.8

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 +26 -11
package/package.json CHANGED
@@ -1,6 +1,6 @@
1
1
  {
2
2
  "name": "@subiz/wsclient",
3
- "version": "4.0.7",
3
+ "version": "4.0.8",
4
4
  "main": "realtime.js",
5
5
  "scripts": {
6
6
  "test": "node realtime.test.js"
package/webrtc.js CHANGED
@@ -15,6 +15,7 @@ function WebRTCConn(options) {
15
15
  }
16
16
 
17
17
  callAPI = callAPI || xhrsend // allow hook
18
+ let callStream = {} // callid => stream
18
19
 
19
20
  let parseJSON = (str) => {
20
21
  try {
@@ -89,13 +90,12 @@ function WebRTCConn(options) {
89
90
  console.log('NEW PERR')
90
91
  }
91
92
 
92
- let stopMic = () => {
93
- console.log('00000000STOP TRACK', currentStream)
94
- return
95
- currentStream &&
96
- currentStream.getTracks().forEach((track) => {
97
- track.enabled = false
98
- })
93
+ let stopStream = (stream) => {
94
+ if (!stream) return
95
+ stream.getTracks().forEach((track) => {
96
+ track.stop()
97
+ track.enabled = false
98
+ })
99
99
  }
100
100
 
101
101
  // auto tear down when byte received is zero
@@ -106,8 +106,16 @@ function WebRTCConn(options) {
106
106
  callAPI('get', url, undefined, function (body, code) {
107
107
  if (code == 200) {
108
108
  body = parseJSON(body) || []
109
- activeCalls = {}
110
- body.map && body.map((call) => (activeCalls[call.call_id] = call))
109
+ let serverCall = {}
110
+ body.map && body.map((call) => (serverCall[call.call_id] = call))
111
+
112
+ // not found
113
+ Object.keys(activeCalls).map((callid) => {
114
+ let call = activeCalls[callid]
115
+ if (!serverCall[callid] && call && call.status != 'ended')
116
+ publish({type: 'call_ended', data: {call_info: {hanup_code: 'outdated', call_id: callid}}})
117
+ })
118
+ activeCalls = serverCall
111
119
  }
112
120
 
113
121
  if (!peer) return
@@ -126,7 +134,6 @@ function WebRTCConn(options) {
126
134
  while (receivedBytes.length > 10) receivedBytes.shift()
127
135
  let diff = receivedBytes.filter((n) => n != receivedByte)
128
136
  if (diff.length == 0 && receivedBytes.length >= 10 && Object.keys(activeCalls).length == 0) {
129
- if (Object.keys(activeCalls).length == 0) stopMic()
130
137
  // could tear down
131
138
  peer.close()
132
139
  peer = undefined
@@ -137,7 +144,14 @@ function WebRTCConn(options) {
137
144
  check()
138
145
  env.setInterval(check, 30000)
139
146
 
140
- let publish = (ev) => onEvent && onEvent(ev)
147
+ let publish = (ev) => {
148
+ if (ev.type == 'call_ended') {
149
+ let callid = lo.get(ev, 'data.call_info.call_id')
150
+ stopStream(callStream[callid])
151
+ delete callStream[callid]
152
+ }
153
+ onEvent && onEvent(ev)
154
+ }
141
155
 
142
156
  this.matchCall = (callid) => {
143
157
  if (!callid || callid == '*' || callid == '-') {
@@ -305,6 +319,7 @@ function WebRTCConn(options) {
305
319
  }
306
320
 
307
321
  this.joinCall = (callid, stream, cb) => {
322
+ callStream[callid] = stream
308
323
  console.time('makecall' + callid)
309
324
  makeSure(() => {
310
325
  pendingStream = stream