java - How to simulate a slow Websocket client in Netty 4.1? -


i'm trying write slow reading client test if server handles correctly.

currently have following code. looks long simple.

the main points note:

  1. i'm using oioeventloopgroup , oiosocketchannel it'll blocking.
  2. there thread.sleep() in messagehandler block channel's eventloop.

i expect eventloop block , not read socket, seems messages still received in buffer somewhere. further, if kill server, client continues print out messages until buffer exhausted.

can please explain why happening? , what's proper way in netty?

here's code:

public class nettyclient {    public static void main(string[] args) {       nettyclient client = new nettyclient();       channelfuture future = client.startclient("ws://localhost:9090/websocket");       runtime.getruntime().addshutdownhook(new thread() {          @override          public void run() {             future.channel().close();          }       });       future.syncuninterruptibly();    }     private channelfuture startclient(string connection) {       bootstrap bootstrap = new bootstrap();       bootstrap.group(new oioeventloopgroup())             .channel(oiosocketchannel.class)             .remoteaddress("localhost", 9090)             .handler(new channelinitializer<socketchannel>() {                @override                protected void initchannel(socketchannel ch) throws exception {                   channelpipeline pipeline = ch.pipeline();                   pipeline.addlast(new httpclientcodec());                   pipeline.addlast(new httpobjectaggregator(10));                   pipeline.addlast(new websocketclientprotocolhandler(new uri(connection), websocketversion.v08, null, false, new defaulthttpheaders(), 1024));                   pipeline.addlast(new subscriptionhandler());                }             });        return bootstrap.connect();    }     private static class subscriptionhandler extends channelinboundhandleradapter {       @override       public void usereventtriggered(channelhandlercontext ctx, object evt) throws exception {          if (evt == websocketclientprotocolhandler.clienthandshakestateevent.handshake_complete) {             system.out.println("subscriptionhandler: handshake complete");             // remove self             ctx.pipeline().remove(subscriptionhandler.class);             // add messagehandler             ctx.pipeline().addlast(new messagehandler());          }       }    }     private static class messagehandler extends simplechannelinboundhandler {        @override       protected void channelread0(channelhandlercontext ctx, object msg) throws exception {          websocketframe frame = (websocketframe) msg;          if (frame instanceof textwebsocketframe) {             textwebsocketframe textframe = (textwebsocketframe) frame;             system.out.println("client sleeping 3000ms now...");             timeunit.milliseconds.sleep(3000);             system.out.println("client received: " + textframe.text());          } else {             system.out.println("client received message of type: " + msg.getclass().getsimplename());          }       }    }  } 


Comments

Popular posts from this blog

account - Script error login visual studio DefaultLogin_PCore.js -

xcode - CocoaPod Storyboard error: -