Back

CreateNetClient

Hello World
Content:
- Methods

NetClients can be used to communicate with servers on the web.

net = app.CreateNetClient( type ) → app object: NetClient

You can choose between two different protocols:
The connection based TransmissionControlProtocol which always checks if the data was received correctly and in right order. It is used in most cases because it is very reliable. The downside is that it is relatively slow becaus of the numerous checks.
The connectionless UserDatagramProtocol which sends the data once without any checks so that packages may be corrupt or lost completely during the transmission. Because of that data can be sent as fast as possible and it suits perfectly for games which need a fast update rate between the devices.


Note: A few routers block fast UDP messages by default

Example - TCP Basic

function OnStart()
{
    lay = app.CreateLayout( "linear" );

    web = app.CreateWebView( 1, .5, "ignoreerrors" );
    lay.AddChild( web );

    txt = app.CreateTextEdit( "", 1, .5, "ReadOnly,NoKeyboard" );
    txt.SetTextSize( 12 );
    lay.AddChild( txt );

    app.AddLayout( lay );

    net = app.CreateNetClient( "TCP,Raw" );
    net.SetOnConnect( net_OnConnect );
    net.Connect( "www.randomfunfacts.com", 80 );

}

function net_OnConnect( connected )
{
    if( !connected ) return app.ShowPopup( "Failed to connect!" );

    net.SendText( "GET / HTTP/1.1\r\nHost:www.randomfunfacts.com\r\n\r\n", "UTF-8" );

    var msg = "", s = "";
    do msg += s = net.ReceiveText( "UTF-8" );
    while( s.length > 0 );

    txt.SetText( msg );
    web.LoadHtml( msg );

    net.Disconnect();
}
    Copy     Copy All       Run      

Example - TCP AutoReceive

function OnStart()
{
    lay = app.CreateLayout( "linear" );

    web = app.CreateWebView( 1, .5, "ignoreerrors" );
    lay.AddChild( web );

    txt = app.CreateTextEdit( "", 1, .5, "ReadOnly,NoKeyboard" );
    txt.SetTextSize( 12 );
    lay.AddChild( txt );

    app.AddLayout( lay );

    net = app.CreateNetClient( "TCP,Raw" );
    net.SetOnConnect( net_OnConnect );
    net.SetOnReceive( OnReceive );
    net.AutoReceive( "www.randomfunfacts.com", 80, "UTF-8" );

}

var sent = false;
function net_OnConnect( connected )
{
    if( !connected ) return app.ShowPopup( "Failed to connect!" );

    if( sent ) return sent = msg != "";
    else sent = true;

    net.SendText( "GET / HTTP/1.1\r\nHost:www.randomfunfacts.com\r\n\r\n", "UTF-8" );
}

var msg = "";
function OnReceive( s )
{
    msg += s;

    if(s.endsWith( "\r\n\r\n" ))
    {
        txt.SetText( msg );
        web.LoadHtml( msg );
        msg = "";
    }
}
    Copy     Copy All       Run      

Example - UDP Messaging

function OnStart()
{
    lay = app.CreateLayout( "linear", "VCenter,FillXY" );

    btn = app.CreateButton( "Send", 0.3, 0.1 );
    btn.SetMargins( 0, 0.05, 0, 0 );
    lay.AddChild( btn );
    btn.SetOnTouch( btn_OnTouch );

    app.AddLayout( lay );

    net = app.CreateNetClient( "UDP" );

    address = net.GetBroadcastAddress();
    id = app.GetDeviceId();
    port = 19700;

    setInterval( CheckForMsg, 500 );
}

function btn_OnTouch()
{
    var packet = id + "|Hello";
    net.SendDatagram( packet, "UTF-8", address, port );
}

function CheckForMsg()
{
    var packet = net.ReceiveDatagram( "UTF-8", port, 10 );
    if( packet )
    {
        var parts = packet.split( "|" );

        if( parts[0] != id )
            app.ShowPopup( parts[1] );
    }
}
Copy All       Run      

Methods

The following methods are available on the NetClient object:

Close()
GetBroadcastAddress() → String
GetType() → String: “NetClient”
IsEnabled() → Boolean
ReceiveBytes( mode ) → List: [ bytes ]
ReceiveFile( file, wait ) → String
ReceiveText( mode ) → String
ReceiveVideoStream( port, img ) → String
Number
String
Number: bytes
Number: integer
Number: seconds
String: url path
String: UDP or TCP, Raw
String: “US-ASCII” or “UTF-8” or “UTF-16LE” or “UTF-16BE” or “UTF-16”
String: “Int” or “Hex”
String: “<BUFSIZ>”
String: “Text” or “Hex” or “Bytes”
String: Hex or Int or Text or “<encoding>”
String: “End” or “Start-End” or “Size”
List: [ bytes ]
String: comma “,” separated: bytes
String
Number: integer
Object: { COMMAND }
app object: Image
function( connected )
function( something )
function( address )
net.AutoReceive
Receive TCP received data automatically by calling the OnReceive callback.
net.Batch
Batch method calls to be able to set all object's properties at once.
Note that you need to specify each parameter (use “” or null to leave some out)
Inherited methods can be called by appending an underscore to the function name (ie. txt.Batch({ SetBackColor_: [“red”] })
net.Close
Closes the NetClient socket.
net.Connect
Connect the NetClient to a server.
net.Disconnect
Disconnect the NetClient from the server.
net.DownloadFile
Downloads a file via TCP from the server.
net.GetBroadcastAddress
Returns the broadcast address of UDP connections.
net.GetType
Returns the control class name.
net.IsConnected
Checks if the NetClient is connected to a server.
net.IsEnabled
Returns whether the control is currently useable by the user.
net.ReceiveBytes
Receive data as bytes.
net.ReceiveDatagram
Receive an UDP Datagram.
net.ReceiveDatagrams
Receive datagrams over UDP and calls the OnReceive callback for each one.
net.ReceiveFile
Receive a file via TCP from the server.
net.ReceiveText
Receive text from TCP connection.
net.ReceiveVideoStream
Receive video from TCP connection.

Note: This function is a premium feature. Please consider subscribing to Premium to use this feature and support DroidScript in its further development.
net.SendBytes
Send bytes over TCP connection.
net.SendDatagram
Send an UDP Datagram.
net.SendText
Sends text over TCP connection.
net.SetDataMode
Enable sending data in several modes.
net.SetOnConnect
Define a callback function which is called when a TCP connection could be established or if it failed to connect to the server. The connected state is passed as first argument.
net.SetOnDownload
Define a callback function which is called when a TCP file download has finished.
net.SetOnReceive
Define a callback function which is called when a TCP NetClient received some data when AutoReceive was set.
net.SetSplitMode
Tells AutoReceive how to split received data. Splitted data will result in multiple OnReceive calls.
p2 and p3 have different purposes for different modes:
modep1p2
SizeSize of one data package-
EndByte indicating end of data-
Start-EndByte indicating start of dataByte indicating end of data
net.SetTimeout
Define an interval in which the client should check for new messages.
net.WakeOnLan
Wakes up PC's (and perhaps other devices) when the BIOS/device is configured for it.

Note: This function is a premium feature. Please consider subscribing to Premium to use this feature and support DroidScript in its further development.