Re: client websocket

1,073 views
Skip to first unread message

Andres Junge

unread,
May 30, 2013, 11:18:30 AM5/30/13
to ve...@googlegroups.com
I have run in another pc (windows now, the later was ubuntu) and now I got:

Exception in Java verticle
io.netty.handler.codec.http.websocketx.WebSocketHandshakeException: Websocket connection attempt returned HTTP status code 404
        at org.vertx.java.core.http.impl.ClientConnection$HandshakeInboundHandler.inboundBufferUpdated(ClientConnection.java:157)
        at io.netty.channel.DefaultChannelHandlerContext.invokeInboundBufferUpdated(DefaultChannelHandlerContext.java:944)
        at io.netty.channel.DefaultChannelHandlerContext.fireInboundBufferUpdated0(DefaultChannelHandlerContext.java:912)
        at io.netty.channel.DefaultChannelHandlerContext.fireInboundBufferUpdated(DefaultChannelHandlerContext.java:892)
        at io.netty.handler.codec.ReplayingDecoder.callDecode(ReplayingDecoder.java:441)
        at io.netty.handler.codec.ByteToMessageDecoder.inboundBufferUpdated(ByteToMessageDecoder.java:69)
        at io.netty.channel.ChannelInboundByteHandlerAdapter.inboundBufferUpdated(ChannelInboundByteHandlerAdapter.java:46)
        at io.netty.channel.CombinedChannelDuplexHandler.inboundBufferUpdated(CombinedChannelDuplexHandler.java:175)
        at io.netty.channel.DefaultChannelHandlerContext.invokeInboundBufferUpdated(DefaultChannelHandlerContext.java:944)
        at io.netty.channel.DefaultChannelHandlerContext.fireInboundBufferUpdated0(DefaultChannelHandlerContext.java:912)
        at io.netty.channel.DefaultChannelHandlerContext.fireInboundBufferUpdated(DefaultChannelHandlerContext.java:892)
        at io.netty.channel.DefaultChannelPipeline.fireInboundBufferUpdated(DefaultChannelPipeline.java:826)
        at io.netty.channel.nio.AbstractNioByteChannel$NioByteUnsafe.read(AbstractNioByteChannel.java:116)
        at io.netty.channel.nio.NioEventLoop.processSelectedKey(NioEventLoop.java:429)
        at io.netty.channel.nio.NioEventLoop.processSelectedKeys(NioEventLoop.java:392)
        at io.netty.channel.nio.NioEventLoop.run(NioEventLoop.java:322)
        at io.netty.util.concurrent.SingleThreadEventExecutor$2.run(SingleThreadEventExecutor.java:114)
        at java.lang.Thread.run(Thread.java:722)

and no matter what I put as URI I always get the same error.
Does anybody has a working example of websocket client in a verticle?

Thanx
Andres

El 29-05-2013 22:08, Andres Junge escribió:
Hello
I have this very simple verticle. But I get the error:

public class MtGoxAPIVerticle extends Verticle {

    public void start() {

        HttpClient client = vertx.createHttpClient();
        client.connectWebsocket("ws://websocket.mtgox.com:80/mtgox?Currency=USD",
                new Handler<WebSocket>() {
                    public void handle(WebSocket ws) {
                        // Connected!
                        container.logger().info("MtGoxAPIVerticle connected");
                        vertx.eventBus().send("mtgox-ready", "ok");
                    }
                });

        container.logger().info("MtGoxAPIVerticle started");

    }
}


java.net.ConnectException: Refused Connection: localhost/127.0.0.1:80
every time. What am I doing wrong?

Thanx
Andres



Tim Fox

unread,
May 30, 2013, 11:22:17 AM5/30/13
to ve...@googlegroups.com
Take a look at the examples in the distro
> El 29-05-2013 22:08, Andres Junge escribi�:

Andres Junge

unread,
May 30, 2013, 11:44:22 AM5/30/13
to ve...@googlegroups.com
Where? I'm using vertx 2 beta2. I can�t find any examples.

Thanx for the reply
Andres

El 30-05-2013 11:22, Tim Fox escribi�:

Tim Fox

unread,
May 30, 2013, 11:58:10 AM5/30/13
to ve...@googlegroups.com
On 30/05/13 16:44, Andres Junge wrote:
Where? I'm using vertx 2 beta2.

Examples are here https://github.com/vert-x/vertx-examples

But looking at your code, the problem is you're using an absolute url.

It doesn't work like that - you set the host and port on the client object, and use a relative url when connecting.
I can´t find any examples.

Thanx for the reply
Andres

Tim Fox

unread,
May 30, 2013, 11:59:58 AM5/30/13
to ve...@googlegroups.com

