0

I am new to the Opentok API and I am writing tests. In my tests I simulated disconnecting from the room and connecting again, but if I execute it at various times, the chat is runs slowly, until broken.

To init the chat I run $scope.initSession() and to disconnect I run $scope.disconnectFromSession().

I am using the Opentok.js version 2.13.2. See my following code:

var apiKey, sessionId, token;

apiKey = //my api key;
sessionId = //my sessionid;
token = //my token;

var session = null;
var publisher = null;
var stream = null;
var connectionCount = 0;
var publisherProperties = {frameRate: 7};

$scope.connected = false;

$scope.initSession = function() {
    if (OT.checkSystemRequirements() == 1) {
        // Initialize Session Object
        session = OT.initSession(apiKey, sessionId);

        createElement("publisher");
        // initialize a publisher
        publisher = OT.initPublisher('publisher', publisherProperties);

        session.on({
            streamCreated: function(event) {
                console.log("EVENT streamCreated: " + event.stream.name + " - " + event.reason);
                createElement("subscriber");
                stream = event.stream;
                session.subscribe(stream, 'subscriber');
            },
            streamDestroyed: function(event) {
                event.preventDefault();
                console.log("EVENT streamDestroyed: " + event.stream.name + " - " + event.reason);
                console.log('Stream ${event.stream.name} ended because ${event.reason}.');
            }                
        });
        connectToSession();
    } else {
        console.log('Browser haven´t support to WebRTC');
    }
}

function connectToSession() {
    session.connect(token, function(err) {
        if (err) {
            if (err.name === "OT_NOT_CONNECTED") {
                showMessage('Failed to connect. Please check your connection and try connecting again.');
            } else {
                showMessage('An unknown error occurred connecting. Please try again later.');
            }
        } else {
            // publish to the session
            session.publish(publisher);
            $scope.connected = true;
        }
    });
}

function createElement(id) {
    console.log(document.getElementById(id));
    if (document.getElementById(id) === null) {
        var divPublisher = document.createElement("div");
        divPublisher.setAttribute("id", id);
        document.getElementById("div-videos").appendChild(divPublisher);
    }
}

$scope.disconnectFromSession = function() {
    session.disconnect();
    $scope.connected = false;
    OT.off();
}

$scope.initSession();

I appreciate any help.

lloydpick
  • 1,638
  • 1
  • 18
  • 24
  • Is there any reason you're using event.preventDefault in the streamDestroyed handler? The default behaviour is to clean up the subscriber. So if you are preventing that and then connecting and disconnecting you might be ending up with a bunch of hanging subscribers. But not sure if that would cause things to slow down or not. – Adam Ullman Apr 23 '18 at 21:46
  • Hello Adam, thaks for your ask. – Bruno Polone Apr 25 '18 at 00:59
  • Hi Adam, thanks for your answer. I resolved. My problem is that I was calling the `$scope.initSession()` function many times and in it is added multiple listeners in `on(...)` . I changed my code to start this listener only once. – Bruno Polone Apr 25 '18 at 01:06

0 Answers0