Created
October 28, 2014 16:40
-
-
Save electricimp/41148b5620d62b43c03b to your computer and use it in GitHub Desktop.
Unexpected Server Disconnection Recipe
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
// 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