Klipfolio Dashboard 5 API

Object TCPConnection

Object
   |
   +--TCPConnection

The TCPConnection object provides an interface for TCP based communication. Use Engines.TCP.newConnection() to create a TCPConnection object.

Overview

The steps in making a successful TCP send/receive session are as follows
  1. Create a TCPConnection object using Engines.TCP.newConnection().
  2. Open a connecting using TCPConnection.open().
  3. Send data using TCPConnection.send().
  4. Read incoming data using TCPConnection.receive().
  5. Call TCPConnection.close() to close the connection.

Klip Security Model

Using the API calls below requires running with developer mode enabled and having the XML <scriptmode>extended</scriptmode> within the Klip's <setup> block, as shown below.

   <setup>
     <scriptmode>
       extended
     </scriptmode>
   </setup>
 
In developer mode, select "Allow Blocked Script to Run" from the Klip's Developer Tools menu.

In order to use this object without the Developer Mode turned on, you must have your Klip digitally signed by Klipfolio Inc.

Example

The following Klip uses Klipfolio Dashboard's TCP API to connect to www.serence.com at port 80 and make a request to the web server for the index page.

<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 <scriptmode>extended</scriptmode> to the Klip's <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>

     	
Upon loading, the Klip will display the following trace output to Klip's Debug Window.

 onRefresh() ...
 Attempting to connect to (www.serence.com, 80)...
 Connected!
 
 Sending:
 GET / HTTP/1.0
 
 
 
 Received:
 HTTP/1.1 302 Moved Temporarily
 Server: Zeus/4.3
 Date: Tue, 05 Aug 2008 17:05:52 GMT
 Connection: close
 Location: http://www.klipfarm.com/farm.php
 X-Powered-By: PHP/4.3.10-20041216
 Content-Type: text/html
 Expires: Wed, 06 Aug 2008 17:05:52 GMT
 Cache-Control: max-age=86400
 
 
 
 
 ... closed
 ... onRefresh()
 
This Klip demonstrates a single request/response; a more real-world TCP/IP Klips would do a set of send/receives of commands. TCPConnection objects can be reused with or without changes simply by calling the open() function again.


 
Function Summary
 boolean close()
           Close the TCP connection.
 boolean isConnected()
           Tests whether the TCP connection is still connected.
 boolean open( <String> host, <integer> port )
           Open the TCP connection.
 String receive( [<integer> length] )
           Receives data over an open TCP connection.
 boolean send( <String> data, <integer> length )
           Sends data over an open TCP connection.

Function Detail

close

boolean close()

isConnected

boolean isConnected()

open

boolean open( <String> host, <integer> port )

receive

String receive( [<integer> length] )

send

boolean send( <String> data, <integer> length )

Klipfolio Dashboard 5 API

© 2009 Klipfolio Inc. All Rights Reserved.