Hi Folks, working to build a robust system to auto-reconnect in the event of socket drop. I'm taking advantage of app.SetOnError, ws.onclose, and ws.onerror to re-open the socket. The behavior I've witnessed however, is that the server code tends to remember the prior socket connections even though the client code believes to have closed or errored out. So what ends up happening, is if you send message using the button, you get response times connections the server believes are open. My question is, how would you mitigate the server duplicating the connections? I suppose somehow I'd need to regularly scan the clients array and remove duplicates?
Client Code=====================
var socket = '';
function OnStart()
{
//Open web socket to phone.
app.SetOnError( ws_onerror );
ws_opensocket();
}
function ws_opensocket()
{
if ( socket == "error" | socket == "closed" ) {
console.log( "Recovering from " + socket + " socket after 20 seconds" );
sleep ( 20000 );
}
ws.onopen = ws_onopen;
ws.onmessage = ws_onmessage;
ws.onclose = ws_onclose;
ws.onerror = ws_onerror;
}
//Handle socket open.
function ws_onopen()
{
console.log( "Socket Open" );
socket = "open";
}
//Handle messages from phone.
function ws_onmessage( msg )
{
console.log( msg.data );
if ( msg.data == "armstate" ) console.log( "run armstate" );
}
//Other websocket callbacks.
function ws_onclose() {
console.log( "Socket Closed" );
socket = "closed";
ws_opensocket();
}
function ws_onerror(e) {
console.log( "Socket Error: " + e.data );
socket = "error";
ws_opensocket();
}
function sleep( time )
{
var now = new Date().getTime();
console.log( "Sleeping: " + ( time / 1000 ) + "sec");
while( new Date().getTime() < now + time ) {}
}
end client code==============
server code================
//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/DroidScript" );
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 + "\n";
//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() )
}
end server code============