Search in sources :

Example 1 with ImMessageService

use of com.akaxin.site.message.service.ImMessageService in project openzaly by akaxincom.

the class ImMessageHandler method handle.

public CommandResponse handle(Command command) {
    try {
        ChannelSession channelSession = command.getChannelSession();
        String deviceId = channelSession.getDeviceId();
        if (StringUtils.isEmpty(deviceId)) {
            channelSession.getChannel().close();
            logger.error("{} client={} im request error with deviceId={}.", AkxProject.PLN, command.getClientIp(), deviceId);
            return customResponse(ErrorCode2.ERROR);
        }
        ChannelSession acsession = ChannelManager.getChannelSession(deviceId);
        if (acsession == null) {
            channelSession.getChannel().close();
            logger.error("{} client={} im request error with channelSession={}", AkxProject.PLN, command.getClientIp(), acsession);
            return customResponse(ErrorCode2.ERROR);
        }
        if (!checkSiteUserId(command.getSiteUserId(), acsession.getUserId())) {
            channelSession.getChannel().close();
            logger.error("{} client={} im request fail siteUserId={},sessionUserId={}", AkxProject.PLN, command.getClientIp(), command.getSiteUserId(), acsession.getUserId());
            return customResponse(ErrorCode2.ERROR);
        }
        if (RequestAction.IM_CTS_PING.getName().equalsIgnoreCase(command.getAction())) {
            Map<Integer, String> header = new HashMap<Integer, String>();
            header.put(CoreProto.HeaderKey.SITE_SERVER_VERSION_VALUE, CommandConst.SITE_VERSION);
            CoreProto.TransportPackageData.Builder packBuilder = CoreProto.TransportPackageData.newBuilder();
            packBuilder.putAllHeader(header);
            channelSession.getChannel().writeAndFlush(new RedisCommand().add(CommandConst.PROTOCOL_VERSION).add(RequestAction.IM_STC_PONG.getName()).add(packBuilder.build().toByteArray()));
            // 检测是否需要给用户发送PSN
            if (channelSession.detectPsn()) {
                channelSession.getChannel().writeAndFlush(new RedisCommand().add(CommandConst.PROTOCOL_VERSION).add(RequestAction.IM_STC_PSN.getName()).add(packBuilder.build().toByteArray()));
                logger.debug("{} client={} siteUserId={} deviceId={} push psn {} {}", AkxProject.PLN, command.getClientIp(), command.getSiteUserId(), command.getDeviceId(), channelSession.getPsnTime(), channelSession.getSynFinTime());
            }
        } else {
            // 排除ping请求,其他请求走im服务
            new ImMessageService().execute(command);
        }
        return customResponse(ErrorCode2.SUCCESS);
    } catch (Exception e) {
        logger.error(StringHelper.format("{} client={} im request error.", AkxProject.PLN, command.getClientIp()), e);
    }
    return defaultErrorResponse();
}
Also used : ChannelSession(com.akaxin.common.channel.ChannelSession) HashMap(java.util.HashMap) ImMessageService(com.akaxin.site.message.service.ImMessageService) RedisCommand(com.akaxin.common.command.RedisCommand)

Aggregations

ChannelSession (com.akaxin.common.channel.ChannelSession)1 RedisCommand (com.akaxin.common.command.RedisCommand)1 ImMessageService (com.akaxin.site.message.service.ImMessageService)1 HashMap (java.util.HashMap)1