-
Notifications
You must be signed in to change notification settings - Fork 7
Expand file tree
/
Copy pathwebrtc.coffee
More file actions
94 lines (78 loc) · 2.03 KB
/
webrtc.coffee
File metadata and controls
94 lines (78 loc) · 2.03 KB
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
61
62
63
64
65
66
67
68
69
70
71
72
73
74
75
76
77
78
79
80
81
82
83
84
85
86
87
88
89
90
91
92
93
94
Base = require './base'
debug = require('debug') 'fbp-protocol-client:webrtc'
class WebRTCRuntime extends Base
constructor: (definition) ->
super definition
@peer = null
@connecting = false
@connection = null
@protocol = 'webrtc'
@buffer = []
getElement: ->
return null
isConnected: ->
return @connection != null
connect: ->
return if @connection or @connecting
address = @getAddress()
if (address.indexOf('#') != -1)
signaller = address.split('#')[0]
id = address.split('#')[1]
else
signaller = 'https://api.flowhub.io'
id = address
options =
room: id
debug: true
channels:
chat: true
signaller: signaller
capture: false
constraints: false
expectedLocalStreams: 0
@peer = RTC options
@peer.on 'channel:opened:chat', (id, dc) =>
@connection = dc
@connection.onmessage = (data) =>
debug 'message', data.data
@handleMessage data.data
@connecting = false
@sendRuntime 'getruntime', {}
@emit 'status',
online: true
label: 'connected'
@emit 'connected'
@flush()
@peer.on 'channel:closed:chat', (id, dc) =>
dc.onmessage = null
@connection = null
@emit 'status',
online: false
label: 'disconnected'
@emit 'disconnected'
@connecting = true
disconnect: ->
return unless @connection
@connecting = false
@connection.close()
@connection = null
@emit 'disconnected'
send: (protocol, command, payload) ->
m = @_prepareMessage protocol, command, payload
if @connecting
@buffer.push m
return
return unless @connection
debug 'send', m
@connection.send JSON.stringify m
handleError: (error) =>
@connection = null
@connecting = false
handleMessage: (message) =>
msg = JSON.parse message
@recvMessage msg
flush: ->
for item in @buffer
@send item.protocol, item.command, item.payload
@buffer = []
module.exports = WebRTCRuntime