Created
January 25, 2016 08:39
-
-
Save vietj/51ff223bfb7cfcbc97ce to your computer and use it in GitHub Desktop.
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
var ShellService = require("vertx-shell-js/shell_service"); | |
var CommandBuilder = require("vertx-shell-js/command_builder"); | |
var CommandRegistry = require("vertx-shell-js/command_registry"); | |
var service = ShellService.create(vertx, { | |
"telnetOptions" : { | |
"host" : "localhost", | |
"port" : 5000 | |
} | |
}); | |
service.start(); | |
var client = vertx.createHttpClient(); | |
var builder = CommandBuilder.command("http-client"); | |
builder.processHandler(function (process) { | |
// Check the url argument | |
if (process.args().length < 1) { | |
process.write("Missing URL\n").end(); | |
return; | |
} | |
var url = process.args()[0]; | |
// Create the client request | |
var request = client.getAbs(url, function(response) { | |
// Print the response in the shell console | |
response.handler(function(buffer) { | |
process.write(buffer.toString("UTF-8")); | |
}); | |
// End the command when the response ends | |
response.endHandler(function() { | |
process.end(); | |
}); | |
}); | |
// Set a request handler to end the command with error | |
request.exceptionHandler(function(err) { | |
process.write("Error: " + err.getMessage()); | |
process.end(); | |
}); | |
// End the http request | |
request.end(); | |
}); | |
// Register the command | |
var registry = CommandRegistry.getShared(vertx); | |
registry.registerCommand(builder.build(vertx)); | |
Sign up for free
to join this conversation on GitHub.
Already have an account?
Sign in to comment