我们如何使用openTok连接为单个会话生成的不同令牌的视频流

时间:2014-03-20 09:49:57

标签: javascript video-streaming webrtc opentok tokbox

你可以看到我想要做的事情。

我希望通过在会话中提供单独的令牌来连接会话中的所有用户 sessionId,以便他们可以查看彼此的流媒体。但用户只能看到他们的 straming。我只需要为每个用户在我的页面上为div分配一个连接到任何令牌的div 特别是sessionId。

这是用户只能看到他们的流媒体的代码

    <script src="http://static.opentok.com/webrtc/v2.0/js/TB.min.js" ></script>
    <script src="https://static.opentok.com/webrtc/v2.0/js/TB.min.js" ></script>
    <script type="text/javascript" src="http://code.jquery.com/jquery-1.7.1.min.js"></script>
   <script type="text/javascript">

    var publisher;
    var session;

    var apiKey = "44686132";


    var sessionId = "1_MX40NDY4NjEzMn4xMjcuMC4wLjF-V2VkIE1hciAxOSAyMDo1ODozNyBQRFQgMjAxNH4wLjAzMTA3MTAwN34";
    var token = document.getElementById("<%= hdn1.ClientID %>").value;


    publisher = TB.initPublisher(apiKey);

    session = TB.initSession(sessionId);

    session.connect(apiKey, token);
    session.addEventListener("sessionConnected",
                           sessionConnectedHandler);


    session.addEventListener("streamCreated",
                           streamCreatedHandler);


    function sessionConnectedHandler(event) {
        alert("sessionConnectedHandler");


        subscribeToStreams(event.streams);
        session.publish(publisher);

    }
        function subscribeToStreams(streams) {

                if (stream.connection.connectionId
               != session.connection.connectionId) {
                    //var streams = event.streams;
                    for (var i = 0; i < streams.length; i++) {
                        var stream = streams[i];

                        var newDivId = "streams" + stream[i].streamId;
                        var newDiv = $('<div />', { id: newDivId });
                        $('body').append(newDiv);
                        if (stream.connection.connectionId
               != session.connection.connectionId) {
                            session.subscribe(stream[i], newDivId);
                        }

                }
            }
        }
        function streamCreatedHandler(event) {
            subscribeToStreams(event.streams);

        }

</script>

2 个答案:

答案 0 :(得分:0)

您的会话和发布商对象超出了其他方法的范围,您可能会遇到错误。如果在方法之外定义会话和发布者变量a(),一切都将按预期工作。

<script type="text/javascript">
  var session, publisher;

  function sessionConnectedHandler(event)...
  ...

答案 1 :(得分:0)

<script src="http://static.opentok.com/webrtc/v2.0/js/TB.min.js" ></script>
<script src="https://static.opentok.com/webrtc/v2.0/js/TB.min.js" ></script>
<script type="text/javascript" src="http://code.jquery.com/jquery-1.7.1.min.js"></script>
<script type="text/javascript">

var publisher;
var session;

var apiKey = "44686132";


var sessionId = "1_MX40NDY4NjEzMn4xMjcuMC4wLjF-V2VkIE1hciAxOSAyMDo1ODozNyBQRFQgMjAxNH4wLjAzMTA3MTAwN34";

var token = document.getElementById("<%= hdn1.ClientID %>").value;


publisher = TB.initPublisher(apiKey);

session = TB.initSession(apiKey,sessionId);
  $.post('generate token url', data)
                .success(function (data, status, headers, config) {

                    session.connect(data.token, function (error) {
                        if (error) 
                        {

                        }
                        });
                    });

session.addEventListener("sessionConnected",
                       sessionConnectedHandler);


session.addEventListener("streamCreated",
                       streamCreatedHandler);


function sessionConnectedHandler(event) {
    alert("sessionConnectedHandler");


    subscribeToStreams(event.streams);
    session.publish(publisher);

}
    function subscribeToStreams(streams) {

            if (stream.connection.connectionId
           != session.connection.connectionId) {
                //var streams = event.streams;
                for (var i = 0; i < streams.length; i++) {
                    var stream = streams[i];

                    var newDivId = "streams" + stream[i].streamId;
                    var newDiv = $('<div />', { id: newDivId });
                    $('body').append(newDiv);
                    if (stream.connection.connectionId
           != session.connection.connectionId) {
                        session.subscribe(stream[i], newDivId);
                    }

            }
        }
    }
    function streamCreatedHandler(event) {
        subscribeToStreams(event.streams);

    }

当你想用不同的令牌连接到单一会话时,

每当新的时候生成令牌。

这段代码可以帮助你。