Web Sockets Demo

1,067 views
Skip to first unread message

Dave Smart

unread,
Jun 14, 2015, 5:27:47 AM6/14/15
to androi...@googlegroups.com
Hi Guys,

Attached is a demo of using web sockets to to send messages in both directions from a server to client and the other way around. This technique allows very fast communication and is preferable to using servlets when speed is required.

The demo serves up the client page from the phone for convenience but there is no reason why the client functionality should not be contained in a separate DroidScript App or static web page stored on another device's local storage (as long as that device/browser supports web sockets).

Regards
David
Web Sockets.spk

Dave Smart

unread,
Jun 16, 2015, 3:25:44 AM6/16/15
to androi...@googlegroups.com
A note on reliable network comms: 

I'm working on a small installation at the Cambridge MakeSpace which includes 4 or 5 Android devices communicating with each other while playing videos and controlling servos etc.  I originally used UDP broadcasting to talk between these devices and although I know UDP is an inherently unreliable protocol, it seemed to be performing pretty well until..... I started do lots of things at the same time such as controlling animations and playing videos while listening for UDP messages.

So the solution was to switch to Web Sockets.  Now I run a web server on each device and communicate using Web Sockets which has made a huge difference.  The communications are now very reliable and very fast.

Note: You can use the 'readyState' of a web socket to detect it's current state and re-open it if necessary (eg. App at the other end has been restarted)

if( ws.readyState != 1 ) OpenMySocket();

Also, you send as much data as you like on a Web Socket as it is message based and will automatically collect all the network packets for you and fire the callbacks when complete.. which is nice :)


grimmdev

unread,
Jun 16, 2015, 6:39:37 AM6/16/15
to androi...@googlegroups.com
I've been toying with websockets for droidscript as well but I host my own Dev websocket server that I've been testing it with. I'll look at your demo to see it and I'll upload mine. But mine is just a websocket client while my websocket server is hosted elsewhere. :)

Symbroson Development

unread,
Dec 24, 2016, 8:21:37 AM12/24/16
to DroidScript
Hi Dave,
I was able to test your demo with my and my dads phone (running Android 4.4.2 and Cyanogenmod Android 4.4.4)
I started the demo on one device in DS and typed the address in the browser on the other - but the web address wasnt found. I am sure that the address was right and both devices were connected ti the same wifi. The IP of the phones were the same, just the last number was different (_._._.74 and _._._.59)
So the websocket didn't seem to work.
But if I open the browser on the same device where I opened the WebSocket it worked.
So where was my mistake? I thaught it is possible to commznicate via WebSockets between two devices?
If it didnt work I am not able to send my competition app to you :(
The problem is that the app is already finished and the last problem now is the communication - could please someone help me? I need a working solution.

Symbroson Development

unread,
Dec 24, 2016, 8:24:25 AM12/24/16
to DroidScript
Thnks for fast replies
Best regards,
Symbroson

Dave Smart

unread,
Dec 24, 2016, 1:23:06 PM12/24/16
to DroidScript
Hi Alex,

Please check in Google if Android 4.4.2 supports web sockets.  I'm not sure they were added till 4.4.4

Also check that you specified the correct ports with a colon at the end of the address 8080 not 8088

Also might be worth checking that your router does not block certain ports (but that is unlikely if it is just a common router)

Dave Smart

unread,
Dec 24, 2016, 1:26:05 PM12/24/16
to DroidScript
You could also download our Wifi Jukebox app from Google play and check if it works


If that does not work, then it probably your router at fault.

Symbroson Development

unread,
Dec 24, 2016, 3:22:05 PM12/24/16
to DroidScript
Thanks dave - that may be very helpful
Just one question:
What is the difference between SetOnReceive(callb) and AddServlet(fld,callb)?
Because the Jukebox use the second function to handle requests but I use the first one.
Thank you :)
Reply all
Reply to author
Forward
0 new messages