netty-确定客户端和服务端编解码工具
This commit is contained in:
@@ -1,14 +1,16 @@
|
||||
package com.sv.netty;
|
||||
|
||||
import com.sv.netty.message.HeartBeat;
|
||||
import com.sv.netty.message.MessageType;
|
||||
import com.sv.netty.message.VenueMessage;
|
||||
import com.sv.netty.utils.EncodeMsg;
|
||||
import io.netty.channel.Channel;
|
||||
import io.netty.channel.ChannelHandler;
|
||||
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;
|
||||
|
||||
import java.net.UnknownHostException;
|
||||
|
||||
/**
|
||||
* 通讯服务器请求处理
|
||||
*
|
||||
@@ -16,77 +18,82 @@ import io.netty.handler.timeout.IdleStateEvent;
|
||||
* @date 05/12/2017 10:27 PM
|
||||
*/
|
||||
@ChannelHandler.Sharable
|
||||
public class ClientHandler extends ChannelInboundHandlerAdapter {
|
||||
|
||||
private final static String TAG = "ClientHandler";
|
||||
|
||||
private boolean hasRead = false;
|
||||
|
||||
@Override
|
||||
public void channelRegistered(ChannelHandlerContext ctx) throws Exception {
|
||||
super.channelRegistered(ctx);
|
||||
// ClientTcpSession.getInstance().setContext(ctx);
|
||||
}
|
||||
|
||||
@Override
|
||||
public void channelActive(ChannelHandlerContext ctx) throws Exception {
|
||||
super.channelActive(ctx);
|
||||
//服务器连上以后立即模拟心跳返回
|
||||
ctx.writeAndFlush(getHbMessage());
|
||||
}
|
||||
|
||||
@Override
|
||||
public void channelInactive(ChannelHandlerContext ctx) throws Exception {
|
||||
super.channelInactive(ctx);
|
||||
ClientThread.getInstance().clearFuture();
|
||||
ClientThread.getInstance().restart();
|
||||
}
|
||||
|
||||
@Override
|
||||
public void channelRead(ChannelHandlerContext ctx, Object msg) throws Exception {
|
||||
super.channelRead(ctx, msg);
|
||||
// Message message = JsonMapper.fromJson(msg.toString(), Message.class);
|
||||
// MessageService.getInstance().execute(message);
|
||||
}
|
||||
|
||||
@Override
|
||||
public void exceptionCaught(ChannelHandlerContext ctx, Throwable cause) throws Exception {
|
||||
super.exceptionCaught(ctx, cause);
|
||||
System.err.println("错了Error");
|
||||
System.err.println("Error");
|
||||
// GlobalConfig.isConnected = false;
|
||||
ctx.close();
|
||||
}
|
||||
public class ClientHandler extends SimpleChannelInboundHandler<String> {
|
||||
|
||||
/**
|
||||
* 获取心跳返回消息
|
||||
*
|
||||
* @return
|
||||
*/
|
||||
private VenueMessage getHbMessage() {
|
||||
HeartBeat hb = new HeartBeat();
|
||||
// hb.setVersionCode(AppUtil.getVersionCode(StartApplication.getAppContext()));
|
||||
VenueMessage message = new VenueMessage();
|
||||
message.setMessageType(MessageType.HB);
|
||||
// message.setDeviceId(DeviceIdUtil.generateDeviceId(mContext));
|
||||
return message;
|
||||
}
|
||||
|
||||
/**
|
||||
* 心跳处理
|
||||
*
|
||||
* 当通道就绪就会触发
|
||||
* @param ctx
|
||||
* @param evt
|
||||
* @throws Exception
|
||||
*/
|
||||
@Override
|
||||
public void userEventTriggered(ChannelHandlerContext ctx, Object evt) throws Exception {
|
||||
public void channelActive(ChannelHandlerContext ctx) throws Exception {
|
||||
super.channelActive(ctx);
|
||||
}
|
||||
|
||||
/**
|
||||
* 当通道失效就会触发
|
||||
* @param ctx
|
||||
* @throws Exception
|
||||
*/
|
||||
@Override
|
||||
public void channelInactive(ChannelHandlerContext ctx) throws Exception {
|
||||
super.channelInactive(ctx);
|
||||
}
|
||||
|
||||
/**
|
||||
* 当通道有读取事件时触发
|
||||
* @param ctx
|
||||
* @param msg
|
||||
* @throws Exception
|
||||
*/
|
||||
@Override
|
||||
public void channelRead0(ChannelHandlerContext ctx, String msg) throws Exception {
|
||||
System.out.println("接收服务器响应msg:[" + msg + "]");
|
||||
// MessageDTO message = JsonMapper.fromJson(msg, MessageDTO.class);
|
||||
// MessageService.getInstance().execute(message);
|
||||
}
|
||||
|
||||
/**
|
||||
* 心跳
|
||||
* @param ctx
|
||||
* @param evt
|
||||
*/
|
||||
@Override
|
||||
public void userEventTriggered(ChannelHandlerContext ctx, Object evt) throws UnknownHostException {
|
||||
if (IdleStateEvent.class.isAssignableFrom(evt.getClass())) {
|
||||
IdleStateEvent event = (IdleStateEvent) evt;
|
||||
if (event.state() == IdleState.ALL_IDLE) {
|
||||
ctx.writeAndFlush(getHbMessage());
|
||||
ctx.write(EncodeMsg.INSTANCE.encode(getHbMessage()));
|
||||
ctx.flush();
|
||||
}
|
||||
}
|
||||
}
|
||||
|
||||
/**
|
||||
* 封装心跳请求包
|
||||
* @throws Exception
|
||||
*/
|
||||
private HeartBeat getHbMessage() {
|
||||
HeartBeat hb = new HeartBeat();
|
||||
hb.setDeviceName("dsadasdasd");
|
||||
hb.setVenueId(123);
|
||||
return hb;
|
||||
}
|
||||
|
||||
/**
|
||||
* 处理异常
|
||||
* @param ctx
|
||||
* @param cause
|
||||
* @throws Exception
|
||||
*/
|
||||
@Override
|
||||
public void exceptionCaught(ChannelHandlerContext ctx, Throwable cause) {
|
||||
System.out.println("ClientHandler exceptionCaught");
|
||||
cause.printStackTrace();
|
||||
Channel channel = ctx.channel();
|
||||
if(channel.isActive()) {
|
||||
ctx.close();
|
||||
}
|
||||
}
|
||||
|
||||
}
|
||||
|
||||
Reference in New Issue
Block a user