0

I want to Connect all the users in a session by providing them separate token under a sessionId, so that they can view each other's streaming. But users can see only their straming. I just need to allocate div on my page for each user with a token connected to any particular sessionId.

This is the code using which users can see their streaming only

 <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>

Moreover, alert("sessionConnectedHandler"); inside
function sessionConnectedHandler(event) never gets called. What am I doing wrong here?

1 Answers1

0

Please consult your browser console, it will help you solve most of your problems. Here's a few quick bugs I have discovered in your code:

  • You included the javascript library multiple times.
  • Your token is probably retrieved incorrectly. Please print your token out in the console and verify that it looks like a token and not null or give you an error.
  • subscribeToStreams method takes in an array of streams. stream variable in the method is not defined anywhere
songz
  • 2,072
  • 1
  • 13
  • 18