webrtc-rails 0.1.4 → 0.1.5

Sign up to get free protection for your applications and to get access to all the features.
checksums.yaml CHANGED
@@ -1,7 +1,7 @@
1
1
  ---
2
2
  SHA1:
3
- metadata.gz: 994645e1d94a760fe2a08e28b93597efb8bab401
4
- data.tar.gz: 45a5bf72635604e39776493580bbf49ffdcbf385
3
+ metadata.gz: 643e6f36529bf140d6da5ebf72f924c7700b9bea
4
+ data.tar.gz: 498a7b8496a5c3831aa642cb3d42012d9c2904cc
5
5
  SHA512:
6
- metadata.gz: c4bf71305a37d52ae82ca4cd9a75a08872e0d5df4d1a22f61a887dbeaef03bd9c8086165d3ca9282fbca15eeeafb60c1d8389a3cc322fd2e0b60d692d33be7de
7
- data.tar.gz: bd8a114ddf75fc0efa79a5a1cc20d64b5d08432274c943985d2f22c3843da7f36b6ac80a58efc73e3b9fb93c4d479db8378e9d56bcd9a89ac55015d25876d5fc
6
+ metadata.gz: 6e02dc861006c7434f994335bfdeed68d1a22843995a7fad893006e15ab5350e25e75aceeb42633a4a0c9e02ab5f31b21c0f67722fe3d36947c29f4b2e3b70fe
7
+ data.tar.gz: b03c263bde72518cef778a4d2ea4905b50efed172d01ce3c36ead625546eb2db9bf90185a82e60bc12c0485695783cf08dfadb2d25dc06684c25472e0b798fc7
@@ -1,30 +1,49 @@
1
1
  class @WebRTC
2
+ @DISCONNECTED: 0
3
+ @TIMEOUT: 1
4
+ @CALLING: 2
5
+
2
6
  myUserID: null
3
7
 
4
8
  onWebSocketConnected: ->
5
9
  onWebSocketReconnectingStarted: ->
6
- onWebSocketReConnected: ->
10
+ onWebSocketReconnected: ->
7
11
  onWebRTCConnected: ->
8
12
  onWebRTCReconnectingStarted: ->
9
13
  onWebRTCReconnected: ->
10
14
  onWebRTCHangedUp: ->
15
+ onWebRTCConnectFailed: (reason) ->
11
16
 
12
- constructor: (url, userToken, @localOutput, @remoteOutput) ->
13
- @localOutput = @localOutput[0] || @localOutput
14
- @remoteOutput = @remoteOutput[0] || @remoteOutput
15
- @_startOutput(@localOutput.tagName.toUpperCase() == 'VIDEO')
17
+ constructor: (url, userToken, localOutput, remoteOutput) ->
18
+ @localOutput = if localOutput? then (localOutput[0] || localOutput) else null
19
+ @remoteOutput = remoteOutput[0] || remoteOutput
20
+ @_startOutput(@localOutput)
16
21
  @_webSocketInitialize(url, userToken)
17
22
  @_addNetworkEventListener()
18
23
 
19
24
  connect: (remoteUserID) ->
25
+ if @_webRTCReconnecting && @_hangedUp
26
+ @_webRTCReconnecting = false
27
+ return
28
+
29
+ @_isCaller = true
20
30
  @_remoteUserID = remoteUserID
21
31
  if !@_peerStarted && @_localStream
22
32
  @_sendMessage(
23
33
  type: 'call'
24
34
  remoteUserID: @myUserID
35
+ reconnect: @_webRTCReconnecting
36
+ )
37
+ @_callAnswerReceived = false
38
+ window.setTimeout(
39
+ =>
40
+ unless @_callAnswerReceived
41
+ if @_webRTCReconnecting
42
+ @connect(remoteUserID)
43
+ else
44
+ @onWebRTCConnectFailed(WebRTC.TIMEOUT)
45
+ 5000
25
46
  )
26
- @_sendOffer()
27
- @_peerStarted = true
28
47
  else
29
48
  alert 'Local stream not running yet - try again.'
30
49
 
@@ -39,8 +58,8 @@ class @WebRTC
39
58
  track.enabled = enabled
40
59
 
41
60
  hangUp: ->
61
+ @_hangUp()
42
62
  @_sendMessage(type: 'hangUp')
43
- @_hangedUp = true
44
63
 
45
64
  # private
46
65
 
@@ -53,40 +72,65 @@ class @WebRTC
53
72
  'OfferToReceiveVideo': true
54
73
 
55
74
  _webSocketInitialize: (url, userToken) ->
56
- @_url = url
57
75
  @_userToken = userToken
58
76
  @_webSocket = new WebSocket(url)
59
77
  @_webSocket.onopen = =>
60
78
  @_startHeartbeat()
61
- @_sendValue('setMyToken',
62
- token: String(userToken)
63
- )
64
- if @_webRTCRreconnecting
79
+ @_sendValue('setMyToken')
80
+ if @_wantWebRTCReconnecting
81
+ @_wantWebRTCReconnecting = false
65
82
  @connect(@_remoteUserID)
