Java client to SockJS server

1,959 views
Skip to first unread message

Leng Sheng Hong

unread,
Mar 2, 2013, 1:32:26 PM3/2/13
to ve...@googlegroups.com
I am curious if anyone has written a java client to connect to an existing vertx SockJS server. What's the best way to do it? Is there any examples?
I am trying to write a server side bot for a game. (Might be connecting with python too once i got how to do it in java)

Leng Sheng Hong

unread,
Mar 2, 2013, 1:55:05 PM3/2/13
to ve...@googlegroups.com
I realized that I can use vertx http client t o connect to the SockJS server.
Vertx vertx = Vertx.newVertx();
HttpClient client = vertx.createHttpClient().setHost("127.0.0.1").setPort(8080);;
client.connectWebsocket("/testapp",WebSocketVersion.RFC6455,new Handler<WebSocket>() { .....

Message has been deleted

Leng Sheng Hong

unread,
Mar 4, 2013, 6:47:40 AM3/4/13
to ve...@googlegroups.com
I am managed to connect to the server however handleRegister never gets trigger.
I tried to manually connect to the server with html5 WebSocket, i don't really know what to do with it but this is what i can see from firebug, it's connecting to:
http://127.0.0.1:8080/myserver/info

1) the vertxbus is trying to connect to http://127.0.0.1:8080/myserver/info
2) then it's upgraded to websocket automatically with a URL that is always random ws://127.0.0.1:8080/myserver/122/abcdefgh/websocket

So, how can I connect to the sockjs underlying websocket url with the normal WebSocket class.
I tried these but they won't work (no reply from server).
new WebSocket('ws://127.0.0.1:8080/myserver');
new WebSocket('ws://127.0.0.1:8080/myserver/info');

When I tried the websocket URL which i make it up myself: ws://127.0.0.1:8080/myserver/122/abcdefgh/websocket
I am able to get message 'o' and 'h' where h is the heartbeat.

then i tried to send the message which vertxbus.js is sending to subscribe to a channel, the websocket connection will get disconnected
ws.send('{"type":"register","address":"app.mychannel.test"}')

How can i determine the correct WS url and subsribe to a channel in standard html5 WebSocket? I m trying to understand how it works so i can port it to other languages

Angelo K H

unread,
Aug 28, 2013, 2:13:21 AM8/28/13
to ve...@googlegroups.com
Hi,

Do you resolve this issue?

Martin Ahrens

unread,
Nov 21, 2013, 4:11:02 PM11/21/13
to ve...@googlegroups.com
I've finally gotten it to work with the following Groovy code, note the double JSON encoding:

static void vertxWrite(type, address, body) {
    VertxFactory.newVertx().createHttpClient().setHost('localhost').setPort(8888)
        .connectWebsocket("/eventbus/123/abc/websocket", new Handler<WebSocket>() {
        public void handle(WebSocket ws) {
            def envelope = [type:type, address:address, body:body]
            def doubleJSON = JsonOutput.toJson(JsonOutput.toJson(envelope))
            ws.writeTextFrame(doubleJSON)
            ws.close()
        }
    }).close()
}

Angelo H

unread,
Nov 21, 2013, 4:23:48 PM11/21/13
to ve...@googlegroups.com
Thank you!

--
You received this message because you are subscribed to a topic in the Google Groups "vert.x" group.
To unsubscribe from this topic, visit https://groups.google.com/d/topic/vertx/sLtXoIQu6-8/unsubscribe.
To unsubscribe from this group and all of its topics, send an email to vertx+un...@googlegroups.com.
For more options, visit https://groups.google.com/groups/opt_out.

Martin Ahrens

unread,
Nov 22, 2013, 1:05:57 PM11/22/13
to ve...@googlegroups.com
Thanks to clarification from Tim I'm now using a less complex way with raw websockets:

static void vertxWrite(type, address, body) {
    VertxFactory.newVertx().createHttpClient().setHost('localhost').setPort(8888)
        .connectWebsocket("/eventbus/websocket", new Handler<WebSocket>() {
        public void handle(WebSocket ws) {
            def envelope = [type:type, address:address, body:body]
            ws.writeTextFrame(JsonOutput.toJson(envelope))
            ws.close()
        }
    }).close()
}

田传武

unread,
Nov 22, 2013, 10:49:29 PM11/22/13
to ve...@googlegroups.com
Hi,
Can the vertx websockets java client run on android?
> You received this message because you are subscribed to the Google Groups
> "vert.x" group.
> To unsubscribe from this group and stop receiving emails from it, send an
Reply all
Reply to author
Forward
0 new messages