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
UserDatagramP
rotocol 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 = mid + "|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 ]
ReceiveDatagram( mode, port, timeout ) → string
ReceiveFile( file, wait ) → string
ReceiveText( mode ) → 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: “url”
string: “Int” or “Hex”
string: “Text” or “Hex” or “Bytes”
list: [ bytes ]
string: comma “,” separated: bytes
function( connected )
function( something )
function( address )
net.AutoReceive
Receive TCP received data automatically by calling the OnReceive callback.
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.SendBytes
Send bytes over TCP connection.
net.SendDatagram
Send an UDP Datagram.
net.SendText
Sends text over TCP connection.
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.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.