Connection Handlers while using AutobahnJS in wamp ws

360 Views Asked by At

I am using the Autobahn JS for creating a connection with Wamp WS version 1. The code used for connection is:

ab.connect(serverUrl,function (session) {
        sess = session ;

        sess.prefix("event", "abc/");

        console.log("Connected to " + serverUrl);



        sess.subscribe("event:topicDemo",onMessage);


        }

    },
    function(code, reason){
        sess = null;
        Console.log("Client disconnected");
    }
    );

Here the connection is established successfully. But I want to use a mechanism in which I would subscribe to the topic after the connection is established. Plus I want to use the session object in future as per the requirement. (In this case ab.connect works asynchronously, due to which the session object cannot be used in future) Is it possible?

Thanks in advance.

1

There are 1 best solutions below

0
On

The connection stores a session object (connection.session), then after your onopen event has finished, you can store a boolean: isConnectionReady = true

And I don't know how you want to use the session object in the future, but all you need to do is waiting for onopen event, after that, your session object exists until an onclose event is emitted (or connection.isOpen() == false).