Re: [Sample App] Using Web Sockets

898 views
Skip to first unread message

Chris Larcombe

unread,
Oct 13, 2014, 5:56:06 AM10/13/14
to androi...@googlegroups.com
Thank you Dave. This works for me! I am able to get multiple clients to communicate through the sockets (client to client) and also send messages from the server (phone) to clients. 

However, I am not able to get the server to receive any messages. Is there a function for receiving messages (text) yet?

I did try the following:

    socket = new WebSocket("ws://192.168.1.111:8080");
    socket.onmessage = function(msg){alert(msg)}

But as before it doesn't seem to work (doesn't seem to connect).

Perhaps there could be/is a GetText or ReceiveText function in serve (or OnMessage)?

Thank you for creating and posting this; it's particularly brilliant that you have found a way to serve the websockets from the app (so no need for 3rd party socket server)! :D

Chris Larcombe

unread,
Oct 15, 2014, 12:37:22 PM10/15/14
to androi...@googlegroups.com
Hmm, still not able to receive socket messages within Droidscript (or establish websocket connection).

When I do socket = new WebSocket(".....") (with appropriate IP and port, after the socket server is definitely up)

The socket seems to stay in 'connecting' phase:

socket.readyState seems to equal 0

(0 means socket.CONNECTING)

Dave Smart

unread,
Oct 18, 2014, 5:31:22 AM10/18/14
to
Hi Chris,

You can't currently receive socket messages inside DroidScript (remember this functionality is still a work in progress).  You can however send messages into your App using a 'Servlet' as shown in the samples. 

There is a bug in the sample html page which can be fixed by changing the following line:-

serv.AddServlet( "/control", OnServlet );

to

serv.AddServlet( "/message", OnServlet );

Regards
David 

Chris Larcombe

unread,
Oct 19, 2014, 11:22:48 AM10/19/14
to androi...@googlegroups.com
Hi David,

Thank you, the servlet works for me. Also, I am able to use Socket.io (but it is using GET and POST rather than Websockets).

Also:

I can confirm that websockets do work within Droidscript on Android 4.4 (Kitkat) - this appears to be due to a new version of WebView built into the OS.

So actually it appears you can receive socket messages inside DroidScript :) as long as you have KitKat. Yay!

Dave Smart

unread,
Oct 21, 2014, 4:19:17 AM10/21/14
to androi...@googlegroups.com
Hi Chris,

What I meant was we have not yet added a callback facility to the server so that you get notified when your web clients send the server socket messages.

But I'm glad you found a way of doing what you want :)

Dave Smart

unread,
Oct 13, 2014, 5:09:02 AM10/13/14
to androi...@googlegroups.com
[Edit: This post is now out of date, please see the following post instead: https://groups.google.com/d/msg/androidscript/t1QzCgGZrH8/0S8xYbByjNsJ ]

Hi Guys,

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).

Note: Currently if you send messages from a web client, they will be reflected to all connected clients (this will probably be optional rather than the default in the next release)


//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() )
}

SocketTest.html
Reply all
Reply to author
Forward
0 new messages