<klip>
   <identity>
      <title>
         API: TCPConnection
      </title>
   </identity>
   <locations>
      <contentsource>
         http://support.klipfolio.com/files/demo/demo.xml
      </contentsource>
      <icon>
         http://www.klipfolio.com/static/klips/klipfolio/sample_icon.png
      </icon>
      <banner>
         http://www.klipfolio.com/static/klips/klipfolio/sample_banner.png   
      </banner>
   </locations>
   <setup>
      <scriptmode>
         extended
      </scriptmode>
   </setup>
   <klipscript>
   <![CDATA[

//
// A simple TCP/IP to connect to the HTTP server at www.serence.com.
//
// Note: To run this script, you must
//  - enable on developer mode
//  - add &lt;scriptmode>extended&lt;/scriptmode> to the Klip's &lt;setup>
//
function onRefresh()
{  
    _t( "onRefresh() ..." );  
    var tcp = Engines.TCP.newConnection( "www.serence.com", 80 );              // 1
    _t( "Attempting to connect to (" + tcp.host + ", " + tcp.port + ")..." );  


    if( tcp.open() )                                                           // 2
    {
        var text = "GET / HTTP/1.0\r\n\r\n";
     
        _t( "Connected!\r\n" );      
        _t( "Sending:\r\n" + text + "\r\n" );
        tcp.send( text, text.length )                                          // 3
      
        while( tcp.isConnected() ) 
        {
            Klip.delay( 500 );
            if( (data = tcp.receive( 8192 )) != null && data.length)           // 4
            {
                _t( "Received:\r\n" + data);
            }
        }
    }
  
    tcp.close();                                                               // 5
    _t( "\r\n\r\n... closed" );
  
    // Put one line message to Klip that the user should check the
    // Debug Window
	var xml = "<xml><item><link></link><description>" +
    		  "(See Debug Window for output)" +
    	      "</description></item></xml>";
    	    
    _t( "... onRefresh()" );  
	return Engines.Data.process( xml );
}

//
// Trace code
//
function _t( s )
{
    trace( s + "\r\n" );
}

   ]]>
   </klipscript>
</klip>

