This repository has been archived by the owner on Jan 18, 2023. It is now read-only.
forked from msach22/cordova-plugin-opentok
-
Notifications
You must be signed in to change notification settings - Fork 78
/
Copy pathOTSession.coffee
288 lines (282 loc) · 11.2 KB
/
OTSession.coffee
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
95
96
97
98
99
100
101
102
103
104
105
106
107
108
109
110
111
112
113
114
115
116
117
118
119
120
121
122
123
124
125
126
127
128
129
130
131
132
133
134
135
136
137
138
139
140
141
142
143
144
145
146
147
148
149
150
151
152
153
154
155
156
157
158
159
160
161
162
163
164
165
166
167
168
169
170
171
172
173
174
175
176
177
178
179
180
181
182
183
184
185
186
187
188
189
190
191
192
193
194
195
196
197
198
199
200
201
202
203
204
205
206
207
208
209
210
211
212
213
214
215
216
217
218
219
220
221
222
223
224
225
226
227
228
229
230
231
232
233
234
235
236
237
238
239
240
241
242
243
244
245
246
247
248
249
250
251
252
253
254
255
256
257
258
259
260
261
262
263
264
265
266
267
268
269
270
271
272
273
274
275
276
277
278
279
280
281
282
283
284
285
286
287
288
# Session Object:
# Properties:
# capabilities ( Capabilities ) - A Capabilities object includes info about capabilities of the client. All properties of capabilities object are undefined until connected to a session
# connection ( Connection ) - connection property is only available once session object dispatches sessionConnected event
# sessionId ( String ) - session Id for this session
# Methods:
# connect( token, completionHandler )
# disconnect()
# forceDisconnect( connection ) - forces a remote connection to leave the session
# forceUnpublish( stream ) - forces publisher of the spicified stream to stop publishing the stream
# getPublisherForStream( stream ) - returns the local publisher object for a given stream
# getSubscribersForStream( stream ) - returns array of local subscriber objects for a given stream
# off( type, listener )
# on( type, listener )
# publish( publisher ) - starts publishing
# signal( signal, completionHandler)
# subscribe( stream, targetElement, properties ) : subscriber
# unpublish( publisher )
# unsubscribe( subscriber )
class TBSession
connect: (@token, connectCompletionCallback) ->
if( typeof(connectCompletionCallback) != "function" and connectCompletionCallback? )
TB.showError( "Session.connect() takes a token and an optional completionHandler" )
return
if (connectCompletionCallback?)
errorCallback = (error) -> connectCompletionCallback(error)
successCallback = () -> connectCompletionCallback(null)
Cordova.exec(@eventReceived, TBError, OTPlugin, "addEvent", ["sessionEvents"] )
Cordova.exec(successCallback, errorCallback, OTPlugin, "connect", [@token] )
return
disconnect: () ->
Cordova.exec(TBSuccess, TBError, OTPlugin, "disconnect", [] )
forceDisconnect: (connection) ->
return @
forceUnpublish: (stream ) ->
return @
getPublisherForStream: (stream) ->
return @
getSubscribersForStream: (stream) ->
return @
publish: (divObject, properties) =>
if( @alreadyPublishing )
pdebug("Session is already publishing", {})
return
@alreadyPublishing = true
@publisher = new TBPublisher(divObject, properties)
@publish( @publisher )
publish: () =>
if( @alreadyPublishing )
pdebug("Session is already publishing", {})
return
@alreadyPublishing = true
if(typeof arguments[0] == "object")
@publisher = arguments[0]
else
@publisher = OT.initPublisher(arguments)
@publisher.setSession(@)
Cordova.exec(TBSuccess, OTPublisherError, OTPlugin, "publish", [] )
return @publisher
signal: (signal, signalCompletionHandler) ->
# signal payload: [type, data, connection( separated by spaces )]
type = if signal.type? then signal.type else ""
data = if signal.data? then signal.data else ""
to = if signal.to? then signal.to else ""
to = if typeof(to)=="string" then to else to.connectionId
Cordova.exec(TBSuccess, TBError, OTPlugin, "signal", [type, data, to] )
return @
subscribe: (one, two, three, four) ->
@subscriberCallbacks = {}
if( four? )
# stream,domId, properties, completionHandler
subscriber = new TBSubscriber(one, two, three)
@subscriberCallbacks[one.streamId] = four
@subscribers[one.streamId] = subscriber
return subscriber
if( three? )
# stream, domId, properties || stream, domId, completionHandler || stream, properties, completionHandler
if( (typeof(two) == "string" || two.nodeType == 1 || two instanceof Element) && typeof(three) == "object" )
console.log("stream, domId, props")
subscriber = new TBSubscriber(one, two, three)
@subscribers[one.streamId] = subscriber
return subscriber
if( (typeof(two) == "string" || two.nodeType == 1 || two instanceof Element) && typeof(three) == "function" )
console.log("stream, domId, completionHandler")
@subscriberCallbacks[one.streamId]=three
subscriber = new TBSubscriber(one, two, {})
@subscribers[one.streamId] = subscriber
return subscriber
if(typeof(two) == "object" && typeof(three) == "function" )
console.log("stream, props, completionHandler")
@subscriberCallbacks[one.streamId] = three
domId = TBGenerateDomHelper()
subscriber = new TBSubscriber( one, domId, two )
@subscribers[one.streamId] = subscriber
return subscriber
if( two? )
# stream, domId || stream, properties || stream,completionHandler
if( (typeof(two) == "string" || two.nodeType == 1 || two instanceof Element) )
subscriber = new TBSubscriber(one, two, {})
@subscribers[one.streamId] = subscriber
return subscriber
if( typeof(two) == "object" )
domId = TBGenerateDomHelper()
subscriber = new TBSubscriber(one, domId, two)
@subscribers[one.streamId] = subscriber
return subscriber
if( typeof(two) == "function" )
@subscriberCallbacks[one.streamId] = two
domId = TBGenerateDomHelper()
subscriber = new TBSubscriber(one, domId, {})
@subscribers[one.streamId] = subscriber
return subscriber
# stream
domId = TBGenerateDomHelper()
subscriber = new TBSubscriber(one, domId, {})
@subscribers[one.streamId] = subscriber
return subscriber
unpublish:() ->
@alreadyPublishing = false
console.log("JS: Unpublish")
element = @publisher.pubElement
if(element)
@resetElement(element)
TBUpdateObjects()
return Cordova.exec(TBSuccess, TBError, OTPlugin, "unpublish", [] )
unsubscribe: (subscriber) ->
console.log("JS: Unsubscribe")
elementId = subscriber.streamId
element = document.getElementById( "TBStreamConnection#{elementId}" )
console.log("JS: Unsubscribing")
element = streamElements[ elementId ]
if(element)
@resetElement(element)
delete( streamElements[ elementId ] )
TBUpdateObjects()
return Cordova.exec(TBSuccess, TBError, OTPlugin, "unsubscribe", [subscriber.streamId] )
constructor: (@apiKey, @sessionId) ->
@apiKey = @apiKey.toString()
@connections = {}
@streams = {}
@subscribers = {}
@alreadyPublishing = false
OT.getHelper().eventing(@)
Cordova.exec(TBSuccess, TBSuccess, OTPlugin, "initSession", [@apiKey, @sessionId] )
cleanUpDom: ->
objects = document.getElementsByClassName('OT_root')
while( objects.length > 0 )
e = objects[0]
if e
@resetElement(e)
objects = document.getElementsByClassName('OT_root')
resetElement: (element) =>
insertMode = element.getAttribute('data-insertMode')
if (insertMode == "replace")
attributes = ['style', 'data-streamid', 'class', 'data-insertMode']
elementChildren = element.childNodes
element.removeAttribute attribute for attribute in attributes
for childElement in elementChildren
childClass = childElement.getAttribute 'class'
if childClass == 'OT_video-container'
element.removeChild childElement
break
else
element.parentNode.removeChild(element)
return
# event listeners
# todo - other events: connectionCreated, connectionDestroyed, signal?, streamPropertyChanged, signal:type?
eventReceived: (response) =>
@[response.eventType](response.data)
connectionCreated: (event) =>
connection = new TBConnection( event.connection )
connectionEvent = new TBEvent("connectionCreated")
connectionEvent.connection = connection
@connections[connection.connectionId] = connection
@dispatchEvent(connectionEvent)
return @
connectionDestroyed: (event) =>
connection = @connections[ event.connection.connectionId ]
connectionEvent = new TBEvent("connectionDestroyed")
connectionEvent.connection = connection
connectionEvent.reason = "clientDisconnected"
@dispatchEvent(connectionEvent)
delete( @connections[ connection.connectionId] )
return @
sessionConnected: (event) =>
@dispatchEvent(new TBEvent("sessionConnected"))
@connection = new TBConnection( event.connection )
@connections[event.connection.connectionId] = @connection
event = null
return @
sessionDisconnected: (event) =>
@alreadyPublishing = false
sessionDisconnectedEvent = new TBEvent("sessionDisconnected")
sessionDisconnectedEvent.reason = event.reason
@dispatchEvent(sessionDisconnectedEvent)
@cleanUpDom()
return @
sessionReconnected: (event) =>
sessionEvent = new TBEvent("sessionReconnected")
@dispatchEvent(sessionEvent)
return @
sessionReconnecting: (event) =>
sessionEvent = new TBEvent("sessionReconnecting")
@dispatchEvent(sessionEvent)
return @
streamCreated: (event) =>
stream = new TBStream( event.stream, @connections[event.stream.connectionId] )
@streams[ stream.streamId ] = stream
streamEvent = new TBEvent("streamCreated")
streamEvent.stream = stream
#streamEvent = new TBEvent( {stream: stream } )
@dispatchEvent(streamEvent)
return @
streamDestroyed: (event) =>
stream = @streams[event.stream.streamId]
streamEvent = new TBEvent("streamDestroyed")
streamEvent.stream = stream
streamEvent.reason = "clientDisconnected"
@dispatchEvent(streamEvent)
# remove stream DOM
if(stream)
element = streamElements[ stream.streamId ]
if(element)
@resetElement(element)
delete( streamElements[ stream.streamId ] )
TBUpdateObjects()
delete( @streams[ stream.streamId ] )
return @
streamPropertyChanged: (event) ->
stream = new TBStream(event.stream, @connections[event.stream.connectionId])
if(@publisher && @publisher.stream && @publisher.stream.streamId == stream.streamId)
@publisher.stream = stream
else if(@subscribers[stream.streamId])
@subscribers[stream.streamId].stream = stream
@streams[stream.streamId] = stream
streamEvent = new TBEvent("streamPropertyChanged")
streamEvent.stream = event.stream
streamEvent.changedProperty = event.changedProperty
streamEvent.oldValue = event.oldValue
streamEvent.newValue = event.newValue
@dispatchEvent(streamEvent)
return @
subscribedToStream: (event) =>
streamId = event.streamId
callbackFunc = @subscriberCallbacks[streamId]
if !callbackFunc?
return
if event.errorCode?
error = new OTError(event.errorCode)
callbackFunc(error)
return
else
callbackFunc()
return
signalReceived: (event) =>
streamEvent = new TBEvent("signal")
streamEvent.data = event.data
streamEvent.from = @connections[event.connectionId]
@dispatchEvent(streamEvent)
streamEvent = new TBEvent("signal:#{event.type}")
streamEvent.data = event.data
streamEvent.from = @connections[event.connectionId]
@dispatchEvent(streamEvent)
return @
archiveStarted: (event) ->
streamEvent = new TBEvent("archiveStarted")
streamEvent.id = event.id
streamEvent.name = event.name
@dispatchEvent(streamEvent)
return @
archiveStopped: (event) ->
streamEvent = new TBEvent("archiveStopped")
streamEvent.id = event.id
@dispatchEvent(streamEvent)
return @
# deprecating
addEventListener: (event, handler) -> # deprecating soon
@on( event, handler )
return @
removeEventListener: ( event, handler ) ->
@off( event, handler )
return @