Skip to content

Commit

Permalink
fix issue - initSession and connect now takes different parameters
Browse files Browse the repository at this point in the history
  • Loading branch information
songz committed Apr 10, 2014
1 parent b82aa3d commit f978dd3
Show file tree
Hide file tree
Showing 5 changed files with 50 additions and 35 deletions.
11 changes: 5 additions & 6 deletions example/www/js/index.js
Original file line number Diff line number Diff line change
Expand Up @@ -45,22 +45,21 @@ var app = {
xmlhttp.send();
var data = JSON.parse( xmlhttp.response );

// Very simple OpenTok Code
// Very simple OpenTok Code for group video chat
var publisher = TB.initPublisher(data.apiKey,'myPublisherDiv');

var session = TB.initSession( data.sid );
var session = TB.initSession( data.apiKey, data.sid );
session.on({
'sessionConnected': function( event ){
session.publish( publisher );
},
'streamCreated': function( event ){
var div = document.createElement('div');
div.setAttribute('id', 'stream' + event.stream.streamId);
document.body.appendChild(div);
session.subscribe( event.stream, div.id, {subscribeToAudio: false} );
}
});
session.connect( data.apiKey, data.token );
session.connect(data.token, function(){
session.publish( publisher );
});

},
// Update DOM on a Received Event
Expand Down
27 changes: 16 additions & 11 deletions scripts/OpenTok.coffee
Original file line number Diff line number Diff line change
Expand Up @@ -13,20 +13,22 @@ DefaultHeight = 198
# Methods:
# TB.checkSystemRequirements() :number
# TB.initPublisher( apiKey:String [, replaceElementId:String] [, properties:Object] ):Publisher
# TB.initSession( sessionId:String [, production] ):Session
# TB.initSession( apiKey, sessionId ):Session
# TB.log( message )
# TB.off( type:String, listener:Function )
# TB.on( type:String, listener:Function )
# Methods that doesn't do anything:
# TB.setLogLevel(logLevel:String)
# TB.upgradeSystemRequirements()

window.TB =
checkSystemRequirements: ->
return 1
initPublisher: (one, two, three) ->
return new TBPublisher( one, two, three )
initSession: (sid) ->
return new TBSession(sid)
initSession: (apiKey, sessionId ) ->
if( not sessionId? ) then @showError( "OT.initSession takes 2 parameters, your API Key and Session ID" )
return new TBSession(apiKey, sessionId)
log: (message) ->
pdebug "TB LOG", message
off: (event, handler) ->
Expand All @@ -43,6 +45,8 @@ window.TB =
TBUpdateObjects()

# deprecating
showError: (a) ->
alert(a)
addEventListener: (event, handler) ->
@on( event, handler )
removeEventListener: (type, handler ) ->
Expand Down Expand Up @@ -196,7 +200,7 @@ class TBPublisher
# connection ( Connection ) - connection property is only available once session object dispatches sessionConnected event
# sessionId ( String ) - session Id for this session
# Methods:
# connect( apiKey, token )
# 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
Expand All @@ -210,10 +214,11 @@ class TBPublisher
# unpublish( publisher )
# unsubscribe( subscriber )
class TBSession
connect: (apiKey, token, properties={}) ->
pdebug "connect", properties
@apiKey = apiKey
@token = token
connect: (@token, connectCompletionCallback) ->
if( typeof(connectCompletionCallback) != "function" and connectCompletionCallback? )
TB.showError( "Session.connect() takes a token and an optional completionHandler" )
return
if( connectCompletionCallback? ) then @addEventHandlers( "sessionConnected", connectCompletionCallback )
Cordova.exec(@connectionCreatedHandler, TBError, OTPlugin, "addEvent", ["sessConnectionCreated"] )
Cordova.exec(@connectionDestroyedHandler, TBError, OTPlugin, "addEvent", ["sessConnectionDestroyed"] )
Cordova.exec(@sessionConnectedHandler, TBError, OTPlugin, "addEvent", ["sessSessionConnected"] )
Expand All @@ -222,7 +227,7 @@ class TBSession
Cordova.exec(@streamDestroyedHandler, TBError, OTPlugin, "addEvent", ["sessStreamDestroyed"] )
Cordova.exec(@streamPropertyChanged, TBError, OTPlugin, "addEvent", ["sessStreamPropertyChanged"] )
Cordova.exec(@signalReceived, TBError, OTPlugin, "addEvent", ["signalReceived"] )
Cordova.exec(TBSuccess, TBError, OTPlugin, "connect", [@apiKey, @token] )
Cordova.exec(TBSuccess, TBError, OTPlugin, "connect", [@token] )
return
disconnect: () ->
Cordova.exec(TBSuccess, TBError, OTPlugin, "disconnect", [] )
Expand Down Expand Up @@ -299,9 +304,9 @@ class TBSession
TBUpdateObjects()
return Cordova.exec(TBSuccess, TBError, OTPlugin, "unsubscribe", [subscriber.streamId] )

