Skip to content

Instantly share code, notes, and snippets.

@electricimp
Created October 28, 2014 16:40
Show Gist options
  • Save electricimp/41148b5620d62b43c03b to your computer and use it in GitHub Desktop.
Save electricimp/41148b5620d62b43c03b to your computer and use it in GitHub Desktop.
Unexpected Server Disconnection Recipe
// Set the disconnection policy
server.setsendtimeoutpolicy(RETURN_ON_ERROR, WAIT_TIL_SENT, 30)
// Define the disconnection handler function
function disconnectionHandler(reason)
{
if (reason != SERVER_CONNECTED)
{
// Attempt to reconnect in 10 minutes' time
imp.wakeup(600, reconnect)
}
else
{
server.log("Device reconncted")
}
}
// Define the reconnection function
function reconnect()
{
// Attempt to reconnect
// server.connect calls disconnectHandler() on success or failure
// with an appropriate reason parameter
server.connect(disconnectionHandler, 60)
}
// Register the unexpected disconnect handler
server.onunexpecteddisconnect(disconnectionHandler)
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment