Name

connection (LzConnection) (as2) — A connection manager to create a persistent connection and an authenticated session.

Synopsis

LZX: connection
JavaScript: LzConnection
Type: Class
Access: public
Runtimes: as2
Topic: LFC.Data
Declared in: WEB-INF/lps/lfc/data/platform/swf/LzConnection.lzs

Description

The connection tag instantiates a connection manager object that handles asynchronous messages sent from the OpenLaszlo Server. Use the the LzConnection.connect method to establish a persistent connection and disconnect to close it down.

Example 3. Connection example

 <canvas debug="true" height="200">
   <debug y="60"/>
   <connection authenticator="anonymous">
     <handler name="onconnect">
       Debug.write('connected');
     </handler>
 
     <handler name="ondisconnect">
       Debug.write('client disconnected');
     </handler>
   </connection>
 
   <view>
     <simplelayout axis="x" spacing="5"/>
 
     <button>connect
       <handler name="onclick">
         canvas.connection.connect();
       </handler>
     </button>
     <button>disconnect
       <handler name="onclick">
 
         canvas.connection.disconnect();
       </handler>
     </button>
   </view>
 </canvas>
 

You can optionally use the clientDisconnect method to close down only the client-side connection. The server will drop its end of the connection when it next becomes aware that the connection has been dropped.

The previous example declares connection with authenticator="anonymous", which tells the connection manager that no authentication should be used to handle the persistent connection. Normally, the connection manager will tell the OpenLaszlo Server to verify that your connection is authenticated with a back-end authentication server before allowing the persistent connection, which is done through a session cookie. The default authentication server it uses is the AuthenticationServlet bundled with the OpenLaszlo Server. The set of usernames that server uses is located in lps/config/lzusers.xml. To session and unsession your application, use the connection's login and logout methods.

The next example demonstrates how to login with the username adam. Note that you will be allowed to connect only after logging in.

Example 4. Session Example

 <canvas debug="true" height="200">
   <debug y="60"/>
 
   <connection>
     <handler name="onconnect">
 
       Debug.write('connected as ' + connection.getUsername()); 
     </handler>
     <handler name="ondisconnect">
       Debug.write('client disconnected');
     </handler>
   </connection>
 
   <datapointer xpath="connection:loginDset:/login[1]/authentication[1]/response[1]/status[1]">
 
     <handler name="ondata">
       var statusMessage = this.xpathQuery('@msg');
       Debug.write('login: ' + statusMessage);
     </handler>
     <handler name="onerror">
       Debug.write('login resulted in error');
     </handler>
     <handler name="ontimeout">
 
       Debug.write('login timed out');
     </handler>
   </datapointer>
 
   <datapointer xpath="connection:logoutDset:/logout[1]/authentication[1]/response[1]/status[1]">
     <handler name="ondata">
       var statusMessage = this.xpathQuery('@msg');
       Debug.write('logout: ' + statusMessage);
     </handler>
 
     <handler name="onerror">
       Debug.write('logout resulted in error');
     </handler>
     <handler name="ontimeout">
       Debug.write('logout timed out');
     </handler>
   </datapointer>
 
   <view>
     <simplelayout axis="x" spacing="5"/>
     <button>login
       <handler name="onclick">
         canvas.connection.login('adam', 'adam');
       </handler>
     </button>
 
     <button>logout
       <handler name="onclick">
         canvas.connection.logout();
       </handler>
     </button>
     <simplelayout axis="x" spacing="5"/>
     <button>connect
       <handler name="onclick">
 
         canvas.connection.connect();
       </handler>
     </button>
     <button>disconnect
       <handler name="onclick">
           canvas.connection.disconnect();
       </handler>
     </button>
 
   </view>
 </canvas>
 

Once a connection is established, clients can send and receive messages using the connectiondatasource tag.

Sample Apps

Sample applications that use the connection tag are:

Superclass Chain

node (LzNode) » datasource (LzDatasource) » datasource (LzHTTPDatasource) » connection (LzConnection)

Known Subclasses

Details

Initial Attributes (7)

Initial Attributes are given as attributes in LZX but are not generally available as properties in JavaScript.

authenticator
<attribute name="authenticator" />
authparam
<attribute name="authparam" />
group
<attribute name="group" />
heartbeat
<attribute name="heartbeat" type="number" />
min is 1000ms
secure
<attribute name="secure" />
secureport
<attribute name="secureport" />
timeout
<attribute name="timeout" type="number" />
min is 1000ms

Methods (10)

clientDisconnect()
<method name="clientDisconnect" />
public function clientDisconnect();
Disconnect connection, but don't notify server. See disconnect().
connect()
<method name="connect" />
public function connect();
Establish a connection. An application typically must be sessioned before calling this method.
disconnect()
<method name="disconnect" />
public function disconnect();
Disconnect connection. This also notifies the server to disconnect the application. The results from asking the server to disconnect are returned in the connection's disconnectDset. See clientDisconnect().
getAuthParam()
<method name="getAuthParam" />
public function getAuthParam() : String;
Get parameters for authenticator.
getLoaderForDataset()
<method name="getLoaderForDataset" args="forset, proxied" />
public function getLoaderForDataset(forset, proxied);
getSID()
<method name="getSID" />
public function getSID() : String;
Get connection's session id.
getUsername()
<method name="getUsername" />
public function getUsername() : String;
Get connection's username.
login()
<method name="login" args="usr, pwd" />
public function login(usr : String, pwd : String);
Authenticate and session the application. The results from this call are returned in the connection's loginDset.
logout()
<method name="logout" />
public function logout();
De-authenticate and unsession the application. The results from this call are returned in the connection's logoutDset.
setAuthParam()
<method name="setAuthParam" args="authparam" />
public function setAuthParam(authparam : String);
Set paramaters for server authenticator.

LZX Synopsis

<class name="LzConnection" extends=" LzHTTPDatasource ">
  <method name=" clientDisconnect " />
  <method name=" connect " />
  <method name=" disconnect " />
  <method name=" getAuthParam " />
  <method name=" getLoaderForDataset " args="forset, proxied" />
  <method name=" getSID " />
  <method name=" getUsername " />
  <method name=" login " args="usr, pwd" />
  <method name=" logout " />
  <method name=" setAuthParam " args="authparam" />
</class>

JavaScript Synopsis

public LzConnection extends  LzHTTPDatasource  {
  prototype public function clientDisconnect ();
  prototype public function connect ();
  prototype public function disconnect ();
  prototype public function getAuthParam () : String;
  prototype public function getLoaderForDataset (forset, proxied);
  prototype public function getSID () : String;
  prototype public function getUsername () : String;
  prototype public function login (usr : String, pwd : String);
  prototype public function logout ();
  prototype public function setAuthParam (authparam : String);
}