constructor: (@sessionId) ->
constructor: (@apiKey, @sessionId) ->
@userHandlers = {}
Cordova.exec(TBSuccess, TBSuccess, OTPlugin, "initSession", [@sessionId] )
Cordova.exec(TBSuccess, TBSuccess, OTPlugin, "initSession", [@apiKey, @sessionId] )
cleanUpDom: ->
objects = document.getElementsByClassName('OT_root')
for e in objects
Expand Down
7 changes: 5 additions & 2 deletions src/android/com/tokbox/cordova/OpenTokAndroidPlugin.java
Original file line number Diff line number Diff line change
Expand Up @@ -34,6 +34,8 @@


public class OpenTokAndroidPlugin extends CordovaPlugin implements Session.Listener{
private String apiKey;
private String sessionId;
protected Session mSession;
public static final String TAG = "OTPlugin";
public boolean sessionConnected;
Expand Down Expand Up @@ -278,7 +280,8 @@ public boolean execute(String action, JSONArray args, CallbackContext callbackCo

}else if( action.equals( "initSession" )){
Log.i( TAG, "created new session with data: "+args.toString());
mSession = new Session(this.cordova.getActivity().getApplicationContext(), args.getString(0), this );
apiKey = args.getString(0);
mSession = new Session(this.cordova.getActivity().getApplicationContext(), args.getString(1), this );

// publisher methods
}else if( action.equals( "publishAudio") ){
Expand All @@ -304,7 +307,7 @@ public boolean execute(String action, JSONArray args, CallbackContext callbackCo
myEventListeners.put( args.getString(0), callbackContext);
}else if( action.equals( "connect" )){
Log.i( TAG, "connect command called");
mSession.connect( args.getString(0), args.getString(1));
mSession.connect( apiKey, args.getString(0));
}else if( action.equals( "disconnect" )){

}else if( action.equals( "publish" )){
Expand Down
9 changes: 4 additions & 5 deletions src/ios/OpenTokPlugin.m
Original file line number Diff line number Diff line change
Expand Up @@ -42,10 +42,11 @@ -(void)exceptionHandler:(CDVInvokedUrlCommand*)command{
// Called by TB.initsession()
-(void)initSession:(CDVInvokedUrlCommand*)command{
// Get Parameters
NSString* sessionId = [command.arguments objectAtIndex:0];
NSString* apiKey = [command.arguments objectAtIndex:0];
NSString* sessionId = [command.arguments objectAtIndex:1];

// Create Session
_session = [[OTSession alloc] initWithApiKey:@"44443122" sessionId:sessionId delegate:self];
_session = [[OTSession alloc] initWithApiKey: apiKey sessionId:sessionId delegate:self];

// Initialize Dictionary, contains DOM info for every stream
subscriberDictionary = [[NSMutableDictionary alloc] init];
Expand Down Expand Up @@ -158,9 +159,7 @@ - (void)connect:(CDVInvokedUrlCommand *)command{
NSLog(@"iOS Connecting to Session");

// Get Parameters
NSString* tbKey = [command.arguments objectAtIndex:0];
NSString* tbToken = [command.arguments objectAtIndex:1];

NSString* tbToken = [command.arguments objectAtIndex:0];
[_session connectWithToken:tbToken error:nil];
}

Expand Down
31 changes: 20 additions & 11 deletions www/opentok.js

Some generated files are not rendered by default. Learn more about how customized files appear on GitHub.

0 comments on commit f978dd3

Please sign in to comment.