Skip to content

Instantly share code, notes, and snippets.

@pledbrook
Created May 10, 2012 12:52
Show Gist options
  • Star 10 You must be signed in to star a gist
  • Fork 3 You must be signed in to fork a gist
  • Save pledbrook/2652835 to your computer and use it in GitHub Desktop.
Save pledbrook/2652835 to your computer and use it in GitHub Desktop.
Embed Vert.x in Grails
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 = {
}
}
<!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>
lib/vert.x-core.jar
lib/vert.x-platform.jar
lib/netty.jar
lib/jackson-core.jar
lib/jackson-mapper.jar
@shyamramineni
Copy link

Will this work without violationg cross domain policy? http://grails.1312388.n4.nabble.com/TCP-Socket-Server-td2285321.html

@shyamramineni
Copy link

Will this work without violationg cross domain policy? http://grails.1312388.n4.nabble.com/TCP-Socket-Server-td2285321.html

Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment