【问题标题】:OpenTok video chatOpenTok 视频聊天
【发布时间】:2014-01-31 05:22:47
【问题描述】:

我使用 opentok 创建了在线教育视频门户。 学生人数应该看到老师的视频。老师也会看到所有连接学生的视频。 使用以下代码,我可以阻止自己订阅:-

function subscribeToStreams(streams) {
            for (var i = 0; i < streams.length; i++) {
                // Make sure we don't subscribe to ourself

                if (streams[i].connection.connectionId == session.connection.connectionId) {
                    return;
                }

                //if (streams[i].connection.connectionId != session.connection.connectionId) {
                    // Create the div to put the subscriber element in to
                    var container = document.getElementById('videoContainer');
                    var div = document.createElement('div');

                    var id = 'stream' + streams[i].streamId;
                    div.setAttribute('id', id);
                    container.appendChild(div);

                    // Subscribe to the stream
                    session.subscribe(streams[i], div.id);
                    div.style.width = '20%';
                    div.style.height = '20%';
                    div.style.left = '80%';
                //}
            }
        }

我想阻止学生观看其他学生视频。 学生应该只能看到老师的视频。

我们将不胜感激。 谢谢

【问题讨论】:

    标签: javascript opentok


    【解决方案1】:

    最好的方法是使用令牌。假设您正在为每个用户生成一个令牌(您应该这样做),您必须通过设置 connection_data 属性将数据放入每个用户的令牌中。 Example in Ruby。我会将字符串“student”或“teacher”设置到令牌的connection_data中。

    在 streamCreated 事件中,您可以在事件处理程序中查找与流关联的 connection_data:event.stream.connection.data

    在学生方面,您可以简单地检查连接数据,以便在收到 streamCreated 事件时仅订阅教师的流:

    function(e){
      if( e.stream.connection.data == "teacher" ){
        // Create the div to put the subscriber element in to
        var container = document.getElementById('videoContainer');
        var div = document.createElement('div');
    
        var id = 'stream' + streams[i].streamId;
        div.setAttribute('id', id);
        container.appendChild(div);
    
        // Subscribe to the stream
        session.subscribe(streams[i], div.id);
      }
    }
    

    在教师方面,他可以简单地订阅每个传入的流。

    希望对你有帮助,

    祝你好运!

    【讨论】:

    • 如何在发布会话时设置stream.connection.data
    • @akshaykumar6 生成令牌时,您可以设置一些绑定到令牌本身的数据。
    猜你喜欢
    • 1970-01-01
    • 1970-01-01
    • 1970-01-01
    • 1970-01-01
    • 1970-01-01
    • 1970-01-01
    • 1970-01-01
    • 1970-01-01
    • 2017-06-26
    相关资源
    最近更新 更多