11. Asynchronous
Java Version web server –Anonymous inner class
public class ServerExample extends Verticle {
public void start() {
vertx.createHttpServer().requestHandler(new Handler<HttpServerRequest>() {
public void handle(HttpServerRequestreq) {
System.out.println("Got request: " + req.uri());
req.response().headers().set("Content-Type", "text/html; charset=UTF-8");
req.response().end("<html><body><h1>Hello from vert.x!</h1></body></html>");
}
}).listen(8080);
}
}
12. Asynchronous
Groovy Version web server-closure
package http
vertx.createHttpServer().requestHandler{ req ->
req.response.end "<html><body><h1>Hello
from vert.x!</h1></body></html>"
}.listen(8080, "localhost")
13. Asynchronous
JavaScript Version web server-function
varvertx = require('vertx')
vertx.createHttpServer().requestHandler(function(r
eq) {
req.response.end("<html><body><h1>Hello from
vert.x!</h1></body></html>");
}).listen(8080);
14. Asynchronous
Java Version web server –Anonymous inner class
public class ServerExampleextends Verticle {
public void start() {
vertx.createHttpServer().requestHandler(new Handler<HttpServerRequest>() {
public void handle(HttpServerRequestreq) {
System.out.println("Got request: " + req.uri());
req.response().headers().set("Content-Type", "text/html; charset=UTF-8");
req.response().end("<html><body><h1>Hello from vert.x!</h1></body></html>");
}
}).listen(8080);
}
}
16. WebSockets (Client)
<script>
var socket;
if (window.WebSocket) {
socket = new WebSocket("ws://localhost:8080/myapp");
socket.onmessage = function(event) {
alert("Received data from websocket: " + event.data);
}
socket.onopen = function(event) {
alert("Web Socket opened!");
};
socket.onclose = function(event) {
alert("Web Socket closed.");
};
} else {
alert("Your browser does not support Websockets. (Use Chrome)");
}
function send(message) {
if (!window.WebSocket) {
return;
}
if (socket.readyState == WebSocket.OPEN) {
socket.send(message);
} else {
alert("The socket is not open.");
}
}
</script>
17. SockJS
• Handles the communication between the
browser and the server.
• Provides a websocket-like API in client-side JS
• Works when websockets not available
• JSON-Polling, XHR-Polling/Streaming, etc
19. SockJS
def server = vertx.createHttpServer()
// Serve the index page
server.requestHandler { req ->
if (req.uri == "/") req.response.sendFile 'sockjs/index.html'
}
// The handler for the SockJS app - we just echo data back
vertx.createSockJSServer(server).installApp(prefix: '/testapp') { sock ->
sock.dataHandler { buff ->
sock << buff
}
}
server.listen(8080)
28. Demonstration
• Web app
• Listen on a topic and send received data to
web through websocket
• A server that searches random topics on
duckduckgo periodically and publish result to
the topic
• Multiple browsers display the data and are
update simultaneously
29. References
• Vertx.io
• Some slides/charts are lifted from the following
presentations
• http://m.javaworld.com/javaworld/jw-072013/130730-osjp-enterprise-messaging-andintegration-with-vertx.html?page=1
• http://www.cubrid.org/blog/devplatform/inside-vertx-comparison-with-nodejs/
• http://www.javacodegeeks.com/2012/07/osgicase-study-modular-vertx.html
Notes de l'éditeur
Explain the code:Request handler handle request eventAsynchronousThe handler accept request and response Response is on request server list port on server
Start the server from parent of sockjs folder, then open http://localhost:8080