Andres Junge

unread,
May 30, 2013, 12:58:11 PM5/30/13
to ve...@googlegroups.com
Hi,
I haven't found any example for websocket client (only for server).

Thanx for tip on the code, the error is gone! (still no conection, but at least error is gone)

Is this behavior changed from 1.3.1 to 2.0 ? Cause I have copy the code from:

http://vertx.io/core_manual_java.html#websockets-on-the-http-client

Thanx, i will search now why it doesn't connect.

My new code (in case you have any clue):

public class MtGoxAPIVerticle extends Verticle {

    public void start() {

        HttpClient client = vertx.createHttpClient();
        client.setHost("websocket.mtgox.com");
        client.setPort(443);
        client.connectWebsocket("/mtgox?Currency=USD",

                new Handler<WebSocket>() {
                    public void handle(WebSocket ws) {
                        // Connected!
                        System.out.println("MtGoxAPIVerticle connected");

                        container.logger().info("MtGoxAPIVerticle connected");
                        vertx.eventBus().send("mtgox-ready", "ok");
                       
                        ws.dataHandler(new Handler<Buffer>() {
                          @Override
                          public void handle(Buffer buff) {
                            System.out.println("b:"+buff.toString());
                          }
                        });
                       
                    }
                });
        System.out.println("MtGoxAPIVerticle started");
        container.logger().info("MtGoxAPIVerticle started");

    }
}


Andres
--
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 email to vertx+un...@googlegroups.com.
For more options, visit https://groups.google.com/groups/opt_out.
 
 

Tim Fox

unread,
May 30, 2013, 1:00:28 PM5/30/13
to ve...@googlegroups.com
Best bet is to look at the tests and see how your code differs.

At first glance... you're using port 443, normally that used for
https/wss not http/ws, so if your server is expecting wss then you need
to tell the client to use https too
> El 30-05-2013 11:58, Tim Fox escribi�:
>> On 30/05/13 16:44, Andres Junge wrote:
>>> Where? I'm using vertx 2 beta2.
>>
>> Examples are here https://github.com/vert-x/vertx-examples
>>
>> But looking at your code, the problem is you're using an absolute url.
>>
>> It doesn't work like that - you set the host and port on the client
>> object, and use a relative url when connecting.
>>> I can�t find any examples.
>>>
>>> Thanx for the reply
>>> Andres
>>>
>>> El 30-05-2013 11:22, Tim Fox escribi�:
>>>>> El 29-05-2013 22:08, Andres Junge escribi�:

Andres Junge

unread,
May 30, 2013, 3:05:47 PM5/30/13
to ve...@googlegroups.com
Ok. I think I have found the problem. It seem that the server (it is not
mine) needs the Origin header (as seen in
http://en.wikipedia.org/wiki/WebSocket). I have sniffed the connection
and I got:

-->GET /mtgox HTTP/1.1
-->Upgrade: websocket
-->Connection: Upgrade
-->Sec-WebSocket-Key: nEJQhC8gaOEWyHtj00tOnA==
-->Host: websocket.mtgox.com
-->Sec-WebSocket-Origin: http://websocket.mtgox.com
-->Sec-WebSocket-Version: 13


<--HTTP/1.1 403 Forbidden

How can i set that? Or how do I change the Sec-WebSocket-Origin to Origin?

Thanx a lot!
An3



El 30-05-2013 13:00, Tim Fox escribi�ソス:
>> El 30-05-2013 11:58, Tim Fox escribi�ソス:
>>> On 30/05/13 16:44, Andres Junge wrote:
>>>> Where? I'm using vertx 2 beta2.
>>>
>>> Examples are here https://github.com/vert-x/vertx-examples
>>>
>>> But looking at your code, the problem is you're using an absolute url.
>>>
>>> It doesn't work like that - you set the host and port on the client
>>> object, and use a relative url when connecting.
>>>> I can�ソスt find any examples.
>>>>
>>>> Thanx for the reply
>>>> Andres
>>>>
>>>> El 30-05-2013 11:22, Tim Fox escribi�ソス:
>>>>>> El 29-05-2013 22:08, Andres Junge escribi�ソス:

Andres Junge

unread,
May 30, 2013, 3:23:16 PM5/30/13
to ve...@googlegroups.com
I�ソスve found it!! The server is using the WebSocketVersion.HYBI_08. So I
change that on the code and worked like a charm!!

Thanx everybody
salu2
an3


El 30-05-2013 15:05, Andres Junge escribi�ソス:

Andres Junge

unread,
May 29, 2013, 10:08:38 PM5/29/13
to ve...@googlegroups.com
Reply all
Reply to author
Forward
0 new messages