Adobe AIR gives you the ability to use socket connections for push-model connectivity. 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.
In JavaScript, the operations to check and monitor the connection against TCP/IP socket endpoints use the SocketMonitor that is a subclass of the ServiceMonitor class.
The service monitor framework is external to the standard AIR APIs so that the SocketMonitor can be instanced only after having imported the service monitor framework.
In order to import the service framework, the servicemonitor.swf file needs to be imported with a script tag in the page:
<!-- Include service monitor framework -->
<script src="frameworks/servicemonitor.swf" type="application/x-shockwave-flash" />
The following code uses the SocketMonitor to test and monitor if the network state changes against a given socket endpoint using HTML and JavaScript. If the test has a positive outcome we carry out the connection to the socket server with the nsIServerSocket interface of Mozilla.
The nsIServerSocket interface provides a way to connect to a server socket. It is implemented by this component: @mozilla.org/network/server-socket;1.
This interface is intended to be used as an instance using the following JavaScript syntax:
var myIstance = Components.classes["@mozilla.org/network/server-socket;1"].createInstance(Components.interfaces.nsIServerSocket);
In this example I will use this component and its methods to create a socket server connection.
This is the full HTML file with the service monitor APIs invoked by JavaScript:
<!DOCTYPE html PUBLIC "-//W3C//DTD XHTML 1.0 Transitional//EN" "http://www.w3.org/TR/xhtml1/DTD/xhtml1-transitional.dtd">
<html xmlns="http://www.w3.org/1999/xhtml">
<head>
<!-- Include service monitor framework -->
<script src="frameworks/servicemonitor.swf" type="application/x-shockwave-flash" />
<script type="text/javascript" src="frameworks/AIRAliases.js" />
<script type="text/javascript" src="frameworks/AIRIntrospector.js" />
<script type="text/javascript">
var xmlhttp = newXMLHttpRequest();
var socketConn = null;
var checkSocket = null;
var host = 'localhost';
var port = 25;
function checkConn()
{
checkSocket = new air.SocketMonitor( host, port );
checkSocket.addEventListener(air.StatusEvent.STATUS, announceStatus);
checkSocket.start();
}
function announceStatus(e)
{
document.getElementById("resultDiv").innerText = HTTPMonitor.available;
if (HTTPMonitor.available)
{
socketConn = Components.classes["@mozilla.org/network/server-socket;1"]
.createInstance(Components.interfaces.nsIServerSocket);
socketConn.init(25,false,-1);
socketConn.asyncListen(listener);
} else {
document.getElementById("resultDiv").innerText = 'Can’t connect to the rss feed. The connection is unavailable';
}
}
var listener =
{
onSocketAccepted : function(socketConn, transport)
{
var stream = transport.openOutputStream(0,0,0);
stream.write("OK",2);
stream.close();
},
onStopListening : function(socketConn, status){}
};
</script>
<meta http-equiv="Content-Type" content="text/html; charset=utf-8" />
<title>Monitoring the Connection to a Specific Port on a Server (JavaScript)</title>
</head>
<body onload="checkConn()">
<div id="resultDiv">Checking .... </div>
</body>
</html>
The SocketMonitor class accepts two parameters in the constructor, the host and the port.
These two parameters are declared in the global variables in the script block and then used in the checkConn() function, invoked on the the onload event of the body.
The announceStatus event listener is created, and inside it we check the availability of the socket server. If the result is positive, and therefore the available property returns true, the nsIServerSocket instance of the interface is created with the createInstance()method.
To launch the server socket connection we use the init() method to initiate the connection and we specify the following three topics:
port: specifies the port to listen to
loopbackOnly: indicates whether connections are allowed from any machine or just the same machine
backLog: specifies the length of the queue of incoming connections
Finally, a listener object is passed to the asyncListen method to start the socket listening for connections.
Comments (2)
I think this is wat i m lookin for. I intend to write a LAN based chat client in AIR using HTML + javascript. i knw nothin of flash/actionscript coding.
how do i create a server on each chat client that can listen to incomin socket connections? there wont be a centralised server. any chat client(the one i m makin) on the LAN can chat wid the other client. How do i go about this? in java we have serversocket to create server. In air...?
Posted by kamikaze | November 8, 2009 9:02 AM
Posted on November 8, 2009 09:02
when i add this code i have error
ActionScript error: ReferenceError: Can't find variable: HTTPMonitor
why ?
Posted by grek | December 22, 2009 1:28 PM
Posted on December 22, 2009 13:28