Go Back   OpenLaszlo Developers Forums > Non-programming help and discussions > Development Tools and Practices

Development Tools and Practices Questions about development tools and practices. An appropriate place to talk about text editors, IDEs, and anything else that makes your development life easier.

 
 
Thread Tools Search this Thread Display Modes
Prev Previous Post   Next Post Next
  #1  
Old 06-16-2008, 11:56 AM
mjessup mjessup is offline
Community Member
 
Join Date: Jun 2008
Location: Pennsylvania, USA
Posts: 135
mjessup is on a distinguished road
Lightbulb Raw character sockets for persistent connection/server push

There seem to be a number of people looking to create a Persistent connection and while the laszlo documentation does contain documentation for persistent connections through the ConnectionManager class the developers guide says this class is "provisional" and "not recommended for deployment".

After some digging I found documentation for the XMLSocket class of the flash runtime, and have been able to create a working example using this as a basis for persistent connections. The documentation for this class is available on the Adobe Website.

The following snippet shows a simple class creating such a socket and registering listeners for events.

Code:
<class name="socket" extends="node">
	<attribute name="xmlSocket" />
	<handler name="oninit">
		//SETUP A SOCKET
		xmlSocket = new XMLSocket();
		//create a closure over 'this'
		var that = this;

		//CALLABCK FOR CONNECTING
		xmlSocket.onConnect = function () {
			that.onConnect.apply(that, arguments);
		};

		//CALLBACK FOR REMOTE DISCONNECT
		xmlSocket.onClose = function () {	
			that.onClose.apply(that, arguments);
		};

		//CALLBACK FOR RECIEVED DATA
		xmlSocket.onData = function () {
			that.onData.apply(that, arguments);
		};

		//SET THE LOCATION FOR THE CONNECTION POLICY
		System.security.loadPolicyFile("xmlsocket://myhost.com:2367");
	</handler>
...
</class>
The "on" fields are references to functions invoked for the appropriate event. In the callback functions we must reference the current object, otherwise when they are invoked, they will not be in the proper context (I am still a little fuzzy on this, and received this solution in the coding help forum). The final line tells the flash runtime an appropriate place to look for a policy file for the connection. This document helps to explain flash security policy files.

Using the socket is a fairly straightforward process using

Code:
xmlSocket.connect("myhost.com", 3678);
to connect to some host at the specified port and

Code:
xmlSocket.send(data);
to send (character) data over the socket and the onData event to read data as it comes over the socket. The attached zip contains three files. sockets.lzx makes the connections and sends and reads data from a socket via a text box. Policy.java is a simple server in java which will handle policy requests. Echo.java is a simple java server that will echo data read from a socket to stdout and write data read from stdin back over the socket. The java files should compile with any javac and can be run as "java FILE PORT" where FILE is either Policy or Echo and PORT is the port to which the server will bind.

Neither of the java examples provide appropriate error handling etc, and are provided as a simple means of showing "Hey, this does work!". That said any robust server can be used for the remote connection.

A couple items of note. First as far as I can tell these sockets can only be used for character data since flash uses the byte 0x00 as a message terminator. Secondly I use the onData event in my processing, but the default is actually to parse the string into a flash XML object and pass this to a different handler "onXML". I don't know if the flash XML object is compatible/the same as the laszlo XML/dataset object but you can always use "LzDataNode.stringToLzData".

In summary this provides a simple mechanism to create a raw socket between flash and some remote server. Since the socket is raw you may use any protocol you see fit, or pass XML and leverage Laszlo's capabilities to manipulate it. This persistent can be used to more easily create stateful applications. Finally this should make it possible to deploy applications SOLO where previously the LPS was necessary for "proxied only" features (such as JavaRPC in my case).

I would be interested in knowing if anyone else has used this or another approach and what type of success/issues have been encountered.
Attached Files
File Type: zip XMLSocket.zip (3.6 KB, 118 views)

Last edited by mjessup; 07-22-2008 at 11:03 AM. Reason: Updated attached code for Laszlo 4.1.1
Reply With Quote
 

Bookmarks

Thread Tools Search this Thread
Search this Thread:

Advanced Search
Display Modes

Posting Rules
You may not post new threads
You may not post replies
You may not post attachments
You may not edit your posts

BB code is On
Smilies are On
[IMG] code is Off
HTML code is Off

Forum Jump


All times are GMT -8. The time now is 04:34 PM.


Powered by vBulletin® Version 3.8.4
Copyright ©2000 - 2014, Jelsoft Enterprises Ltd.