/*
* Created by 李迅 on 2022-01-26 14:18...
*/
package com.parcel.im.server.handler;
import com.alibaba.fastjson.JSON;
import com.alibaba.fastjson.JSONObject;
import com.parcel.config.message.MessageCode;
import com.parcel.config.message.Msg;
import com.parcel.domain.model.enums.NumericEnum;
import com.parcel.im.server.model.enums.XtEnums;
import com.parcel.im.server.model.message.ConnectResponseMessage;
import com.parcel.im.server.model.message.Message;
import com.parcel.im.server.model.session.SessionFactory;
import com.parcel.support.AutowiredSupport;
import io.netty.channel.ChannelHandler;
import io.netty.channel.ChannelHandlerContext;
import io.netty.channel.SimpleChannelInboundHandler;
import io.netty.handler.codec.http.websocketx.TextWebSocketFrame;
import io.netty.util.AttributeKey;
import lombok.extern.slf4j.Slf4j;
import org.apache.commons.lang3.StringUtils;
/**
* 自定义处理器,只关注 TextWebSocketFrame
*
* @author lixx
* @version 1.0
* @since 2022-01-26 14:18
*/
@Slf4j
@ChannelHandler.Sharable
public class TextWebSocketFrameHandler extends SimpleChannelInboundHandler<TextWebSocketFrame> {
@Override
protected void channelRead0(ChannelHandlerContext ctx, TextWebSocketFrame msg) throws Exception {
String content = msg.text();
System.err.println(content);
JSONObject jsonObject = JSONObject.parseObject(content);
Integer event = jsonObject.getInteger("event");
// 连接消息,就验证 用户的id是否有效
/*String fromId = jsonObject.getString("fromId");
if (StringUtils.isNotBlank(fromId)) {
AttributeKey<Boolean> key = AttributeKey.valueOf(fromId);
Boolean verify = ctx.channel().attr(key).get();
if (null != verify && !verify) {
ctx.channel().writeAndFlush(new TextWebSocketFrame(JSON.toJSONString(new ConnectResponseMessage(false, Msg.msg(MessageCode.VERIFICATION_FAILED), XtEnums.SYSTEM_MSG.getXt(), XtEnums.TEXT_MSG.getXt()))));
return;
}
if (event == Message.ConnectRequestMessage) {
Long fromCount = AutowiredSupport.autowired.sysUserService.isExists(Long.valueOf(fromId));
if (fromCount == NumericEnum.ZERO.getNumeric().longValue()) {
ctx.channel().attr(key).set(false);
ctx.channel().writeAndFlush(new TextWebSocketFrame(JSON.toJSONString(new ConnectResponseMessage(false, Msg.msg(MessageCode.FROM_ID_NOT_EXIST), XtEnums.SYSTEM_MSG.getXt(), XtEnums.TEXT_MSG.getXt()))));
return;
}
String toId = jsonObject.getString("toId");
if (StringUtils.isNotBlank(toId)) {
Long toCount = AutowiredSupport.autowired.sysUserService.isExists(Long.valueOf(toId));
if (toCount == NumericEnum.ZERO.getNumeric().longValue()) {
ctx.channel().attr(key).set(false);
ctx.channel().writeAndFlush(new TextWebSocketFrame(JSON.toJSONString(new ConnectResponseMessage(false, Msg.msg(MessageCode.TO_ID_NOT_EXIST), XtEnums.SYSTEM_MSG.getXt(), XtEnums.TEXT_MSG.getXt()))));
return;
}
}
}
}*/
// 验证通过
Message message = JSONObject.parseObject(content, Message.getMessageClass(event));
message.setContentType(XtEnums.TEXT_MSG.getXt());
ctx.fireChannelRead(message);
}
@Override
public void exceptionCaught(ChannelHandlerContext ctx, Throwable cause) throws Exception {
log.info("{} 异常断开,异常信息 {}", ctx.channel(), cause.getMessage());
SessionFactory.getSession().unbind(ctx.channel());
ctx.channel().close();
}
@Override
public void handlerRemoved(ChannelHandlerContext ctx) throws Exception {
SessionFactory.getSession().unbind(ctx.channel());
}
}