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:
|
4
|
-
data.tar.gz:
|
3
|
+
metadata.gz: 643e6f36529bf140d6da5ebf72f924c7700b9bea
|
4
|
+
data.tar.gz: 498a7b8496a5c3831aa642cb3d42012d9c2904cc
|
5
5
|
SHA512:
|
6
|
-
metadata.gz:
|
7
|
-
data.tar.gz:
|
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
|
-
|
10
|
+
onWebSocketReconnected: ->
|
7
11
|
onWebRTCConnected: ->
|
8
12
|
onWebRTCReconnectingStarted: ->
|
9
13
|
onWebRTCReconnected: ->
|
10
14
|
onWebRTCHangedUp: ->
|
15
|
+
onWebRTCConnectFailed: (reason) ->
|
11
16
|
|
12
|
-
constructor: (url, userToken,
|
13
|
-
@localOutput =
|
14
|
-
@remoteOutput =
|
15
|
-
@_startOutput(@localOutput
|
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
|
-
|
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
|
-
@
|
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
|
-
|
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
|
-
@
|
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
|
-
@
|
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
|
-
@
|
85
|
-
@
|
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('
|
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
|
-
|
168
|
+
_sendMessageToOther: (message, userID) ->
|
124
169
|
@_sendValue('sendMessage',
|
125
|
-
userID: String(
|
170
|
+
userID: String(userID)
|
126
171
|
message: message
|
127
172
|
)
|
173
|
+
|
174
|
+
_sendMessage: (message) ->
|
175
|
+
@_sendMessageToOther(message, @_remoteUserID)
|
128
176
|
|
129
|
-
_startOutput: (
|
177
|
+
_startOutput: (localOutput) ->
|
178
|
+
isVideo = (@localOutput? && @localOutput.tagName.toUpperCase() == 'VIDEO')
|
130
179
|
navigator.webkitGetUserMedia(
|
131
|
-
video:
|
180
|
+
video: isVideo
|
132
181
|
audio: true
|
133
182
|
(stream) =>
|
134
183
|
@_localStream = stream
|
135
|
-
@localOutput
|
136
|
-
|
137
|
-
|
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 @
|
195
|
-
@
|
196
|
-
|
197
|
-
|
198
|
-
|
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 @
|
208
|
-
@
|
209
|
-
|
210
|
-
|
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
|
250
|
-
|
251
|
-
|
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].
|
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
|
-
|
26
|
-
|
27
|
-
token = data[:value][:token]
|
27
|
+
if data[:event] != 'heartbeat'
|
28
|
+
token = data[:token]
|
28
29
|
if token.present?
|
29
|
-
|
30
|
-
|
31
|
-
|
32
|
-
|
33
|
-
|
34
|
-
|
35
|
-
|
36
|
-
|
37
|
-
|
38
|
-
|
39
|
-
|
40
|
-
|
41
|
-
|
42
|
-
|
43
|
-
|
44
|
-
|
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
|
data/lib/webrtc_rails/version.rb
CHANGED