Created
April 19, 2013 21:10
-
-
Save cat-haines/5423264 to your computer and use it in GitHub Desktop.
Electric Imp example code that processes an incoming HTTP Request (through an HttpIn node hooked up to an InputPort) and sends a "response" (which is actually just an HTTP Post to a predefined location).
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
// This is the function that actually handles the incoming HTTP Request | |
function SimpleRequestHandler(data) { | |
server.log("Got a request with value='" + data + "'"); | |
return 200; | |
} | |
// handles incoming HttpIn requests, and sends "responses" to an HttpRequest node through | |
// an output port. The ResponsePort sends a url-encoded message where "value" is the response code. | |
class HttpInPortWithResponse extends InputPort { | |
RequestHandler = null; // function that will be called whenever a request comes into the input port. Should return a status code. | |
ResponsePort = null; // OutputPort that will have it's value set based on RequestHandlers response. | |
constructor(requestHandler, portName, responsePort) { | |
base.constructor(portName); // Set the port name so it's easy to identify in the planner | |
OutputPort = responsePort; | |
RequestHandler = requestHandler; | |
} | |
function set(value) { | |
local responseCode = RequestHandler(value); // Run the request handler | |
OutputPort.set(responseCode); // and return the result | |
server.log("Responded with " + responseCode); | |
} | |
} | |
local httpOutPort = OutputPort("HTTPResponse"); | |
local httpInPort = HttpInPortWithResponse(SimpleRequestHandler, "HTTPRequest", httpOutPort); | |
imp.configure("Http Example", [httpInPort], [httpOutPort]); |
Sign up for free
to join this conversation on GitHub.
Already have an account?
Sign in to comment