Hi all
recently I just quickly implement the event bus bridge on Vert.x 1.3.
but client side always throw the "INVALID_STATE_ERR", in vertxbus.js,
when registerHandle or publish, it always check the state, if state !=
vertx.EventBus.OPEN will throw Error('INVALID_STATE_ERR') ,who can
help me on that?
thanks in advance
the code on server side:
HttpServer httpServer = vertx.createHttpServer();
httpServer.requestHandler(new Handler<HttpServerRequest>() {
public void handle(HttpServerRequest req) {
String file = req.path.equals("/") ? "index.html" : req.path;
req.response.sendFile(serverRoot + file);
}
});
SockJSServer sockJSServer = vertx.createSockJSServer(httpServer);
// Set security permission to let everything go through
JsonArray permitted = new JsonArray();
permitted.add(new JsonObject());
//bridge to eventbus and allow
sockJSServer.bridge(new JsonObject().putString("prefix", "/
eventbus"), permitted, permitted);
eb = vertx.eventBus();
myHandler = new Handler<Message<JsonObject>>(){
public void handle(Message<JsonObject> event) {
eb.publish(_Complete_Update_Topo_Event, new
JsonObject().putString("aaa","bbbb"));
}
};
//register event that ask for update current status of Topology
eb.registerHandler(_Ask_Update_Topo_Event, myHandler);
httpServer.listen(8080);
client side:
<script src="
http://code.jquery.com/jquery-1.8.3.min.js"></script>
<script src="
http://cdn.sockjs.org/sockjs-0.3.4.min.js"></script>
<script src="js/vertxbus.js"></script>
<script src="js/json2.js"></script>
<script>
var eb = null;
function publish(msg) {
if (eb) {
var json = {
text : msg
};
eb.publish('asking', json);
console.log(' ---- publish event ----');
}
}
function subscribe() {
if (eb) {
eb.registerHandler('completing', function(msg, replyTo) {
alert(msg);
});
}
}
function openConn() {
if (!eb) {
eb = new vertx.EventBus("
http://localhost:8080/eventbus");
//eb.sockJSConn.onopen();
eb.onopen = function() {
};
eb.onclose = function() {
eb = null;
};
}
}
function closeConn() {
if (eb) {
eb.close();
}
}
$(function() {
openConn();
subscribe();
publish('hello');
});
</script>