| Function Summary | |
TCPConnection
|
newConnection( <String> host, <integer> port )
Creates a new TCP connection handler. |
| Function Detail |
newConnection
TCPConnection newConnection( <String> host, <integer> port )
- Creates a new TCP connection handler.
Example:
The following is a more sophisticated example that uses the TCP API to make a connection to the jabber server at jabber.org,
send a login request, and parse the reply using regular expressions.
Note: Because this Klip contains script that may be harmful to your computer, it will be blocked. To unblock it, select "Allow Blocked Script to Run" while you are in developer mode.
<klip>
<identity>
<title>
API: Engines.TCP
</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 send/receive from a Jabber server to demonstrate the TCP/IP capabilities
// of Klipfolio Dashboard and parsing XML using regular expressions.
//
// Note: To run this script, you must
// - turn on developer mode
// - add <scriptmode>extended</scriptmode> to the Klip's <setup>
//
// <?xml version="1.0" encoding="UTF-8" ?>
// <stream:stream to="jabber.org"
// xmlns="jabber:client"
// xmlns:stream="http://etherx.jabber.org/streams">
//
// It will then receive:
//
// <stream:stream from='jabber.org'
// xmlns='jabber:client'
// id='323EB370765371'
// xmlns:stream='http://etherx.jabber.org/streams'>
//
function onRefresh()
{
var tcp = Engines.TCP.newConnection( "jabber.org", 5222 );
_t( "Opened connection to (" + tcp.host + ", " + tcp.port + ")\r\n" );
if( tcp.open() )
{
var text = "<?xml version=\"1.0\" encoding=\"UTF-8\" ?>" +
"<stream:stream " +
"to=\"jabber.org\" " +
"xmlns=\"jabber:client\" " +
"xmlns:stream=\"http://etherx.jabber.org/streams\">";
tcp_send( tcp, text );
// Look for specific XML in the response
regEx = new RegExp ( "(<stream:stream [^]*?>)", "i" );
read_match = tcp_read_match( tcp, regEx );
parse_jabber_header( read_match );
// Send a close stream command
tcp_send( tcp, "</stream>" );
// Read the last part
while( tcp.isConnected() ) {
Klip.delay( 500 );
_t( "Received: " + tcp.receive( 8192 ));
}
tcp.close();
}
// 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 );
}
function tcp_send( tcp, data )
{
_t( "Sending: " + data + "\r\n" );
tcp.send( data, data.length );
}
function tcp_read_match( tcp, regEx ) {
var data, match;
var received = "";
if( tcp.isConnected() ) {
while ( (data = tcp.receive( 8192 )) != null ) {
if( data.length ) {
received += data;
//
// Do we have a match for our regular expression
//
match = regEx.exec( received );
if( match ) {
_t( "Matched: " + match + "\r\n" );
return match[1];
}
}
}
}
return null;
}
function parse_jabber_header( xml )
{
_t( "parse_jabber_header: " + xml );
//
// Define a style to extract the attributes we want from the XML
//
var style =
" stream:stream::attribute(xmlns) { type: scratch; name: 'xmlns'; } " +
" stream:stream::attribute(from) { type: scratch; name: 'from'; } " +
" stream:stream::attribute(xmlns:stream) { type: scratch; name: 'stream'; } " +
" stream:stream::attribute(id) { type: scratch; name: 'id'; } " +
" stream:stream { type: item; }";
//
// Using the above style, process the given XML. For style elements
// defined as 'type: scratch', the XML parser will extract the parsed
// values and make them available via Engines.Data.getScratch().
//
Engines.Data.stylesheet = style;
Engines.Data.process( xml );
Engines.Data.stylesheet = "@import klip";
_t( "Got xmlns : " + Engines.Data.getScratch( "xmlns" ));
_t( "Got from : " + Engines.Data.getScratch( "from" ));
_t( "Got stream : " + Engines.Data.getScratch( "stream" ));
_t( "Got id : " + Engines.Data.getScratch( "id" ));
}
//
// Trace code
//
function _t( s )
{
trace( s + "\r\n" );
}
]]>
</klipscript>
</klip>

Opened connection to (jabber.org, 5222) Sending: <?xml version="1.0" encoding="UTF-8" ?><stream:stream to="jabber.org" xmlns="jabber:client" xmlns:stream="http://etherx.jabber.org/streams"> Matched: <stream:stream xmlns='jabber:client' xmlns:stream='http://etherx.jabber.org/streams' id='2273410292' from='jabber.org' xml:lang='en'>,<stream:stream xmlns='jabber:client' xmlns:stream='http://etherx.jabber.org/streams' id='2273410292' from='jabber.org' xml:lang='en'> parse_jabber_header: <stream:stream xmlns='jabber:client' xmlns:stream='http://etherx.jabber.org/streams' id='1522625874' from='jabber.org' xml:lang='en'> Got xmlns : jabber:client Got from : jabber.org Got stream : http://etherx.jabber.org/streams Got id : 1522625874 Sending: </stream> Received: <stream:error><xml-not-well-formed xmlns='urn:ietf:params:xml:ns:xmpp- streams'/></stream:error></stream:stream> Received: undefined ... onRefresh()
-
Parameters:
host - either the host or IP address for the remote server (Note that the host name must be without protocol, i.e. 'www.serence.com', not 'http://www.serence.com/')
port - the port (between 1 and 65535) to connect to on the remote server
-
Returns:
-
a new TCPConnection object ready to be opened to a TCP server
|
Klipfolio Dashboard 5 API | ||||||||
| PREV OBJECT NEXT OBJECT | FRAMES NO FRAMES | ||||||||
| SUMMARY: PROPERTY | FUNCTION | DETAIL: PROPERTY | FUNCTION | ||||||||
© 2009 Klipfolio Inc. All Rights Reserved.
