How to properly disconnect from the server?

Hi,

-Version: 5.5.2-1043 (Amazon AWS)
-WebSDK: 2.0.202


In my webapp i use the method
Code:
stream.stop()
when i leave the page where the stream is shown. I am wondering if and how i disconnect from the flashphoner websocket or isn't this neccessary? It will be automatically disconnected after a while i saw in the flashphoner.log but i'm wondering if i should manually disconnect once i stop the stream.


Best,
Thomas
 

Max

Administrator
Staff member
Good day.
Use the session.disconnect() method to close the websocket session, for example
Code:
function connect() {
    var url = $('#urlServer').val();

    //create session
    console.log("Create new session with url " + url);
    Flashphoner.createSession({urlServer: url}).on(SESSION_STATUS.ESTABLISHED, function (session) {
        setStatus("#connectStatus", session.status());
        onConnected(session);
        ...
    });
}

function onConnected(session) {
    $("#connectBtn").text("Disconnect").off('click').click(function () {
        $(this).prop('disabled', true);
        session.disconnect();
    }).prop('disabled', false);
    onUnpublished();
    onStopped();
}
See Two Way Streaming example source code.
 
I use

Code:
stream.stop()
followed by a

Code:
session.disconnect()
However, the stream is still in status PLAYING

If i wrap the disconnect like this it works (i get the status STOPPED in the flashphoner log, otherwise i get FAILED)
Code:
    if (this.LOCAL_SESSION) {
      setTimeout(() => {
        this.LOCAL_SESSION.disconnect()
      }, 1000);
    }
Do you know a way to fix this?
 

Max

Administrator
Staff member
You should use STREAM_STATUS.STOPPED event after stream.stop() call to disconnect the session, for example
Code:
    session.createStream({
        name: streamName,
        display: remoteVideo
    }).on(STREAM_STATUS.PENDING, function (stream) {
        ...
    }).on(STREAM_STATUS.PLAYING, function (stream) {
        ...
    }).on(STREAM_STATUS.STOPPED, function () {
        session.disconnect();
    }).on(STREAM_STATUS.FAILED, function (stream) {
        session.disconnect();
    }).play();
 
Top