WebSocketClientHandler : how to wait for server response without closing channel ?

1,807 views
Skip to first unread message

philippe...@gmail.com

unread,
Jun 22, 2013, 4:48:30 PM6/22/13
to ne...@googlegroups.com
Hello,

I am working with Websocket on client side with Netty.
I have a use case where I need to wait on the client side for server response and timeout if answer does not come in a defined interval.

I have looked at https://github.com/netty/netty/blob/master/example/src/main/java/io/netty/example/http/websocketx/client/ code comment and there is something not clear for me:
Comment says:
// Send 10 messages and wait for responses
ch.write(new TextWebSocketFrame("Message #" + i));

But  it seems it is the following code that makes the client wait for server response:
ch.closeFuture().sync();

Note that in my case I don't want to close the websocket as shown in sample.

So my idea to implement this was to use a CountDownLatch which I would pass to WebSocketClientHandler.
In the equivalent of WebSocketClient, I would wait on latch with timeout.

If WebSocketClientHandler gets a response it would decrement latch and make WebSocketClient continue processing, otherwise timeout would occur.

Is this way of doing it is correct ? Are there better way to do it ?

Thanks for your help
Regards
Philippe

Norman Maurer

unread,
Jun 23, 2013, 4:04:09 AM6/23/13
to ne...@googlegroups.com
You need to have a ChannelInboundHandler in your ChannelPipeline which will intercept the received frames and so handle them. For timeout you could use the IdleStateHandler.

Bye,
Norman
--
 
---
You received this message because you are subscribed to the Google Groups "Netty discussions" group.
To unsubscribe from this group and stop receiving emails from it, send an email to netty+un...@googlegroups.com.
For more options, visit https://groups.google.com/groups/opt_out.
 
 

philippe...@gmail.com

unread,
Jun 23, 2013, 4:48:33 AM6/23/13
to ne...@googlegroups.com
Hello ,
Thanks for rapid answer.
Thanks Norman, for timeout it's ok, I had found info in one of your previous answers.

But for blocking client after sending , it is not clear for me.
As I want to block the thread just after this (in your example of WebSocketClient:

            System.out.println("WebSocket Client sending message");
            for (int i = 0; i < 10; i++) {
                ch.write(new TextWebSocketFrame("Message #" + i));
            }
wait here a certain time for server response.

I may misunderstand but the channelinboundhandler is executed by another thread right ?

thanks

Regards
Message has been deleted

Norman Maurer

unread,
Jun 24, 2013, 9:45:13 AM6/24/13
to ne...@googlegroups.com, ne...@googlegroups.com
If you really want to block you could make use of a CountDownLatch which is injected in the ChannelHandler. But I would advice to rethink your usage of Netty as most of the times you don't want to block to get most out of your application

Am 23.06.2013 um 10:49 schrieb philippe...@gmail.com:

> Hello ,
> Thanks for rapid answer.
> Thanks Norman, for timeout it's ok, I had found info in one of your previous answers.
>
> But for blocking client after sending , it is not clear for me.
> As I want to block the thread just after this (in your example of WebSocketClient:
>
> System.out.println("WebSocket Client sending message");
> for (int i = 0; i < 10; i++) {
> ch.write(new TextWebSocketFrame("Message #" + i));
> }
> wait here a certain time for server response.
>
> I may misunderstand but the channelinboundhandler is executed by another thread right ?
>
> thanks
>
> Regards
>

philippe...@gmail.com

unread,
Jun 24, 2013, 3:00:37 PM6/24/13
to ne...@googlegroups.com, norman...@googlemail.com
Hello Norman,
Thanks for answer.

Regards
Philippe
Reply all
Reply to author
Forward
0 new messages