Browse files

added EventBusBridgeListener example in javascript

  • Loading branch information...
1 parent 3eb880b commit 753e58b5a3e40b877601141827f69764d0875848 @Narigo Narigo committed Aug 11, 2012
View
45 vertx-examples/src/main/javascript/eventbusbridgelistener/bridge_server.js
@@ -0,0 +1,45 @@
+load('vertx.js')
+
+var server = vertx.createHttpServer()
+var sockjsServer;
+
+var log = [];
+
+// Serve the static resources
+server.requestHandler(function(req) {
+ if (req.uri == "/") req.response.sendFile("eventbusbridgelistener/index.html")
+ if (req.uri == "/log.json") req.response.end(JSON.stringify(log));
+ if (req.uri == "/vertxbus.js") req.response.sendFile("eventbusbridgelistener/vertxbus.js")
+});
+
+
+var myListener = {
+ sendingMessage: function(clientId, address, message) {
+ log.push(clientId + ' sent ' + message.encode() + ' to ' + address);
+ return true; // let the client send the message
+ },
+ publishingMessage: function(clientId, address, message) {
+ log.push(clientId + ' tried to publish ' + message.encode() + ' to ' + address);
+ return false; // don't let any publish methods through
+ },
+ registeringHandler: function(clientId, address) {
+ if (address === 'secret') {
+ log.push('did not allow ' + clientId + ' registering at ' + address);
+ return false;
+ } else {
+ log.push(clientId + ' registered at ' + address);
+ return true;
+ }
+ },
+ unregisteredHandler: function(clientId, address) {
+ log.push(clientId + ' unregistered handler at ' + address);
+ },
+ clientDisconnected: function(clientId) {
+ log.push(clientId + ' disconnected');
+ }
+};
+
+// Create a SockJS bridge which lets everything through (be careful!)
+vertx.createSockJSServer(server).setEventBusBridgeListener(myListener).bridge({prefix: "/eventbus"}, [{}], [{}]);
+
+server.listen(8080);
View
107 vertx-examples/src/main/javascript/eventbusbridgelistener/index.html
@@ -0,0 +1,107 @@
+<html>
+<head>
+ <title></title>
+ <script src="http://ajax.googleapis.com/ajax/libs/jquery/1.7.1/jquery.min.js"></script>
+ <script src="http://cdn.sockjs.org/sockjs-0.2.1.min.js"></script>
+ <script src="vertxbus.js"></script>
+</head>
+<style>
+ .box {
+ background-color: #F0F0F0;
+ border: 5px solid blue;
+ width: 400px;
+ height: 300px;
+ overflow: scroll;
+ }
+
+ body {
+ background-color: #fff;
+ }
+</style>
+
+<body>
+
+<div id="sendbox" class="box">
+
+ <form onsubmit="return false;">
+ Address: <input type="text" id="address" value="someaddress"/><br />
+ Message: <input type="text" id="message" value="Hello, World!"/><br />
+ <div id="status">disconnected</div>
+ <button id="send">Send</button>
+ <button id="publish">Publish</button>
+ <button id="register">Register</button>
+ <button id="unregister">Unregister</button>
+ <button id="connection">Connect</button>
+ <button id="refresh">Refresh log</button>
+ </form>
+
+</div>
+
+<div id="logbox" class="box">
+<ul>
+</ul>
+</div>
+
+<script>
+
+ var eb = null;
+ var dummyHandler = function() {};
+
+ function refreshLog() {
+ $.getJSON('/log.json', function(data) {
+ $('#logbox ul li').remove();
+ for (var i = data.length - 1; i >= 0; i--) {
+ $('#logbox ul').append('<li>' + data[i] + '</li>');
+ }
+ });
+ }
+
+ $(document).ready(function() {
+ $('#refresh').click(function() {
+ refreshLog();
+ });
+ $('#send').click(function() {
+ if (eb) {
+ eb.send($('#address').val(), {"text" : $('#message').val()});
+ }
+ });
+ $('#publish').click(function() {
+ if (eb) {
+ eb.publish($('#address').val(), {"text" : $('#message').val()});
+ }
+ });
+ $('#register').click(function() {
+ if (eb) {
+ eb.registerHandler($('#address').val(), dummyHandler);
+ }
+ });
+ $('#unregister').click(function() {
+ if (eb) {
+ eb.unregisterHandler($('#address').val(), dummyHandler);
+ }
+ });
+ $('#connection').click(function() {
+ if (eb) {
+ eb.close();
+ } else {
+ eb = new vertx.EventBus("http://localhost:8080/eventbus");
+
+ eb.onopen = function() {
+ $("#status").text("connected");
+ $('#connection').text('disconnect');
+ };
+
+ eb.onclose = function() {
+ $("#status").text("disconnected");
+ $('#connection').text('connect');
+ eb = null;
+ };
+ }
+ });
+ $('#sendbox').show();
+ });
+
+</script>
+
+</body>
+</html>

0 comments on commit 753e58b

Please sign in to comment.