This is an example of how to send messages from a web server running on your phone to any number of connected clients via WebSockets.
(Please bear in mind that the WebSocket functionality is still under construction)
The DroidScript App is shown here and a test web page is attached (you will need to change the IP address in the web page).
//Called when application is started.
function OnStart()
{
//Check wifi is enabled.
ip = app.GetIPAddress();
if( ip == "0.0.0.0" ) {
app.ShowPopup( "Please Enable Wi-Fi" );
app.Exit();
}
//Prevent wifi from powering down.
app.PreventWifiSleep();
//Create a layout with objects vertically centered.
lay = app.CreateLayout( "linear", "VCenter,FillXY" );
//Create a text label and add it to layout.
txt = app.CreateText( ip, 0.8, 0.3, "AutoScale,MultiLine" );
txt.SetTextSize( 22 );
lay.AddChild( txt );
//Create a 'Send Message' button.
btn = app.CreateButton( "Send Message", 0.4, 0.1 );
btn.SetMargins( 0, 0.05, 0, 0 );
btn.SetOnTouch( SendMessage );
lay.AddChild( btn );
//Add layout to app.
app.AddLayout( lay );
//Create and run web server on port 8080.
serv = app.CreateWebServer( 8080 );
serv.SetFolder( "/sdcard/AndroidScript" );
serv.Start();
//Start timer to show WebSock connections.
setInterval( ShowConnections, 3000 );
}
//Show who is connected.
function ShowConnections()
{
var clients = serv.GetWebSockClients();
if( clients.length > 0 )
{
//Make a list of clients.
var list = "";
for( var i=0; i<clients.length; i++ )
list += clients[i].remoteAddress;
//Show client list.
txt.SetText( list );
}
}
//Send a message to all connected socket clients.
function SendMessage()
{
//Note: You can send to a specific client by passing
//the IP address as the second parameter.
serv.SendText( "Hello " + (new Date()).toLocaleString() )
}