All Downloads are FREE. Search and download functionalities are using the official Maven repository.

xin.alum.aim.handler.AimReceiver Maven / Gradle / Ivy

There is a newer version: 1.9.6
Show newest version
package xin.alum.aim.handler;

import io.netty.handler.codec.http.FullHttpResponse;
import io.netty.handler.codec.http.HttpResponse;
import io.netty.handler.codec.http.HttpResponseStatus;
import xin.alum.aim.config.DataAgreement;
import xin.alum.aim.constant.ChannelAttr;
import xin.alum.aim.constant.ChannelClose;
import xin.alum.aim.constant.ChannelPlatform;
import xin.alum.aim.groups.Session;
import xin.alum.aim.groups.SessionGroups;
import xin.alum.aim.groups.Sessions;
import xin.alum.aim.model.Reply;
import xin.alum.aim.model.Sent;
import xin.alum.aim.model.proto.SentProto;
import xin.alum.aim.util.JSONUtils;
import com.google.protobuf.InvalidProtocolBufferException;
import io.netty.buffer.ByteBuf;
import io.netty.buffer.ByteBufInputStream;
import io.netty.channel.ChannelFuture;
import io.netty.handler.codec.http.FullHttpRequest;

import io.netty.channel.Channel;
import io.netty.util.internal.logging.InternalLogger;
import io.netty.util.internal.logging.InternalLoggerFactory;
import org.springframework.beans.factory.annotation.Autowired;

import java.io.InputStream;

/**
 * 此类为源码测试接受者
 * 正式业务请重写此服务类
 *
 * @auther alum(alum @ live.cn)
 * @date 2021/8/5 10:01
 */
public class AimReceiver implements AimServerReceiver {

    @Autowired
    protected Sessions sessions;

    @Autowired
    protected SessionGroups sessionGroups;

    protected static final InternalLogger logger = InternalLoggerFactory.getInstance(AimReceiver.class);

    /**
     * WebSocket握手处理逻辑,绑定用户信息到会话系统
     *
     * @param ch  当前连接通道
     * @param req 当前上行请求,URL参数已经放入ch中
     * @param res 握手失败 return false 输出给客户端的对象
     * @return
     */
    @Override
    public boolean onHandShake(Channel ch, FullHttpRequest req, FullHttpResponse res) {
        return true;
    }

    /**
     * Socks握手处理逻辑,绑定用户信息到会话系统
     *
     * @param ch 通道
     * @param in 握手数据包
     * @return
     */
    @Override
    public boolean onHandShake(Channel ch, ByteBuf in) {
        return true;
    }

    @Override
    public void onHandShaked(Channel ch) {
        if (sessions != null) {
            sessions.bindUser(ch, ch.id().toString(), ChannelPlatform.NON, "");
        }
    }

    @Override
    public void onPing(Channel ch) {

    }

    /**
     * Text类型数据处理,测试用,正式业务请重写
     *
     * @param ch
     * @param text
     */
    @Override
    public void onText(Channel ch, String text) {
        if (ch.attr(ChannelAttr.AGREEMENT).get() == DataAgreement.Text) {
            ch.writeAndFlush(text);
        } else {
            onRecive(ch, JSONUtils.fromJson(text, Sent.class));
        }
    }


    /**
     * 处理WebSocket binary请求
     * 测试方法,正式业务请重写
     *
     * @param ch
     * @param buf
     * @throws InvalidProtocolBufferException
     */
    @Override
    public void onByte(Channel ch, ByteBuf buf) {
        try {
            if (ch.attr(ChannelAttr.AGREEMENT).get() == DataAgreement.Binary) {
                logger.error("{}{}协议未实现", ch, DataAgreement.Binary);
                return;
            }
            InputStream inputStream = new ByteBufInputStream(buf);
            SentProto.Model model = SentProto.Model.parseFrom(inputStream);
            Sent sent = new Sent();
            sent.setKey(model.getKey());
            sent.setTimestamp(model.getTimestamp());
            sent.putAll(model.getDataMap());
            onRecive(ch, sent);
        } catch (Exception e) {
            logger.error("{} 数据解析异常,{},{}", ch.id(), e.getMessage(), e);
        }
    }

    /**
     * 测试方法,正式业务请重写
     *
     * @param ch
     * @param msg
     */
    @Override
    public void onRecive(Channel ch, Sent msg) {
        Reply reply = new Reply();
        reply.setKey(msg.getKey());
        reply.setCode(0);
        reply.putAll(msg.getData());
        reply.setMessage("回复时间:" + msg.getTimestamp());
        sessions.sends(reply);
    }

    @Override
    public Reply onKick(Channel ch, Session session) {
        logger.warn("用户【{}】通过{}-{}登录", session.getUid(), session.getPlatform(), session.getClientIp());
        return new Reply();
    }

    @Override
    public void onClose(Channel ch, ChannelClose heartbeat) {
        logger.warn("{}因{}而关闭.", ch, heartbeat);
    }

}




© 2015 - 2024 Weber Informatics LLC | Privacy Policy