com.feingto.iot.client.handler.CustomClientChannelHandler Maven / Gradle / Ivy
package com.feingto.iot.client.handler;
import com.feingto.iot.common.handler.DefaultSimpleChannelHandler;
import com.feingto.iot.common.model.custom.BaseMessage;
import com.feingto.iot.common.model.custom.HeartMessage;
import com.feingto.iot.common.model.custom.Message;
import com.feingto.iot.common.model.custom.SystemMessage;
import com.feingto.iot.common.model.enums.MessageType;
import io.netty.channel.ChannelHandlerContext;
import io.netty.handler.timeout.IdleState;
import io.netty.handler.timeout.IdleStateEvent;
import lombok.extern.slf4j.Slf4j;
import java.util.Date;
/**
* 自定义消息客户端通道处理器
*
* @author longfei
*/
@Slf4j
public class CustomClientChannelHandler extends DefaultSimpleChannelHandler {
@Override
public void handleMessage(ChannelHandlerContext ctx, BaseMessage msg) {
byte protocol = msg.protocol();
if (protocol == MessageType.LOGIN_RESPONSE.getValue()) {
log.debug(">>> login success");
} else if (protocol == MessageType.SYSTEM.getValue()) {
log.debug(">>> subscription: {}", ((SystemMessage) msg).content());
} else if (protocol == MessageType.MESSAGE_RESPONSE.getValue()) {
log.debug(">>> message: {}", ((Message) msg).content());
}
}
/**
* 心跳检测
*/
@Override
public void userEventTriggered(ChannelHandlerContext ctx, Object evt) {
if (evt instanceof IdleStateEvent) {
IdleStateEvent event = (IdleStateEvent) evt;
if (event.state() == IdleState.WRITER_IDLE) {
log.debug(">>> writer idle - [{}]", new Date());
ctx.writeAndFlush(new HeartMessage());
}
}
}
}
© 2015 - 2025 Weber Informatics LLC | Privacy Policy