| 123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177178179180181182183184185186187188189190191192193194195196197198199200201202203204205206207208209210211212213214215216217218219220221222223224225226227228229230231232233234235236237238239240241242243244245246247248249250251252253254255256257258259260261262263264265266267268269270271272273274275276277278279280281282283284285286287288289290291292293294295296297298299300301302303304305306307308309310311312313314315316317318319320321322323324325326327328329330331332333334335336337338339340341342343344345346347348349350351352353354355356357358359360361362363364365366367368369370371372373374375376377378379380381382383384385386387388389390391392393394395396397398399400401402403404405406407408409410411412413414415416417418419420421422423424425426427428429430431432433434435436437438439440441442443444445446447448449450451452453454 |
- package com.izouma.meta.websocket;
- import com.alibaba.fastjson.JSON;
- import com.alibaba.fastjson.JSONObject;
- import com.izouma.meta.config.Constants;
- import com.izouma.meta.domain.MetaMMOLoginInfo;
- import com.izouma.meta.domain.MetaObjectMoveCoordinate;
- import com.izouma.meta.dto.MMOMessage;
- import com.izouma.meta.dto.MMOSingleMessage;
- import com.izouma.meta.dto.MetaRestResult;
- import com.izouma.meta.dto.MetaServiceResult;
- import com.izouma.meta.enums.MoveType;
- import com.izouma.meta.repo.MetaMMOLoginInfoRepo;
- import com.izouma.meta.utils.ApplicationContextUtil;
- import com.izouma.meta.utils.ObjUtils;
- import lombok.extern.slf4j.Slf4j;
- import org.apache.commons.collections.CollectionUtils;
- import org.apache.commons.lang.StringUtils;
- import org.springframework.data.redis.core.RedisTemplate;
- import org.springframework.stereotype.Service;
- import javax.websocket.*;
- import javax.websocket.server.PathParam;
- import javax.websocket.server.ServerEndpoint;
- import java.time.LocalDateTime;
- import java.util.ArrayList;
- import java.util.List;
- import java.util.Map;
- import java.util.Objects;
- import java.util.concurrent.ConcurrentHashMap;
- import java.util.stream.Collectors;
- @Service
- @ServerEndpoint(value = "/websocket/mmo/{nickName}/{userId}")
- @Slf4j
- public class MMOWebSocket {
- /**
- * 当前在线的客户端map
- */
- private static final Map<String, Session> clients = new ConcurrentHashMap();
- private RedisTemplate redisTemplate;
- private MetaMMOLoginInfoRepo metaMMOLoginInfoRepo;
- private WebsocketCommon websocketCommon;
- private void init() {
- if (Objects.isNull(redisTemplate)) {
- redisTemplate = (RedisTemplate) ApplicationContextUtil.getBean("redisTemplate");
- }
- if (Objects.isNull(metaMMOLoginInfoRepo)) {
- metaMMOLoginInfoRepo = (MetaMMOLoginInfoRepo) ApplicationContextUtil.getBean("metaMMOLoginInfoRepo");
- }
- if (Objects.isNull(websocketCommon)) {
- websocketCommon = (WebsocketCommon) ApplicationContextUtil.getBean("websocketCommon");
- }
- }
- @OnOpen
- public void onOpen(@PathParam("nickName") String nickName, @PathParam("userId") String userId, Session session) {
- init();
- // 判断当前玩家是否在其他地方登陆
- if (clients.containsKey(Constants.REDIS_PREFIX.concat(userId))) {
- log.info(String.format("当前玩家[%S]已经在别处登陆,sessionId为[%S]", userId, session.getId()));
- // 关闭连接
- MMOSingleMessage mmoSingleMessage = new MMOSingleMessage();
- mmoSingleMessage.setMessageType(6);
- mmoSingleMessage.setMessage("您已在其他地方登录,已为您关闭上次登陆信息");
- websocketCommon.sendMessageTo(clients, JSON.toJSONString(mmoSingleMessage), Constants.REDIS_PREFIX.concat(userId));
- try {
- log.info("关闭上次登陆的session连接");
- clients.get(Constants.REDIS_PREFIX.concat(userId)).close();
- } catch (Exception e) {
- log.error("session close throw exception:", e);
- }
- }
- log.info("现在来连接的sessionId:" + session.getId() + "玩家id:" + userId + "玩家昵称" + nickName);
- clients.put(Constants.REDIS_PREFIX.concat(userId), session);
- // 获取上次登录的信息
- MetaMMOLoginInfo dbMetaMMOLoginInfo = metaMMOLoginInfoRepo.findLastByUserId(Long.parseLong(userId));
- // 玩家登陆信息入库
- MetaMMOLoginInfo metaMMOLoginInfo = MetaMMOLoginInfo.initMetaMMOLoginInfo(dbMetaMMOLoginInfo);
- metaMMOLoginInfo.setNickname(nickName);
- metaMMOLoginInfo.setUserId(Long.parseLong(userId));
- metaMMOLoginInfo.setOnLineTime(LocalDateTime.now());
- metaMMOLoginInfo.setSessionId(session.getId());
- MetaMMOLoginInfo save = metaMMOLoginInfoRepo.save(metaMMOLoginInfo);
- MMOMessage mmoMessage = new MMOMessage();
- mmoMessage.setMessageType(5);
- mmoMessage.setMessage(save);
- log.info(String.format("通知玩家[%S],本次登陆信息[%S]", userId, JSON.toJSONString(mmoMessage)));
- websocketCommon.sendMessageTo(clients, JSON.toJSONString(mmoMessage), Constants.REDIS_PREFIX.concat(userId));
- }
- @OnError
- public void onError(Session session, Throwable error) {
- // 异常处理
- log.error(String.format("sessionId[%S]的服务端发生了错误:[%S]", session.getId(), error.getMessage()));
- }
- @OnClose
- public void onClose(@PathParam("userId") String userId) {
- init();
- // 查询地图中玩家信息
- MetaMMOLoginInfo metaMMOLoginInfo = (MetaMMOLoginInfo) redisTemplate.opsForValue().get(Constants.REDIS_PREFIX.concat(userId));
- if (Objects.isNull(metaMMOLoginInfo)) {
- // 如果缓存中玩家信息为空,根据userId和sessionId查询数据库
- MetaMMOLoginInfo dbMetaMMOLoginInfo = metaMMOLoginInfoRepo.findByUserIdAndSessionIdAndDel(Long.parseLong(userId), clients.get(Constants.REDIS_PREFIX.concat(userId)).getId(), false);
- dbMetaMMOLoginInfo.setOffLineTime(LocalDateTime.now());
- // 更新离线时间
- metaMMOLoginInfoRepo.save(dbMetaMMOLoginInfo);
- clients.remove(Constants.REDIS_PREFIX.concat(userId));
- return;
- }
- String key = String.valueOf(metaMMOLoginInfo.getCityId()).concat(":").concat(String.valueOf(metaMMOLoginInfo.getRegionId()));
- List<String> otherUserIds = remove(key, userId);
- if (CollectionUtils.isNotEmpty(otherUserIds)) {
- // 分发下线消息给区域内其他玩家
- buildMessageForSendingToAllOther(otherUserIds, 3, metaMMOLoginInfo);
- }
- MetaMMOLoginInfo dbMetaMMOLoginInfo = metaMMOLoginInfoRepo.findById(metaMMOLoginInfo.getId()).orElse(null);
- if (Objects.isNull(dbMetaMMOLoginInfo)) {
- log.error(String.format("数据库中不存在id[%S]的记录", metaMMOLoginInfo.getId()));
- return;
- }
- ObjUtils.merge(dbMetaMMOLoginInfo, metaMMOLoginInfo);
- dbMetaMMOLoginInfo.setOffLineTime(LocalDateTime.now());
- metaMMOLoginInfoRepo.save(dbMetaMMOLoginInfo);
- clients.remove(Constants.REDIS_PREFIX.concat(userId));
- // 删除redis中自己的信息
- redisTemplate.delete(Constants.REDIS_PREFIX.concat(userId));
- // 移除地图中自己的信息
- redisTemplate.opsForList().remove(Constants.REDIS_PREFIX.concat(key), 0, Constants.REDIS_PREFIX.concat(userId));
- }
- @OnMessage
- public void onMessage(@PathParam("nickName") String nickName, @PathParam("userId") String userId, String message, Session session) {
- init();
- if (StringUtils.isBlank(message)) {
- log.error("Illegal parameter : message can not be null");
- return;
- }
- if (Constants.HEART_RECEIVE.equals(message)) {
- log.info(String.format("sessionId:[%S] userId:[%S] 连接正常", session.getId(), userId));
- websocketCommon.sendMessageTo(clients, Constants.HEART_RETURN, Constants.REDIS_PREFIX.concat(userId));
- return;
- }
- JSONObject jsonObject = JSON.parseObject(message);
- MetaServiceResult result = checkParams(jsonObject);
- if (!result.isSuccess()) {
- log.error(result.getMessage());
- return;
- }
- log.info("来自客户端消息:" + message + "客户端的id是:" + session.getId());
- int type = Integer.parseInt(jsonObject.getString("type"));
- String cityId = jsonObject.getString("cityId");
- String regionId = jsonObject.getString("regionId");
- String key = cityId.concat(":").concat(regionId);
- List<String> otherUserIds = remove(key, userId);
- if (type == 9) {
- String objectId = jsonObject.getString("objectId");
- MetaObjectMoveCoordinate metaObjectMoveCoordinate = null;
- try {
- String str = websocketCommon.getRequest("/metaObjectMove/".concat(objectId).concat("/queryCoordinate"));
- metaObjectMoveCoordinate = JSONObject.parseObject(str, MetaObjectMoveCoordinate.class);
- } catch (Exception e) {
- String errMsg = String.format("MetaObjectMoveCoordinate JSON parse throw exception:[%S]", e);
- log.error(errMsg);
- }
- if (Objects.isNull(metaObjectMoveCoordinate)) {
- log.error(String.format("物体[%S]坐标不存在", objectId));
- return;
- }
- buildMessageForSendingToAllOther(otherUserIds, 9, metaObjectMoveCoordinate);
- return;
- }
- MetaMMOLoginInfo metaMMOLoginInfo;
- List<MetaMMOLoginInfo> metaMMOLoginInfos = redisTemplate.opsForValue().multiGet(otherUserIds);
- switch (type) {
- case 1:
- log.info("当前操作类型为1 -> 玩家进入地图");
- metaMMOLoginInfo = buildMetaMMOLoginInfo(jsonObject, Long.parseLong(cityId), Long.parseLong(regionId), nickName, userId);
- if (CollectionUtils.isNotEmpty(otherUserIds)) {
- if (CollectionUtils.isNotEmpty(metaMMOLoginInfos)) {
- // 分发区域内其他玩家信息给自己
- buildMessageForSendingToUser(Constants.REDIS_PREFIX.concat(userId), 1, metaMMOLoginInfos);
- // 分发自己信息给区域内其他玩家
- buildMessageForSendingToAllOther(otherUserIds, 4, metaMMOLoginInfo);
- }
- }
- // 将自己信息存到redis中
- redisTemplate.opsForValue().set(Constants.REDIS_PREFIX.concat(userId), metaMMOLoginInfo);
- redisTemplate.opsForList().leftPush(Constants.REDIS_PREFIX.concat(key), Constants.REDIS_PREFIX.concat(userId));
- break;
- case 2:
- log.info(String.format("当前操作类型为[%S] -> 玩家切换区域", type));
- metaMMOLoginInfo = buildMetaMMOLoginInfo(jsonObject, Long.parseLong(cityId), Long.parseLong(regionId), nickName, userId);
- if (CollectionUtils.isNotEmpty(otherUserIds)) {
- // 分发自己信息给区域内其他玩家
- buildMessageForSendingToAllOther(otherUserIds, 4, metaMMOLoginInfo);
- // 分发区域内其他玩家信息给自己
- buildMessageForSendingToUser(Constants.REDIS_PREFIX.concat(userId), 1, metaMMOLoginInfos);
- }
- MetaMMOLoginInfo oldMetaMMOLoginInfo = (MetaMMOLoginInfo) redisTemplate.opsForValue().get(Constants.REDIS_PREFIX.concat(userId));
- if (Objects.isNull(oldMetaMMOLoginInfo)) {
- log.error("缺失玩家上次区域的地图缓存信息");
- break;
- }
- String oldKey = String.valueOf(oldMetaMMOLoginInfo.getCityId()).concat(":").concat(String.valueOf(oldMetaMMOLoginInfo.getRegionId()));
- List<String> oldUserIds = redisTemplate.opsForList().range(Constants.REDIS_PREFIX.concat(oldKey), 0, -1);
- if (CollectionUtils.isEmpty(oldUserIds)) {
- log.error("查询不到上次所进入的区域的玩家信息");
- break;
- }
- // 分发消息给之前区域的玩家
- buildMessageForSendingToAllOther(oldUserIds, 3, oldMetaMMOLoginInfo);
- // 清除玩家上次缓存的地图信息
- redisTemplate.opsForList().remove(Constants.REDIS_PREFIX.concat(oldKey), 0, Constants.REDIS_PREFIX.concat(userId));
- // 缓存玩家新的地图信息
- redisTemplate.opsForValue().set(Constants.REDIS_PREFIX.concat(userId), metaMMOLoginInfo);
- redisTemplate.opsForList().leftPush(Constants.REDIS_PREFIX.concat(key), Constants.REDIS_PREFIX.concat(userId));
- break;
- case 3:
- log.info(String.format("当前操作类型为[%S] -> 玩家在地图内移动", type));
- metaMMOLoginInfo = (MetaMMOLoginInfo) redisTemplate.opsForValue().get(Constants.REDIS_PREFIX.concat(userId));
- if (Objects.isNull(metaMMOLoginInfo)) {
- log.error("缓存中不存在本玩家信息");
- break;
- }
- // 更新玩家位置信息
- buildCommonProperty(metaMMOLoginInfo, jsonObject);
- // 分发玩家信息给区域内其他玩家
- buildMessageForSendingToAllOther(otherUserIds, 2, metaMMOLoginInfo);
- redisTemplate.opsForValue().set(Constants.REDIS_PREFIX.concat(userId), metaMMOLoginInfo);
- break;
- case 4:
- log.info(String.format("当前操作类型为[%S] -> 玩家进入大厅", type));
- metaMMOLoginInfo = (MetaMMOLoginInfo) redisTemplate.opsForValue().get(Constants.REDIS_PREFIX.concat(userId));
- if (Objects.isNull(metaMMOLoginInfo)) {
- log.error("缓存中不存在本玩家信息");
- break;
- }
- // 分发玩家信息给区域内其他玩家
- buildMessageForSendingToAllOther(otherUserIds, 3, metaMMOLoginInfo);
- // 更新库中玩家位置信息
- MetaMMOLoginInfo dbMetaMMOLoginInfo = metaMMOLoginInfoRepo.findById(metaMMOLoginInfo.getId()).orElse(null);
- if (Objects.isNull(dbMetaMMOLoginInfo)) {
- log.error(String.format("数据库不存在id[%S]的记录", metaMMOLoginInfo.getId()));
- break;
- }
- dbMetaMMOLoginInfo.setAxisX(metaMMOLoginInfo.getAxisX());
- dbMetaMMOLoginInfo.setAxisY(metaMMOLoginInfo.getAxisY());
- dbMetaMMOLoginInfo.setAxisZ(metaMMOLoginInfo.getAxisZ());
- dbMetaMMOLoginInfo.setEulerX(metaMMOLoginInfo.getEulerX());
- dbMetaMMOLoginInfo.setEulerY(metaMMOLoginInfo.getEulerY());
- dbMetaMMOLoginInfo.setEulerZ(metaMMOLoginInfo.getEulerZ());
- dbMetaMMOLoginInfo.setCityId(metaMMOLoginInfo.getCityId());
- dbMetaMMOLoginInfo.setRegionId(metaMMOLoginInfo.getRegionId());
- dbMetaMMOLoginInfo.setTop(metaMMOLoginInfo.getTop());
- dbMetaMMOLoginInfo.setHat(metaMMOLoginInfo.getHat());
- dbMetaMMOLoginInfo.setShoes(metaMMOLoginInfo.getShoes());
- dbMetaMMOLoginInfo.setDown(metaMMOLoginInfo.getDown());
- dbMetaMMOLoginInfo.setEmoji(metaMMOLoginInfo.getEmoji());
- dbMetaMMOLoginInfo.setAnim(metaMMOLoginInfo.getAnim());
- dbMetaMMOLoginInfo.setRole(metaMMOLoginInfo.getRole());
- dbMetaMMOLoginInfo.setMoveType(metaMMOLoginInfo.getMoveType());
- dbMetaMMOLoginInfo.setTurnedId(metaMMOLoginInfo.getTurnedId());
- dbMetaMMOLoginInfo.setVehicleId(metaMMOLoginInfo.getVehicleId());
- metaMMOLoginInfoRepo.save(dbMetaMMOLoginInfo);
- break;
- default:
- log.error(String.format("不存在的操作类型[%S]", type));
- }
- }
- /**
- * 分发玩家信息给区域内其他玩家
- *
- * @param userIds 玩家id集合
- * @param messageType 消息类型
- * @param data 消息体
- */
- private void buildMessageForSendingToAllOther(List<String> userIds, int messageType, Object data) {
- MMOMessage mmoMessage = new MMOMessage();
- mmoMessage.setMessageType(messageType);
- mmoMessage.setMessage(data);
- // 进行类型转换和判断
- if (data instanceof MetaMMOLoginInfo) {
- MetaMMOLoginInfo metaMMOLoginInfo = (MetaMMOLoginInfo) data;
- if (!clients.containsKey(Constants.REDIS_PREFIX.concat(String.valueOf(metaMMOLoginInfo.getUserId())))) {
- log.error("session信息不存在");
- return;
- }
- }
- userIds.forEach(id -> {
- try {
- log.info(String.format("服务器给所有当前区域内在线用户发送消息,当前在线人员为[%S]。消息:[%S]", id, JSON.toJSONString(mmoMessage)));
- clients.get(id).getBasicRemote().sendText(JSON.toJSONString(mmoMessage));
- } catch (Exception e) {
- log.error(String.format("send message [%S] to [%S] throw exception [%S]:", JSON.toJSONString(mmoMessage), id, e));
- }
- });
- }
- /**
- * 分发区域内其他玩家信息给自己
- *
- * @param userId 玩家id
- * @param messageType 消息类型
- * @param metaMMOLoginInfos 消息体(其他玩家信息)
- */
- private void buildMessageForSendingToUser(String userId, int messageType, List<MetaMMOLoginInfo> metaMMOLoginInfos) {
- MMOMessage mmoMessage = new MMOMessage();
- mmoMessage.setMessageType(messageType);
- mmoMessage.setMap(metaMMOLoginInfos);
- websocketCommon.sendMessageTo(clients, JSON.toJSONString(mmoMessage), userId);
- }
- /**
- * 构建玩家登陆信息
- *
- * @param jsonObject 玩家位置等信息json对象
- * @param cityId 城市id
- * @param regionId 区域id
- * @return 玩家位置信息
- */
- private MetaMMOLoginInfo buildMetaMMOLoginInfo(JSONObject jsonObject, Long cityId, Long regionId, String nickName, String userId) {
- // 获取到进入地图时自己的信息
- MetaMMOLoginInfo metaMMOLoginInfo = new MetaMMOLoginInfo();
- buildCommonProperty(metaMMOLoginInfo, jsonObject);
- metaMMOLoginInfo.setCityId(cityId);
- metaMMOLoginInfo.setRegionId(regionId);
- metaMMOLoginInfo.setUserId(Long.parseLong(userId));
- metaMMOLoginInfo.setNickname(nickName);
- if (Objects.nonNull(jsonObject.getString("role"))) {
- metaMMOLoginInfo.setRole(jsonObject.getString("role"));
- }
- if (Objects.nonNull(jsonObject.getString("id"))) {
- metaMMOLoginInfo.setId(Long.parseLong(jsonObject.getString("id")));
- }
- metaMMOLoginInfo.setCityId(cityId);
- return metaMMOLoginInfo;
- }
- /**
- * 根据jsonObject构建玩家位置信息
- *
- * @param metaMMOLoginInfo 玩家登陆信息
- * @param jsonObject 玩家位置信息json对象
- */
- private void buildCommonProperty(MetaMMOLoginInfo metaMMOLoginInfo, JSONObject jsonObject) {
- if (Objects.nonNull(jsonObject.getString("axisX"))) {
- metaMMOLoginInfo.setAxisX(Float.parseFloat(jsonObject.getString("axisX")));
- }
- if (Objects.nonNull(jsonObject.getString("axisY"))) {
- metaMMOLoginInfo.setAxisY(Float.parseFloat(jsonObject.getString("axisY")));
- }
- if (Objects.nonNull(jsonObject.getString("axisZ"))) {
- metaMMOLoginInfo.setAxisZ(Float.parseFloat(jsonObject.getString("axisZ")));
- }
- if (Objects.nonNull(jsonObject.getString("eulerX"))) {
- metaMMOLoginInfo.setEulerX(Float.parseFloat(jsonObject.getString("eulerX")));
- }
- if (Objects.nonNull(jsonObject.getString("eulerY"))) {
- metaMMOLoginInfo.setEulerY(Float.parseFloat(jsonObject.getString("eulerY")));
- }
- if (Objects.nonNull(jsonObject.getString("eulerZ"))) {
- metaMMOLoginInfo.setEulerZ(Float.parseFloat(jsonObject.getString("eulerZ")));
- }
- if (Objects.nonNull(jsonObject.getString("top"))) {
- metaMMOLoginInfo.setTop(Integer.parseInt(jsonObject.getString("top")));
- }
- if (Objects.nonNull(jsonObject.getString("hat"))) {
- metaMMOLoginInfo.setHat(Integer.parseInt(jsonObject.getString("hat")));
- }
- if (Objects.nonNull(jsonObject.getString("down"))) {
- metaMMOLoginInfo.setDown(Integer.parseInt(jsonObject.getString("down")));
- }
- if (Objects.nonNull(jsonObject.getString("shoes"))) {
- metaMMOLoginInfo.setShoes(Integer.parseInt(jsonObject.getString("shoes")));
- }
- if (Objects.nonNull(jsonObject.getString("anim"))) {
- metaMMOLoginInfo.setAnim(Integer.parseInt(jsonObject.getString("anim")));
- }
- if (Objects.nonNull(jsonObject.getString("emoji"))) {
- metaMMOLoginInfo.setEmoji(Integer.parseInt(jsonObject.getString("emoji")));
- }
- if (Objects.nonNull(jsonObject.getString("moveType"))) {
- metaMMOLoginInfo.setMoveType(MoveType.valueOf(jsonObject.getString("moveType")));
- }
- if (Objects.nonNull(jsonObject.getString("vehicleId"))) {
- metaMMOLoginInfo.setVehicleId(Long.parseLong(jsonObject.getString("vehicleId")));
- }
- if (Objects.nonNull(jsonObject.getString("turnedId"))) {
- metaMMOLoginInfo.setTurnedId(Long.parseLong(jsonObject.getString("turnedId")));
- }
- }
- /**
- * 校验参数
- *
- * @param jsonObject 参数
- * @return 校验结果
- */
- private MetaServiceResult checkParams(JSONObject jsonObject) {
- if (Objects.isNull(jsonObject)) {
- return MetaServiceResult.returnError("Illegal parameter : jsonObject can not be null");
- }
- if (Objects.isNull(jsonObject.getString("type"))) {
- return MetaServiceResult.returnError("Illegal parameter : type can not be null");
- }
- if (Objects.isNull(jsonObject.getString("cityId"))) {
- return MetaServiceResult.returnError("Illegal parameter : cityId can not be null");
- }
- if (Objects.isNull(jsonObject.getString("regionId"))) {
- return MetaServiceResult.returnError("Illegal parameter : regionId can not be null");
- }
- int type = Integer.parseInt(jsonObject.getString("type"));
- if (type == 9) {
- if (Objects.isNull(jsonObject.getString("objectId"))) {
- return MetaServiceResult.returnError("Illegal parameter : objectId can not be null");
- }
- return MetaServiceResult.returnSuccess("check success");
- }
- if (Objects.isNull(jsonObject.getString("id"))) {
- return MetaServiceResult.returnError("Illegal parameter : id can not be null");
- }
- return MetaServiceResult.returnSuccess("check success");
- }
- private List<String> remove(String key, String userId) {
- List<String> userIds = redisTemplate.opsForList().range(Constants.REDIS_PREFIX.concat(key), 0, -1);
- // 去除当前玩家id
- List<String> otherUserIds = new ArrayList<>();
- if (CollectionUtils.isNotEmpty(userIds)) {
- otherUserIds = userIds.stream().filter(id -> !Objects.equals(id, Constants.REDIS_PREFIX.concat(userId))).collect(Collectors.toList());
- }
- return otherUserIds;
- }
- public MetaRestResult<Void> clearMMO(String userId, String regionId, String cityId) {
- init();
- if (clients.containsKey(Constants.REDIS_PREFIX.concat(userId))) {
- String errMsg = String.format("userId[%S]用户mmo连接正常,无法清除缓存!", userId);
- log.info(errMsg);
- return MetaRestResult.returnError(errMsg);
- }
- String key = cityId.concat(":").concat(regionId);
- redisTemplate.delete(Constants.REDIS_PREFIX.concat(userId));
- redisTemplate.opsForList().remove(Constants.REDIS_PREFIX.concat(key), 0, Constants.REDIS_PREFIX.concat(userId));
- return MetaRestResult.returnSuccess("清除成功");
- }
- }
|