| By Marco Casario | RIAvolutionize the web | Article Rating: |
|
| September 12, 2008 12:32 PM EDT | Reads: |
2,223 |
Today I've posted on Comtaste's blog how to monitor the connection against TCP/IP socket endpoints in AIR using JavaScript. Adobe AIR gives you the ability to use socket connections for push-model connectivity. In this context it can be necessary to have to connect to a specific port of a server. A classic scenario is the one in which the application has to send an email using the Simple Mail Transfer Protocol (SMTP). This text protocol initiates a TCP connection to the server’s port 25.
For a this application is crucial to check and monitor the the connection against TCP/IP socket endpoints.
The AIR service monitor framework can be used for this purposes. The SocketMonitor class, a subclass of the ServiceMonitor, is used to detect connectivity. The following code snippet checks the availability of the connection to a socket server running on localhost over port 25 and, if the trial has been successful, it connects to the socket server:
conn = new Socket();
// Create the istance of the SocketMonitor class
// to monitor port 25 on localhost server
monitor = new SocketMonitor( "localhost", 25 );
conn.addEventListener(Event.CONNECT, onConnect);
monitor.addEventListener(StatusEvent.STATUS, onSocketStatus);
// To start the SocketMonitor class
monitor.start();
The service monitor framework is contained within the external resides in the file servicemonitor.swc. file. In order to be used, must be imported into the the AIR project. While Flex Builder includes this automatically, if you're using the Adobe Flash IDE to create the AIR application, you need to import the ServiceMonitorShim.swc file into the Library of the Flash project in your AIR application package.
This is the complete ActionScript 3 class that check and monitor for the connection against TCP/IP socket endpoints:
package com.oreilly.aircookbook.ch15
{
import air.net.SocketMonitor;
import flash.display.Sprite;
import flash.events.*;
import flash.net.Socket;
import mx.controls.Alert;
public class ConnectSocket extends Sprite
{
private var monitor:SocketMonitor;
private var conn:Socket;
private var _isConnected:String;
public function ConnectSocket()
{
super();
// Create the istance of the Socket class
conn = new Socket();
// Create the istance of the SocketMonitor class
// to monitor port 25 on localhost server
monitor = new SocketMonitor( "localhost", 25 );
conn.addEventListener(Event.CONNECT, onConnect);
monitor.addEventListener(StatusEvent.STATUS, onSocketStatus);
// To start the SocketMonitor class
monitor.start();
}
private function onConnect(event:Event):void
{
trace( "Connection to port 25 established !" );
// You are now ready to send and receive data
}
private function onSocketStatus(e:StatusEvent):void
{
// it returns a Boolean
isConnected = monitor.available.toString();
// it returns Service.available
// isConnected = event.code;
if (monitor.available)
{
conn.connect( "localhost", 25 );
} else {
// If you're using the Flex SDK
//Alert.show( "Connection to port 25 NOT established !" );
// If you're using the Flash IDE
trace( "Connection to port 25 NOT established !" );
}
}
public function get isConnected():String
{
return _isConnected;
}
[Bindable]
public function set isConnected(_isConnected:String):void
{
this._isConnected = _isConnected;
}
}
}
The heart of the class is in the constructor of the ConnectSocket() class, which is a subclass of the Sprite class.
Once the private variable has been declared for the class, we create the instance of the Socket() e SocketMonitor()classes.
For the latter we specify the host and port property:
monitor = new SocketMonitor( "localhost", 25 );
Then we create two event listeners on the events of Event.CONNECT for the Socket class and StatusEvent.STATUS for the SocketMonitor class.
In the event handler of the STATUS event, the available property is checked, and returns a Boolean according to the result obtained by the connection test.
Published September 12, 2008 Reads 2,223
Copyright © 2008 SYS-CON Media, Inc. — All Rights Reserved.
Syndicated stories and blog feeds, all rights reserved by the author.
- Kindle 2 vs Nook
- Cloud Computing on Gartner's Top 10 List and SYS-CON Events' 2010 Calendar
- Confessions of a Ulitzer Addict
- IBM Hardware Chief, Intel VC Exec Arrested in Insider Trading Scam
- Tactical Cloud Computing Panel at 1st Annual GovIT Expo
- Ulitzer.com Named Exclusive "New Media" Sponsor of Cloud Computing Conference & Expo
- Moving Your RIA Apps into the Cloud: Seven Challenges
- Adobe’s Aiming ColdFusion at Multiple Clouds
- Windows 7 – Microsoft’s First Step to the Cloud
- Ulitzer Provides a Powerful Social Journalism Platform
- Jill Tummler Singer, Deputy CIO of CIA, Keynotes at GovIT Expo
- Open Source Mobile Cloud Sync and Push Email
- Kindle 2 vs Nook
- The Difference Between Web Hosting and Cloud Computing
- Cloud Computing on Gartner's Top 10 List and SYS-CON Events' 2010 Calendar
- Ajax in RichFaces 3.3, JSF 2 and RichFaces 4
- Confessions of a Ulitzer Addict
- IBM Hardware Chief, Intel VC Exec Arrested in Insider Trading Scam
- My Thoughts on Ulitzer
- Tactical Cloud Computing Panel at 1st Annual GovIT Expo
- Ulitzer.com Named Exclusive "New Media" Sponsor of Cloud Computing Conference & Expo
- US Post Office Hops a Ride on NetSuite’s Cloud
- Moving Your RIA Apps into the Cloud: Seven Challenges
- Adobe’s Aiming ColdFusion at Multiple Clouds
- Building a Drag-and-Drop Shopping Cart with AJAX
- What Is AJAX?
- Google Maps! AJAX-Style Web Development Using ASP.NET
- Flashback to January 2006: Exclusive SYS-CON.TV Interviews on "OpenAjax Alliance" Announcement
- AJAXWorld Conference & Expo to Take Place October 2-4, 2006, at the Santa Clara Convention Center, California
- AJAX Sponsor Webcasts Are Now Available at AJAXWorld Website
- How and Why AJAX, Not Java, Became the Favored Technology for Rich Internet Applications
- "Real-World AJAX" One-Day Seminar Arrives in Silicon Valley
- AJAXWorld University Announces AJAX Developer Bootcamp
- AJAX Support In JadeLiquid WebRenderer v3.1
- Where Are RIA Technologies Headed in 2008?
- Struts Validations Framework Using AJAX




































