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();
}
from native import app
def OnStart():
global web, txt, net
lay = app.CreateLayout("linear")
web = app.CreateWebView(1, 0.5, "ignoreerrors")
lay.AddChild(web)
txt = app.CreateTextEdit("", 1, 0.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)
def net_OnConnect(connected):
if not connected:
return app.ShowPopup("Failed to connect!")
net.SendText("GET / HTTP/1.1\r\nHost:www.randomfunfacts.com\r\n\r\n", "UTF-8")
msg = ""
s = ""
while True:
s = net.ReceiveText("UTF-8")
msg += s
if len(s) == 0:
break
txt.SetText(msg)
web.LoadHtml(msg)
net.Disconnect()
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 = "";
}
}
from native import app
def OnStart():
global net, msg, web, txt
lay = app.CreateLayout("linear")
web = app.CreateWebView(1, 0.5, "ignoreerrors")
lay.AddChild(web)
txt = app.CreateTextEdit("", 1, 0.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")
sent = False
msg = ""
def net_OnConnect(connected):
if not connected:
return app.ShowPopup("Failed to connect!")
if sent:
return sent and msg != ""
else:
sent = True
net.SendText("GET / HTTP/1.1\r\nHost:www.randomfunfacts.com\r\n\r\n", "UTF-8")
def OnReceive(s):
msg += s
if s.endswith("\r\n\r\n"):
txt.SetText(msg)
web.LoadHtml(msg)
msg = ""
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] );
}
}
from native import app
def OnStart():
global net, address, id, port
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
app.SetInterval(CheckForMsg, 500)
def btn_OnTouch():
packet = id + "|Hello"
net.SendDatagram(packet, "UTF-8", address, port)
def CheckForMsg():
packet = net.ReceiveDatagram("UTF-8", port, 10)
if packet:
parts = packet.split("|")
if parts[0] != id:
app.ShowPopup(parts[1])
Properties
The following properties are available on the NetClient object:
Methods
The following methods are available on the NetClient object:
all types
Number
String
Number: bytes
Number: integer
Number: seconds
String: url path
String: separated: “UDP” or
“TCP”,
“Raw”
String: “US-ASCII” or “UTF-8” or “UTF-16LE” or “UTF-16BE” or “UTF-16”
String: “Int” or “Hex”
String: comma “,” separated: “<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:
args }
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.data
An object for saving individual extra properties.
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.ReceiveVideoStreamReceive 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.SetSplitModeTells AutoReceive how to split received data. Splitted data will result in multiple OnReceive calls.
p2 and p3 have different purposes for different modes:
mode | p1 | p2 |
---|
Size | Size of one data package | - |
End | Byte indicating end of data | - |
Start-End | Byte indicating start of data | Byte indicating end of data |
net.SetTimeout
Define an interval in which the client should check for new messages.
net.WakeOnLanWakes 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.