public
Last active

Embed Vert.x in Grails

  • Download Gist
BootStrap.groovy
Groovy
1 2 3 4 5 6 7 8 9 10 11 12 13 14 15 16 17 18 19
import org.vertx.groovy.core.Vertx
 
class BootStrap {
 
def init = { servletContext ->
def vertx = Vertx.newVertx()
def httpServer = vertx.createHttpServer()
vertx.createSockJSServer(httpServer).installApp(prefix: '/events') { sock ->
sock.dataHandler { buff ->
sock << buff
}
}
 
httpServer.listen(8585)
}
 
def destroy = {
}
}
JARs
1 2 3 4 5
lib/vert.x-core.jar
lib/vert.x-platform.jar
lib/netty.jar
lib/jackson-core.jar
lib/jackson-mapper.jar
index.gsp
Groovy Server Pages
1 2 3 4 5 6 7 8 9 10 11 12 13 14 15 16 17 18 19 20 21 22 23 24 25 26 27 28 29 30 31 32 33 34 35 36 37
<!doctype html>
<html>
<head>
<title>SockJS Test</title>
<script src="http://cdn.sockjs.org/sockjs-0.2.1.min.js"></script>
</head>
<body>
 
<script>
var sock = new SockJS('http://localhost:8585/events');
sock.onopen = function() {
console.log('open');
};
sock.onmessage = function(e) {
console.log('message', e.data);
alert('received message echoed from server: ' + e.data);
};
sock.onclose = function() {
console.log('close');
};
 
function send(message) {
 
if (sock.readyState == WebSocket.OPEN) {
console.log("sending message")
sock.send(message);
} else {
console.log("The socket is not open.");
}
}
</script>
<form onsubmit="return false;">
<input type="text" name="message" value="Hello, World!"/>
<input type="button" value="Send SockJS data" onclick="send(this.form.message.value)"/>
</form>
</body>
</html>

Please sign in to comment on this gist.

Something went wrong with that request. Please try again.