event bus bridge cannot ok on vert.x 1.3

554 views
Skip to first unread message

Yi Long

unread,
Nov 23, 2012, 12:36:10 AM11/23/12
to vert.x
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>

Daryl Teo

unread,
Nov 23, 2012, 1:01:12 AM11/23/12
to ve...@googlegroups.com
That error is given when the socket is not open.

Without really looking at the code, you'll have to make sure that you are actually able to establish an open sockJS connection to your server.

Regards,
Daryl

Tim Yates

unread,
Nov 23, 2012, 1:37:47 AM11/23/12
to ve...@googlegroups.com

don't call subscribe() or publish() until eb.onopen has been called

Tim

--
You received this message because you are subscribed to the Google Groups "vert.x" group.
To post to this group, send an email to ve...@googlegroups.com.
To unsubscribe from this group, send email to vertx+un...@googlegroups.com.
For more options, visit this group at http://groups.google.com/group/vertx?hl=en-GB.

Yi Long

unread,
Nov 23, 2012, 1:54:52 AM11/23/12
to vert.x
I have tried to call eb.onopen but the state is still
"INVALID_STATE_ERR", how could I make the state open? thanks


On Nov 23, 2:37 pm, Tim Yates <tim.ya...@gmail.com> wrote:
> don't call subscribe() or publish() until eb.onopen has been called
>
> Tim

Tim Yates

unread,
Nov 23, 2012, 1:57:16 AM11/23/12
to ve...@googlegroups.com

no, you need to move subscribe and publish into the onopen function. it is called when the connection is successful,  you don't call it explicitly yourself

hope this makes sense

Tim

Yi Long

unread,
Nov 23, 2012, 2:23:17 AM11/23/12
to vert.x
thanks all of your replying , I found the problem is that I have to
bind publish or subscribe to dom event, that means after page load
then the sockjs will connect to server side.
after I bind publish to button click, then everything is fine.
thanks
Yi

On Nov 23, 2:57 pm, Tim Yates <tim.ya...@gmail.com> wrote:
> no, you need to move subscribe and publish into the onopen function. it is
> called when the connection is successful,  you don't call it explicitly
> yourself
>
> hope this makes sense
>
> Tim

Tim Yates

unread,
Nov 23, 2012, 4:21:31 AM11/23/12
to ve...@googlegroups.com
I meant that you could change openConn to:

    function openConn() {
        if (!eb) {
            eb = new vertx.EventBus("http://localhost:8080/eventbus");
            eb.onopen = function() {
                subscribe();
                publish('hello');
            };
            eb.onclose = function() {
                eb = null;
            };
        }
    }

And you last function to just:

    $(function() {
        openConn();
    });

Then subscribe won't be called till the connection is open...

Glad you got it working though.

Tim

Tim Fox

unread,
Nov 23, 2012, 5:23:48 AM11/23/12
to ve...@googlegroups.com
The other replies are correct, but also please look at the working eventbusbridge example in the distribution which shows how to do this.
Reply all
Reply to author
Forward
0 new messages