| | |
| | | package com.xcong.excoin.netty.logic; |
| | | |
| | | import cn.hutool.core.util.StrUtil; |
| | | import cn.hutool.crypto.asymmetric.KeyType; |
| | | import cn.hutool.crypto.asymmetric.RSA; |
| | | import com.alibaba.fastjson.JSONObject; |
| | | import com.xcong.excoin.common.contants.AppContants; |
| | | import com.xcong.excoin.configurations.properties.SecurityProperties; |
| | | import com.xcong.excoin.modules.member.entity.MemberEntity; |
| | | import com.xcong.excoin.netty.bean.ChatRequest; |
| | | import com.xcong.excoin.netty.bean.RequestBean; |
| | | import com.xcong.excoin.netty.bean.ResponseBean; |
| | | import com.xcong.excoin.netty.common.ChannelManager; |
| | | import com.xcong.excoin.netty.common.NettyTools; |
| | | import com.xcong.excoin.rabbit.producer.ChatProducer; |
| | | import com.xcong.excoin.utils.RedisUtils; |
| | | import io.netty.channel.Channel; |
| | | import lombok.extern.slf4j.Slf4j; |
| | | import org.springframework.beans.factory.annotation.Autowired; |
| | | import org.springframework.stereotype.Component; |
| | | |
| | | |
| | |
| | | * @email wangdoubleone@gmail.com |
| | | * @date 2019-05-09 |
| | | */ |
| | | @Slf4j |
| | | @Component |
| | | public class WebSocketLogic { |
| | | |
| | | @Autowired |
| | | private RedisUtils redisUtils; |
| | | @Autowired |
| | | private ChatProducer chatProducer; |
| | | @Autowired |
| | | private SecurityProperties securityProperties; |
| | | |
| | | public void webReqConnection(RequestBean requestBean) { |
| | | public void authCheck(RequestBean requestBean) { |
| | | Channel channel = ChannelManager.findWebSocketChannel(requestBean.getChannelId()); |
| | | channel.writeAndFlush(NettyTools.webSocketBytes("this is ok")); |
| | | } |
| | | ResponseBean responseBean = new ResponseBean(); |
| | | responseBean.setType(requestBean.getType()); |
| | | responseBean.setStatus(1); |
| | | |
| | | public void reqHomeSymbols(RequestBean requestBean) { |
| | | String params = requestBean.getParams(); |
| | | JSONObject jsonObject = JSONObject.parseObject(params); |
| | | String token = jsonObject.getString("token"); |
| | | String type = jsonObject.getString("type"); |
| | | ResponseBean responseBean = ResponseBean.ok(requestBean.getType(), null); |
| | | String token = requestBean.getData().toString(); |
| | | String redisKey = AppContants.APP_LOGIN_PREFIX + token; |
| | | String loginStr = redisUtils.getString(redisKey); |
| | | if (StrUtil.isBlank(loginStr)) { |
| | | ResponseBean res = ResponseBean.fail(); |
| | | res.setType(requestBean.getType()); |
| | | channel.writeAndFlush(NettyTools.webSocketBytes(JSONObject.toJSONString(res))); |
| | | return; |
| | | } |
| | | |
| | | Channel channel = ChannelManager.findWebSocketChannel(requestBean.getChannelId()); |
| | | MemberEntity loginUser = JSONObject.parseObject(loginStr, MemberEntity.class); |
| | | Long memberId = loginUser.getId(); |
| | | |
| | | channel.writeAndFlush(NettyTools.webSocketBytes(JSONObject.toJSONString(responseBean))); |
| | | ChannelManager.addWsChannel(channel, memberId); |
| | | String value = redisUtils.getString(AppContants.MSG_NOTICE + memberId); |
| | | if (StrUtil.isNotBlank(value)) { |
| | | responseBean.setType(3); |
| | | responseBean.setData(Integer.parseInt(value)); |
| | | channel.writeAndFlush(NettyTools.webSocketBytes(JSONObject.toJSONString(responseBean))); |
| | | |
| | | redisUtils.del(AppContants.MSG_NOTICE + memberId); |
| | | } |
| | | } |
| | | |
| | | public void defaultReq(RequestBean requestBean) { |
| | | public void sendMsg(RequestBean requestBean) { |
| | | String chatStr = requestBean.getData().toString(); |
| | | log.info("接收到的消息:{}", chatStr); |
| | | ChatRequest chat = JSONObject.parseObject(chatStr, ChatRequest.class); |
| | | |
| | | Channel channel = ChannelManager.findWebSocketChannel(requestBean.getChannelId()); |
| | | channel.writeAndFlush("this is error type"); |
| | | ResponseBean res = ResponseBean.ok(chat); |
| | | chat.setTimestamp(System.currentTimeMillis()); |
| | | res.setType(2); |
| | | channel.writeAndFlush(NettyTools.webSocketJson(res)); |
| | | |
| | | // 判断是否在线 |
| | | Channel targetChannel = ChannelManager.findWsChannel(chat.getTargetId()); |
| | | if (targetChannel != null) { |
| | | chat.setIsSelf(2); |
| | | ResponseBean toRes = ResponseBean.ok(chat); |
| | | toRes.setType(2); |
| | | targetChannel.writeAndFlush(NettyTools.webSocketJson(toRes)); |
| | | } else { |
| | | // 在redis中保存用户未在线时,给该用户发送的消息条数 |
| | | // String key = AppContants.MSG_NOTICE + chat.getTargetId(); |
| | | // String value = redisUtils.getString(key); |
| | | // if (StrUtil.isEmpty(value)) { |
| | | // redisUtils.set(key , 1); |
| | | // } else { |
| | | // redisUtils.set(key, Integer.parseInt(value) + 1); |
| | | // } |
| | | } |
| | | chatProducer.sendMsgHistory(chat); |
| | | } |
| | | } |