connection (LzConnection) (as2) — A connection manager to create a persistent connection and an authenticated session.
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 applications that use the connection
tag are:
Initial Attributes (7)
Initial Attributes are given as attributes in LZX but are not generally available as properties in JavaScript.
Methods (10)
Copyright © 2002-2007 Laszlo Systems, Inc. All Rights Reserved. Unauthorized use, duplication or distribution is strictly prohibited. This is the proprietary information of Laszlo Systems, Inc. Use is subject to license terms.