| | |
| | | package com.ruoyi.utils.websocket.service; |
| | | |
| | | import com.alibaba.fastjson2.JSONObject; |
| | | import com.ruoyi.app.position.domain.ArdAppPosition; |
| | | import com.ruoyi.app.position.service.IArdAppPositionService; |
| | | import com.ruoyi.app.position.service.impl.AppPositionPush; |
| | | import com.ruoyi.app.position.service.impl.AppPositionPushService; |
| | | import com.ruoyi.app.position.service.impl.ArdAppPositionServiceImpl; |
| | | import com.ruoyi.common.core.domain.entity.SysUser; |
| | | import com.ruoyi.common.utils.spring.SpringUtils; |
| | | import com.ruoyi.system.service.ISysUserService; |
| | | import com.ruoyi.call.dto.CallMessage; |
| | | import com.ruoyi.utils.websocket.util.WebSocketUtils; |
| | | import lombok.extern.slf4j.Slf4j; |
| | | import org.springframework.beans.factory.annotation.Autowired; |
| | | import org.springframework.stereotype.Component; |
| | | |
| | | import javax.annotation.Resource; |
| | | import javax.websocket.*; |
| | | import javax.websocket.server.PathParam; |
| | | import javax.websocket.server.ServerEndpoint; |
| | | import java.io.IOException; |
| | | import java.util.*; |
| | | import java.util.stream.Collectors; |
| | | import java.util.Map; |
| | | import java.util.regex.Matcher; |
| | | import java.util.regex.Pattern; |
| | | |
| | | import static com.ruoyi.utils.websocket.util.WebSocketUtils.ONLINE_USER_SESSIONS; |
| | | |
| | | /** |
| | | * @ClassName ChatServerEndpoint |
| | |
| | | String message = "用户[" + userId + "] 成功连接!"; |
| | | log.info("用户登录:" + message); |
| | | WebSocketUtils.sendMessage(session, message); |
| | | //region 旧方法 |
| | | // //每个用户连接时,启动初始所有app用户位置发送 |
| | | // AppPositionPush.initDataMap.put(userId,AppPositionPush.getAppPositionList()); |
| | | // AppPositionPush.initPushTask(userId,session,3000); |
| | | //endregion |
| | | } |
| | | |
| | | @OnMessage |
| | | public void onMessage(@PathParam("userId") String userId, String message) { |
| | | log.info("收到消息:" + message); |
| | | Session session = WebSocketUtils.ONLINE_USER_SESSIONS.get(userId); |
| | | WebSocketUtils.sendMessage(session, message); |
| | | // sendMessageAll("用户[" + userid + "] : " + message); |
| | | // Session session = WebSocketUtils.ONLINE_USER_SESSIONS.get(userId); |
| | | // WebSocketUtils.sendMessage(session, message); |
| | | Map<String, Object> messageMap = JSONObject.parseObject(message, Map.class); |
| | | if (((String) messageMap.get("type")).equals("callMessage")) { |
| | | String msg = (String) messageMap.get("message"); |
| | | CallMessage callMessage = JSONObject.parseObject(msg, CallMessage.class); |
| | | if (callMessage != null && callMessage.getType().equals(0)) { |
| | | String regex = "^" + Pattern.quote(callMessage.getTargetId()) + "_\\d+$"; |
| | | Pattern pattern = Pattern.compile(regex); |
| | | ONLINE_USER_SESSIONS.entrySet().stream() |
| | | .filter(entry -> { |
| | | Matcher matcher = pattern.matcher(entry.getKey()); |
| | | return matcher.matches(); |
| | | }) |
| | | .map(Map.Entry::getValue) |
| | | .forEach(session -> WebSocketUtils.sendMessage(session, message)); |
| | | } |
| | | |
| | | } |
| | | // 根据用户新的频率重新调整定时任务 |
| | | AppPositionPushService.messageHandler(userId,message); |
| | | AppPositionPushService.messageHandler(userId, message); |
| | | } |
| | | |
| | | @OnClose |