[Code Snippet] Reliable WebSocket communications

654 views
Skip to first unread message

Dave Smart

unread,
Feb 8, 2016, 3:55:53 PM2/8/16
to DroidScript
Here is a little JavaScript object that I am using to maintain reliable (always connected) WebSocket links between multiple Android devices. This code is very useful if you don't know the order in which devices are going to be turned on or if some devices temporarily go missing from the network (it tries to reconnect to remote devices every 7 seconds).

It's designed to connect to a remote device running DroidScript's built-in HTTP server and you can put it in a file called WebSock.js and include into your main App using this line of code:- 

app.LoadScript( "WebSock.js" );

You use it like this:-

function OnStart() 
{
 :
  wsTV = new WebSock( "TV", "192.168.0.3" );
  wsTV.SetOnMessage( ws_onmessage );
}

function ws_onmessage( msg ) 
{
  console.log( msg.data );
}

function btn_OnTouchDown()
{
  sock.Send( "Hello" );
}



Here is the JavaScript object code:-

function WebSock( id, ip, port, options )
{
    var m_OnMessage = null;
    var m_sock = null;
    var m_timer = null;
    
    console.log( "Opening web socket:" + id );
    if( !port ) port = 8080;
m_sock = new WebSocket( "ws://"+ip+":"+port );
m_sock.onopen = OnOpen;
m_sock.onmessage = OnMessage;
m_sock.onclose = OnClose;
m_sock.onerror = OnError;
m_timer = setInterval( CheckSocket, 7000 ); 
    
    function OnOpen() { 
        console.log( "Socket Open: "+id ); 
    }
    
    function CheckSocket() {  
        if( m_sock.readyState != 1 ) {
            console.log( "Re-opening web socket:" + id );
            m_sock = new WebSocket( "ws://"+ip+":"+port );
        }
    }
    
    function OnClose() { console.log( "Socket Closed: "+id ); }
    function OnError(e) { console.log( "Socket Error: "+e.data ); }
    function OnMessage( msg ) { if( m_OnMessage ) m_OnMessage( msg.data ); }
    
    this.GetSocket = function() { return m_sock; }
    this.SetOnMessage = function( callback ) { m_OnMessage = callback; }
    
    this.Send = function( msg ) {
        if( m_sock.readyState != 1 ) console.log( "Socket not ready:"+m_sock ); 
        else m_sock.send( msg );
    }
}




Message has been deleted

Eric Geer

unread,
Sep 16, 2016, 9:01:03 PM9/16/16
to DroidScript
Hi Dave,

I'm extremely motivated to get this functional.  I'm intending on using a Raspberry Pi as the home WebSocks server.  I need to wrap my head around this setup though as I'm having trouble working through the steps.  Once I get it all figured out, I'd be happy to do a writeup on it.  If you can give me some guidance on the above scripts:

1. "WebSock.js" is the name of the second script or is this the script called from the remote websocket server?

2. I'm having some issues separating what is supposed to be DroidScript code and what is supposed to be the WebSocket server script code.  I assume the server code still needs to be written?  For example, I see no other reference to button "OnTouchDown" so this is the button you place onto your remote server code?
Message has been deleted

Robbie Kendall

unread,
Sep 16, 2016, 9:46:43 PM9/16/16
to DroidScript
Eric I believe the only line you need to add to your DroidScript code is this
app.LoadScript( "WebSock.js" );

The rest of the code Dave posted should be saved in a separate javascript file named WebSock.js inside your project folder.

If this isn't what you were asking I apologize

Dave Smart

unread,
Sep 17, 2016, 1:40:15 PM9/17/16
to DroidScript
The above article is a little out of date now.

You should be able to create a websocket with the app.CreateWebSocket() method as it is now built into DS

Eric Geer

unread,
Dec 8, 2016, 2:45:18 AM12/8/16
to DroidScript
I'm very excited to get websockets working between my Nexus 7 and my Leelbox!  Since my intentions are to use this as a security appliance, is there support for running the servlet in secure sockets?  I want to be able to connect using 'wss:server' so that it's encrypted and no one can reverse-engineer and the packets.  Ideally, I'd like it to ignore certs so as not to have to deal with that.  I'm also looking for a way to send challenged response or perhaps embed a UID in the messages to prevent other hosts from attempting to connect and send false messages.

Dave Smart

unread,
Dec 8, 2016, 11:06:08 AM12/8/16
to DroidScript
Hi Eric,

Secure sockets would be possible in theory with the Jetty web server that we use inside DS (using self signed certificates), but we have not implemented the necessary functions for this yet.

An alternative is to use the DS encryption methods to encrypt your websocket data using a password only known to your server and client. That way you would be protected from a 'man in the middle' attack if you were using public wifi etc.  However if someone got hold of your client source code then they would be able to see your password in the source code, so you need to obfuscate the storage of that in some way.  

You could reduce the chances of hacking further by using a 'white list' of mac addresses on the server (but be aware that mac addresses can be spoofed).

Another way to improve security would be to have an algorithm on the server which cycles access tokens according to a certain pattern/formula only known to you (in your head).  The server would reject any messages that did not contain that correct token.

Also, public/private key encryption is an option.

Regards
David

Emma Nevada

unread,
May 3, 2017, 2:47:00 PM5/3/17
to DroidScript
i have a script in nodejs i ported for RSA ENCRYPTION

Emma Nevada

unread,
May 3, 2017, 4:14:05 PM5/3/17
to DroidScript
how would i load in a script the acme location as my main app folder?

Austin Spor

unread,
Apr 30, 2020, 12:19:00 PM4/30/20
to DroidScript
hey Dave, will the WebSocks code work with the TCP server?
look forward to your response.
Austin
 

Dave

unread,
May 1, 2020, 9:39:08 AM5/1/20
to androi...@googlegroups.com
No, you will need to use app.CreateWebServer() to serv websockets
Reply all
Reply to author
Forward
0 new messages