Skip to content

Commit

Permalink
websocker server added.
Browse files Browse the repository at this point in the history
  • Loading branch information
bikash committed Mar 8, 2016
1 parent a7e17a1 commit 64ce4dd
Show file tree
Hide file tree
Showing 3 changed files with 225 additions and 5 deletions.
11 changes: 6 additions & 5 deletions demo/test/index.html
Original file line number Diff line number Diff line change
Expand Up @@ -4,7 +4,7 @@
<script src="http://ajax.googleapis.com/ajax/libs/jquery/2.0.3/jquery.min.js" ></script>
<script src="http://cdnjs.cloudflare.com/ajax/libs/jqueryui/1.11.4/jquery-ui.min.js" ></script>
<script src="http://cdn.jsdelivr.net/sockjs/1.0.0/sockjs.min.js"></script>
<script src="http://cdn.rawgit.com/vert-x3/vertx-bus-bower/master/vertx-eventbus.js"></script>
<script src="vertxbus.js"></script>
<script src="http://cdnjs.cloudflare.com/ajax/libs/smoothie/1.27.0/smoothie.min.js"></script>
<link rel="stylesheet" href="http://maxcdn.bootstrapcdn.com/font-awesome/4.4.0/css/font-awesome.min.css">
<link rel="stylesheet" href="http://cdnjs.cloudflare.com/ajax/libs/animate.css/3.4.0/animate.min.css">
Expand Down Expand Up @@ -116,8 +116,9 @@
});
smoothieChart.streamTo(document.getElementById("mycanvas"), 250);
}
function publish(address, message) {
function publish(address, message, sensor) {
var headers = {sensor: "hr"};
//alert(message)
eb.publish(address, message, headers);
}
function subscribe(address) {
Expand Down Expand Up @@ -169,7 +170,7 @@
}
$(document).ready(function() {
//var wsUri = "ws://localhost:5555/ws";
var host = "ws://localhost:5555/ws";
var host = "localhost:5555"
//var eb = new WebSocket(host);
//var host = "ws://localhost:9090/ws";

Expand All @@ -178,14 +179,14 @@
heartElement = $("#heart");
setupSmootie();
//eb = new EventBus(socket);
eb = new EventBus("http://localhost:5555");
eb = new vertx.EventBus("http://localhost:5555");
eb.onopen = function () {
subscribeToHeartrate();
subscribeToActivity();
};
$("#sendButton").click(function() {
var msg = $("textarea#sendMessage").val();
publish("iot.in.hr", JSON.parse(msg));
publish(host, JSON.parse(msg),"iot.in.hr");
});
});
</script>
Expand Down
216 changes: 216 additions & 0 deletions demo/test/vertxbus.js
Original file line number Diff line number Diff line change
@@ -0,0 +1,216 @@
/*
* Copyright 2011-2012 the original author or authors.
*
* Licensed under the Apache License, Version 2.0 (the "License");
* you may not use this file except in compliance with the License.
* You may obtain a copy of the License at
*
* http://www.apache.org/licenses/LICENSE-2.0
*
* Unless required by applicable law or agreed to in writing, software
* distributed under the License is distributed on an "AS IS" BASIS,
* WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied.
* See the License for the specific language governing permissions and
* limitations under the License.
*/

var vertx = vertx || {};

!function(factory) {
if (typeof define === "function" && define.amd) {
// Expose as an AMD module with SockJS dependency.
// "vertxbus" and "sockjs" names are used because
// AMD module names are derived from file names.
define("vertxbus", ["sockjs"], factory);
} else {
// No AMD-compliant loader
factory(SockJS);
}
}(function(SockJS) {

vertx.EventBus = function(url, options) {

var that = this;
var sockJSConn = new SockJS(url, undefined, options);
var handlerMap = {};
var replyHandlers = {};
var state = vertx.EventBus.CONNECTING;
var sessionID = null;
var pingTimerID = null;

that.onopen = null;
that.onclose = null;

that.login = function(username, password, replyHandler) {
sendOrPub("send", 'vertx.basicauthmanager.login', {username: username, password: password}, function(reply) {
if (reply.status === 'ok') {
that.sessionID = reply.sessionID;
}
if (replyHandler) {
delete reply.sessionID;
replyHandler(reply)
}
});
}

that.send = function(address, message, replyHandler) {
sendOrPub("send", address, message, replyHandler)
}

that.publish = function(address, message, replyHandler) {
sendOrPub("publish", address, message, replyHandler)
}

that.registerHandler = function(address, handler) {
checkSpecified("address", 'string', address);
checkSpecified("handler", 'function', handler);
checkOpen();
var handlers = handlerMap[address];
if (!handlers) {
handlers = [handler];
handlerMap[address] = handlers;
// First handler for this address so we should register the connection
var msg = { type : "register",
address: address };
sockJSConn.send(JSON.stringify(msg));
} else {
handlers[handlers.length] = handler;
}
}

that.unregisterHandler = function(address, handler) {
checkSpecified("address", 'string', address);
checkSpecified("handler", 'function', handler);
checkOpen();
var handlers = handlerMap[address];
if (handlers) {
var idx = handlers.indexOf(handler);
if (idx != -1) handlers.splice(idx, 1);
if (handlers.length == 0) {
// No more local handlers so we should unregister the connection

var msg = { type : "unregister",
address: address};
sockJSConn.send(JSON.stringify(msg));
delete handlerMap[address];
}
}
}

that.close = function() {
checkOpen();
if (pingTimerID) clearInterval(pingTimerID);
state = vertx.EventBus.CLOSING;
sockJSConn.close();
}

that.readyState = function() {
return state;
}

sockJSConn.onopen = function() {
// Send the first ping then send a ping every 5 seconds
sendPing();
pingTimerID = setInterval(sendPing, 5000);
state = vertx.EventBus.OPEN;
if (that.onopen) {
that.onopen();
}
};

sockJSConn.onclose = function() {
state = vertx.EventBus.CLOSED;
if (that.onclose) {
that.onclose();
}
};

sockJSConn.onmessage = function(e) {
var msg = e.data;
var json = JSON.parse(msg);
var body = json.body;
var replyAddress = json.replyAddress;
var address = json.address;
var replyHandler;
if (replyAddress) {
replyHandler = function(reply, replyHandler) {
// Send back reply
that.send(replyAddress, reply, replyHandler);
};
}
var handlers = handlerMap[address];
if (handlers) {
// We make a copy since the handler might get unregistered from within the
// handler itself, which would screw up our iteration
var copy = handlers.slice(0);
for (var i = 0; i < copy.length; i++) {
copy[i](body, replyHandler);
}
} else {
// Might be a reply message
var handler = replyHandlers[address];
if (handler) {
delete replyHandlers[address];
handler(body, replyHandler);
}
}
}

function sendPing() {
var msg = {
type: "ping"
}
sockJSConn.send(JSON.stringify(msg));
}

function sendOrPub(sendOrPub, address, message, replyHandler) {
checkSpecified("address", 'string', address);
checkSpecified("replyHandler", 'function', replyHandler, true);
checkOpen();
var envelope = { type : sendOrPub,
address: address,
body: message };
if (that.sessionID) {
envelope.sessionID = that.sessionID;
}
if (replyHandler) {
var replyAddress = makeUUID();
envelope.replyAddress = replyAddress;
replyHandlers[replyAddress] = replyHandler;
}
var str = JSON.stringify(envelope);
sockJSConn.send(str);
}

function checkOpen() {
if (state != vertx.EventBus.OPEN) {
throw new Error('INVALID_STATE_ERR');
}
}

function checkSpecified(paramName, paramType, param, optional) {
if (!optional && !param) {
throw new Error("Parameter " + paramName + " must be specified");
}
if (param && typeof param != paramType) {
throw new Error("Parameter " + paramName + " must be of type " + paramType);
}
}

function isFunction(obj) {
return !!(obj && obj.constructor && obj.call && obj.apply);
}

function makeUUID(){return"xxxxxxxx-xxxx-4xxx-yxxx-xxxxxxxxxxxx"
.replace(/[xy]/g,function(a,b){return b=Math.random()*16,(a=="y"?b&3|8:b|0).toString(16)})}

}

vertx.EventBus.CONNECTING = 0;
vertx.EventBus.OPEN = 1;
vertx.EventBus.CLOSING = 2;
vertx.EventBus.CLOSED = 3;

return vertx.EventBus;

});
3 changes: 3 additions & 0 deletions server.js
Original file line number Diff line number Diff line change
@@ -0,0 +1,3 @@
var vertx = require('vertx');
vertx.createHttpServer().requestHandler(function(r) {
r.response.end("test 1\n");}).listen(5555);

0 comments on commit 64ce4dd

Please sign in to comment.