-
Notifications
You must be signed in to change notification settings - Fork 2
Expand file tree
/
Copy pathNioClientHandler.java
More file actions
45 lines (39 loc) · 1.14 KB
/
Copy pathNioClientHandler.java
File metadata and controls
45 lines (39 loc) · 1.14 KB
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
package lbtech.obj;
import org.slf4j.Logger;
import org.slf4j.LoggerFactory;
import io.netty.channel.ChannelHandlerContext;
import io.netty.channel.ChannelInboundHandlerAdapter;
import io.netty.channel.SimpleChannelInboundHandler;
import io.netty.handler.timeout.IdleState;
import io.netty.handler.timeout.IdleStateEvent;
/**
* client处理类
* @author DF
*
*/
public class NioClientHandler extends SimpleChannelInboundHandler<Person>{
/**
* 日志对象
*/
protected Logger logger = LoggerFactory.getLogger(NioClientHandler.class);
@Override
protected void channelRead0(ChannelHandlerContext ctx, Person msg)
throws Exception {
logger.info("client----->收到服务器端消息:name:{},age:{}" , msg.getName(),msg.getAge());
}
@Override
public void channelActive(ChannelHandlerContext ctx) throws Exception {
super.channelActive(ctx);
ctx.writeAndFlush(new Person("xxx",11));
}
@Override
public void exceptionCaught(ChannelHandlerContext ctx, Throwable cause)
throws Exception {
try {
super.exceptionCaught(ctx, cause);
} catch (Exception e) {
logger.info("client exception ....关闭连接");
ctx.close();
}
}
}