Adobe Integrated Runtime (AIR) for JavaScript Developers Pocket Reference

(nextflipdebug5) #1

132 | Chapter 4: AIR Mini-Cookbook


// Now we send the bytes to the service and
// clear the buffer.
socket.flush( );
}

This example provides a baseline of functionality that can be
expanded upon to speak to many different protocols. The
only current limitation is that there is not currently an SSL
Socket implementation in AIR. For secure communication
you will be limited to HTTPS:


<html>
<head>

<title>Communicating on a Socket</title>
<script type="text/javascript" src="AIRAliases.js">
</script>

<script>
var socket = null;

function init( )
{
socket = new air.Socket( );

// Create our listeners which tell us when the Socket
// is open and when we receive data from our service.
socket.addEventListener( air.Event.CONNECT, onSocketOpen );
socket.addEventListener( air.ProgressEvent.SOCKET_DATA,
onSocketData );

// Connect to our service, which is located at host foo.com
// using port 5555.
socket.connect( 'foo.com', 5555 );
}

function onSocketOpen( event )
{
// This queues up the binary representation of the
// string 'Bob' in UTF-8 format to be sent to the
// endpoint.
socket.writeUTFBytes( "Bob" );

// Send the actual bytes to the server and clear
// the stream. We then wait for data to be sent
Free download pdf