1

以下代码在桌面浏览器上运行良好,但在我的手机间隙/cordova 应用程序中不起作用。我的输出显示连接并且 iOS 接收到流,但我的应用程序主体中没有显示任何内容。这是在我的 index.js 文件中:

var app = {
initialize: function() {
    this.bindEvents();
},

bindEvents: function() {
    document.addEventListener('deviceready', this.onDeviceReady, false);
},
onDeviceReady: function() {
    app.receivedEvent('deviceready');

////////////////////////////////
//////Start of my opentok code////////////

    var apiKey = '21692492';

    var sessionId = '2_MX4yMTY5MTQ5Mn5-RnJpIEZlYiAwNyAwODozMjozOSBQU1QgMjAxNH4wLjIwMzc2MDV-';

    var token = 'T1==cGFydG5lcl9pZD0yMTY5MTQ5MiZzaWc9ZWUxMTNjNjZiYjlkNWI4NTkwZTE2MDZiMjM0MzFkOWYyMzhiYzgxNjpzZXNzaW9uX2lkPTJfTVg0eU1UWTVNVFE1TW41LVJuSnBJRVpsWWlBd055QXdPRG96TWpvek9TQlFVMVFnTWpBeE5INHdMakl3TXpjMk1EVi0mY3JlYXRlX3RpbWU9MTM5MTc5MDgwNSZyb2xlPXB1Ymxpc2hlciZub25jZT0xMzkxNzkwODA1LjIzMzk0MTE4MzcyJmV4cGlyZV90aW1lPTEzOTQzODI4MDU=';


    function connectedHandler(event) {
        for (var i = 0; i < event.streams.length; i++) {
            var newDiv = $('<div />', {id:event.streams[i].streamId});
            $('body').append(newDiv);
            session.subscribe(event.streams[i], event.streams[i].streamId,{});
        }
    }


    var session = TB.initSession(sessionId);

    session.addEventListener('sessionConnected', connectedHandler);

    session.connect(apiKey, token);

//////End of my opentok code////////////
////////////////////////////////////////////////
},

receivedEvent: function(id) {
    var parentElement = document.getElementById(id);
    var listeningElement = parentElement.querySelector('.listening');
    var receivedElement = parentElement.querySelector('.received');

    listeningElement.setAttribute('style', 'display:none;');
    receivedElement.setAttribute('style', 'display:block;');

    console.log('Received Event: ' + id);
}
};

这是我得到的输出:

2014-02-07 11:44:19.407 HelloCordova[2377:60b] iOS Connected to Session
2014-02-07 11:44:19.408 HelloCordova[2377:60b] object for session is {
connection =     {
    connectionId = "4B191468-46D5-4414-A46A-5C97376D6F2E";
    creationTime = 1391791459;
};
connectionCount = 0;
environment = production;
sessionConnectionStatus = OTSessionConnectionStatusConnected;
sessionId = "2_MX4yMTY5MTQ5Mn5-RnJpIEZlYiAwNyAwODozMjozOSBQU1QgMjAxNH4wLjIwMzc2MDV-";
streams =     (
);
}
2014-02-07 11:44:19.456 HelloCordova[2377:60b] iOS Received Stream

有谁知道为什么这不起作用以及我需要改变什么?

4

1 回答 1

0

Cordova 插件以 OpenTok JS 2.2 为蓝本。在这个新版本中,有几个变化。首先,“addEventListener”已重命名为“on”。要添加事件,您可以这样做:

session.on({
  'sessionConnected': function(event){
    session.publish( publisher );
  },
  'streamCreated': function(event){
    var newDiv = $('<div />', {id:event.stream.streamId});
    $('body').append(newDiv);
    session.subscribe(event.stream, event.stream.streamId, {});
  }
})

请注意以下更改:
* addEventListener 不再存在
* 在 sessionConnected 事件中,您不再获得会话中现有流的数组。会话中的每个流都会触发一个 streamCreated 事件。
* streamCreated 事件回调参数只有 1 个流元素。此事件将为每个流触发一次

以下是即将到来的 v2.2 更改列表:http: //labs.tokbox.com/javascript-2.2

我添加了一些工作示例代码供您参考:https ://github.com/songz/cordova-plugin-opentok/blob/master/README.md#sample-code

于 2014-03-06T23:34:33.260 回答