66
83
 
67
84
  @_webSocket.onclose = (event) =>
68
- @onWebSocketReconnectingStarted()
85
+ unless @_isWebSocketReconnectingStarted
86
+ @_isWebSocketReconnectingStarted = true
87
+ @onWebSocketReconnectingStarted()
69
88
  @_webSocketInitialize(url, userToken)
70
89
 
71
90
  @_webSocket.onmessage = (data) =>
72
91
  event = JSON.parse(data.data)
73
- switch event['type']
92
+ eventType = event['type']
93
+ if eventType != 'myUserID' && eventType != 'call' && eventType != 'webSocketReconnected'
94
+ if @_remoteUserID != event['remoteUserID']
95
+ return
96
+
97
+ switch eventType
74
98
  when 'myUserID'
75
99
  @myUserID = event['myUserID']
76
100
  if @_webSocketConnected
77
- @onWebSocketReConnected()
101
+ @onWebSocketReconnected()
102
+ if @_hangedUp
103
+ @_sendMessage(type: 'hangUp')
104
+ else
105
+ @_sendMessage(type: 'webSocketReconnected')
78
106
  else
79
107
  @onWebSocketConnected()
80
108
  @_webSocketConnected = true
109
+ when 'webSocketReconnected'
110
+ if @_hangedUp || @_remoteUserID != event['remoteUserID']
111
+ @_sendMessageToOther(type: 'hangUp', event['remoteUserID'])
112
+ when 'callFailed'
113
+ @_callAnswerReceived = true
114
+ @onWebRTCConnectFailed(event['reason'] || WebRTC.UNKNOWN)
81
115
  when 'call'
82
- @_remoteUserID = event['remoteUserID']
116
+ if @_peerStarted
117
+ message =
118
+ type: 'callFailed'
119
+ reason: WebRTC.CALLING
120
+ @_sendMessageToOther(message, event['remoteUserID'])
121
+ else if event['reconnect'] && @_hangedUp
122
+ @_sendMessage(type: 'hangUp')
123
+ else
124
+ @_isCaller = false
125
+ @_remoteUserID = event['remoteUserID']
126
+ @_sendOffer()
127
+ @_peerStarted = true
83
128
  when 'hangUp'
84
- @_hangedUp = true
85
- @_sendMessage(type: 'hangUpAnswer')
86
- @_stop()
87
- when 'hangUpAnswer'
88
- @_stop()
129
+ @_callAnswerReceived = true
130
+ @_hangUp()
89
131
  when 'offer'
132
+ @_callAnswerReceived = true
133
+ @_webRTCReconnecting = false
90
134
  @_onOffer(event)
91
135
  when 'answer'
92
136
  if @_peerStarted
@@ -96,7 +140,7 @@ class @WebRTC
96
140
  @_onCandidate(event)
97
141
 
98
142
  _addNetworkEventListener: ->
99
- window.addEventListener('online', (event) =>
143
+ window.addEventListener('offline', (event) =>
100
144
  @_webSocket.close()
101
145
  )
102
146
 
@@ -116,25 +160,31 @@ class @WebRTC
116
160
  _sendValue: (event, value) ->
117
161
  if @_webSocket.readyState == WebSocket.OPEN
118
162
  @_webSocket.send(JSON.stringify(
163
+ token: @_userToken
119
164
  event: event
120
165
  value: value
121
166
  ))
122
167
 
123
- _sendMessage: (message) ->
168
+ _sendMessageToOther: (message, userID) ->
124
169
  @_sendValue('sendMessage',
125
- userID: String(@_remoteUserID)
170
+ userID: String(userID)
126
171
  message: message
127
172
  )
173
+
174
+ _sendMessage: (message) ->
175
+ @_sendMessageToOther(message, @_remoteUserID)
128
176
 
129
- _startOutput: (video) ->
177
+ _startOutput: (localOutput) ->
178
+ isVideo = (@localOutput? && @localOutput.tagName.toUpperCase() == 'VIDEO')
130
179
  navigator.webkitGetUserMedia(
131
- video: video
180
+ video: isVideo
132
181
  audio: true
133
182
  (stream) =>
134
183
  @_localStream = stream
135
- @localOutput.src = window.URL.createObjectURL(@_localStream)
136
- @localOutput.play()
137
- @localOutput.volume = 0
184
+ if @localOutput?
185
+ @localOutput.src = window.URL.createObjectURL(@_localStream)
186
+ @localOutput.play()
187
+ @localOutput.volume = 0
138
188
  (error) =>
139
189
  console.error('An error occurred: [CODE ' + error.code + ']')
140
190
  )
@@ -190,12 +240,16 @@ class @WebRTC
190
240
  when 'disconnected'
191
241
  @_reconnectPeer()
192
242
  @onWebRTCReconnectingStarted()
243
+ when 'checking'
244
+ @_checking = true
193
245
  when 'connected', 'completed'
194
- if @_hangedUp
195
- @onWebRTCConnected()
196
- else
197
- @onWebRTCReconnected()
198
- @_hangedUp = false
246
+ if @_checking
247
+ @_checking = false
248
+ if @_hangedUp
249
+ @onWebRTCConnected()
250
+ @_hangedUp = false
251
+ else
252
+ @onWebRTCReconnected()
199
253
 
200
254
  peer.addStream(@_localStream)
201
255
  peer.addEventListener('addstream', onRemoteStreamAdded, false)
@@ -204,10 +258,12 @@ class @WebRTC
204
258
 
205
259
  _reconnectPeer: ->
206
260
  @_stop()
207
- if @_webSocket.readyState == WebSocket.OPEN
208
- @connect()
209
- else
210
- @_webRTCRreconnecting = true
261
+ if @_isCaller
262
+ @_webRTCReconnecting = true
263
+ if @_webSocket.readyState == WebSocket.OPEN
264
+ @connect(@_remoteUserID)
265
+ else
266
+ @_wantWebRTCReconnecting = true
211
267
 
212
268
  _sendOffer: ->
213
269
  @_peerConnection = @_prepareNewConnection()
@@ -245,8 +301,14 @@ class @WebRTC
245
301
  return
246
302
  @_peerConnection.setRemoteDescription(new RTCSessionDescription(event))
247
303
 
304
+ _hangUp: ->
305
+ @_stop()
306
+ @_hangedUp = true
307
+ @onWebRTCHangedUp()
308
+
248
309
  _stop: ->
249
- @_peerConnection.removeStream(@_peerConnection.getRemoteStreams()[0])
250
- @_peerConnection.close()
251
- @_peerConnection = null
310
+ if @_peerConnection?
311
+ @_peerConnection.removeStream(@_peerConnection.getRemoteStreams()[0])
312
+ @_peerConnection.close()
313
+ @_peerConnection = null
252
314
  @_peerStarted = false
@@ -15,33 +15,50 @@ EM.run do
15
15
  my_user_id = nil
16
16
 
17
17
  websocket.onclose do
18
- if my_user_id
19
- @websockets[my_user_id].delete(websocket)
18
+ if my_user_id.present?
19
+ if @websockets[my_user_id].present?
20
+ @websockets[my_user_id].delete(websocket)
21
+ end
20
22
  end
21
23
  end
22
24
 
23
25
  websocket.onmessage do |message|
24
26
  data = JSON.parse(message, {symbolize_names: true})
25
- case data[:event]
26
- when 'setMyToken'
27
- token = data[:value][:token]
27
+ if data[:event] != 'heartbeat'
28
+ token = data[:token]
28
29
  if token.present?
29
- my_user_id = User.fetch_by_token(token).id.to_s
30
- @websockets[my_user_id] ||= []
31
- @websockets[my_user_id].push(websocket)
32
- message = {
33
- type: 'myUserID',
34
- myUserID: my_user_id
35
- }
36
- websocket.send JSON.generate(message)
37
- end
38
- when 'sendMessage'
39
- user_id = data[:value][:userID]
40
- type = data[:value][:message][:type]
41
- allow_types = %w/call hangUp hangUpAnswer offer answer candidate/
42
- if @websockets[user_id] && type.present? && allow_types.include?(type)
43
- for ws in @websockets[user_id]
44
- ws.send JSON.generate(data[:value][:message])
30
+ user = User.fetch_by_token(token)
31
+ my_user_id = user ? user.id.to_s : nil
32
+ if my_user_id.present?
33
+ case data[:event]
34
+ when 'setMyToken'
35
+ @websockets[my_user_id] ||= []
36
+ @websockets[my_user_id].push(websocket)
37
+ message = {
38
+ type: 'myUserID',
39
+ myUserID: my_user_id
40
+ }
41
+ websocket.send JSON.generate(message)
42
+ when 'sendMessage'
43
+ user_id = data[:value][:userID]
44
+ type = data[:value][:message][:type]
45
+ Rails.logger.info type
46
+ allow_types = %w/call hangUp offer answer candidate callFailed webSocketReconnected/
47
+ if @websockets.key?(user_id) && type.present? && allow_types.include?(type)
48
+ for ws in @websockets[user_id]
49
+ message = data[:value][:message]
50
+ message[:remoteUserID] = my_user_id
51
+ ws.send JSON.generate(message)
52
+ end
53
+ else
54
+ message = {
55
+ type: 'callFailed',
56
+ reason: 0,
57
+ remoteUserID: user_id
58
+ }
59
+ websocket.send JSON.generate(message)
60
+ end
61
+ end
45
62
  end
46
63
  end
47
64
  end
@@ -1,3 +1,3 @@
1
1
  module WebrtcRails
2
- VERSION = "0.1.4"
2
+ VERSION = "0.1.5"
3
3
  end
metadata CHANGED
@@ -1,7 +1,7 @@
1
1
  --- !ruby/object:Gem::Specification
2
2
  name: webrtc-rails
3
3
  version: !ruby/object:Gem::Version
4
- version: 0.1.4
4
+ version: 0.1.5
5
5
  platform: ruby
6
6
  authors:
7
7
  - Koji Murata