当前位置: 动力学知识库 > 问答 > 编程问答 >

javascript - Opentok switching between sessions

问题描述:

I am making an application where users can switch between different voice rooms (sessions).

The function 'connectToSession' (see below) checks if the user is already in a session, if so will disconnect from current session and then connect to the other session and publish its stream to that session.

The switching of voice rooms works fine and to the user is all apears to be working, however every time a user switches session I get this error in the console:

Publisher State Change Failed: 'Publishing' cannot transition to 'PublishingToSession'

Publisher State Change Failed: 'Publishing' cannot transition to 'Publishing'

From my debugging it seems to have occurred on the line where session.publish is called

var session = null;

var publisher = null;

var subscribers = {};

function connectToSession(sessionId, token) {

if (session) {

session.disconnect();

}

if (!publisher) {

//First time so we need to initialise publisher

var pubOptions = {

videoSource: null,

name: user_id

};

publisher = OT.initPublisher(null, pubOptions, function() {

//Publisher initialised

});

publisher.on({

'streamDestroyed': function(event) {

event.preventDefault();

}

});

}

session = OT.initSession(apiKey, sessionId);

session.on({

'streamCreated': function(event) {

// Subscribe to others stream

subscribers[event.stream.name] = session.subscribe(event.stream);

},

'sessionConnected': function(sessionConnectEvent) {

// Session Connected

},

'streamDestroyed': function(event) {

//Stream removed from session

delete subscribers[event.stream.name];

}

});

session.connect(token,

function(error) {

if (error) {

console.error(error);

}

else {

session.publish(publisher, function() {

//Finished publishing

}

);

}

});

}

Any ideas what is causing this error?

网友答案:
if (session) {
    if (publisher) {
        session.unpublish(publisher);
    }
    session.disconnect();
}

A workaround is to explicitly call unpublish for a publisher before disconnecting from the session.

分享给朋友:
您可能感兴趣的文章:
随机阅读: