java - Check login in blocking mode in Netty -
i have simple netty client (socket). every time when send data server, must check client logged in or not. if not, must send user credentials , wait response server true or false. must in blocking mode , if receive true server, can continue sending other data. current code is:
eventloopgroup workergroup = new nioeventloopgroup(); try { bootstrap bootstrap = new bootstrap(); bootstrap.group(workergroup) .channel(niosocketchannel.class) .option(channeloption.so_keepalive, true) .handler(new trsclientinterfaceinitializer()); channel ch = bootstrap.connect(host, port).sync().channel(); channelfuture lastwritefuture = null; (message message : list) { if (!isloggedin) { lastwritefuture = ch.writeandflush("loginpassword"); } //if login success, must loop through data in list , send other data server lastwritefuture = ch.writeandflush(message.getdatastring); } if (lastwritefuture != null) { lastwritefuture.sync(); } } catch //// this handler:
//handler extended simplechannelinboundhandler<string> @override protected void channelread0(channelhandlercontext ctx, string data) throws exception { system.out.println(data); system.out.flush(); if ("success".equals(data)) { isloggedin = true } } how can implement logic in blocking mode? can't find solution in web. help? pls.
blocking client until write operation finish:
lastwritefuture = ch.writeandflush(message.getdatastring); lastwritefuture.await(); your server may write response indicate whether request successful:
//handler extended simplechannelinboundhandler<string> @override protected void channelread0(channelhandlercontext ctx, string data) throws exception { system.out.println(data); system.out.flush(); if ("success".equals(data)) { isloggedin = true ctx.channel().writeandflush("success!"); return; } ctx.channel().writeandflush("fail!"); } handle server's response in trsclientinterfaceinitializer handler.
Comments
Post a Comment