package com.ruoyi.web.controller.webSocket;
|
|
import lombok.extern.slf4j.Slf4j;
|
import org.springframework.beans.factory.annotation.Autowired;
|
import org.springframework.stereotype.Service;
|
|
import java.util.*;
|
|
/**
|
* 基于AppUser的WebSocket服务类
|
* 提供按用户发送消息的接口
|
*/
|
@Service
|
@Slf4j
|
public class WebSocketUserService {
|
|
@Autowired
|
private WebSocketUserConnectionManager userConnectionManager;
|
|
/**
|
* 向指定用户的所有设备发送消息
|
* @param appUserId 用户ID
|
* @param message 消息内容
|
* @return 成功发送的设备数
|
*/
|
public int sendMessageToUser(String appUserId, String message) {
|
log.info("准备向用户 {} 发送消息: {}", appUserId, message);
|
return userConnectionManager.sendMessageToUser(appUserId, message);
|
}
|
|
/**
|
* 向指定用户发送JSON格式消息
|
* @param appUserId 用户ID
|
* @param messageType 消息类型
|
* @param data 消息数据
|
* @return 成功发送的设备数
|
*/
|
public int sendJsonMessageToUser(String appUserId, String messageType, Object data) {
|
String jsonMessage = buildJsonMessage(messageType, data, appUserId);
|
return sendMessageToUser(appUserId, jsonMessage);
|
}
|
|
/**
|
* 向多个用户发送消息
|
* @param appUserIds 用户ID列表
|
* @param message 消息内容
|
* @return 成功发送的用户数
|
*/
|
public int sendMessageToUsers(List<String> appUserIds, String message) {
|
log.info("准备向 {} 个用户发送消息: {}", appUserIds.size(), message);
|
return userConnectionManager.sendMessageToUsers(appUserIds, message);
|
}
|
|
/**
|
* 向多个用户发送JSON格式消息
|
* @param appUserIds 用户ID列表
|
* @param messageType 消息类型
|
* @param data 消息数据
|
* @return 成功发送的用户数
|
*/
|
public int sendJsonMessageToUsers(List<String> appUserIds, String messageType, Object data) {
|
String jsonMessage = buildJsonMessage(messageType, data, null);
|
return sendMessageToUsers(appUserIds, jsonMessage);
|
}
|
|
/**
|
* 向指定连接发送消息
|
* @param channelId 连接ID
|
* @param message 消息内容
|
* @return 是否发送成功
|
*/
|
public boolean sendMessageToChannel(String channelId, String message) {
|
log.info("准备向连接 {} 发送消息: {}", channelId, message);
|
return userConnectionManager.sendMessageToChannel(channelId, message);
|
}
|
|
/**
|
* 广播消息到所有连接
|
* @param message 消息内容
|
* @return 成功发送的连接数
|
*/
|
public int broadcastMessage(String message) {
|
log.info("准备广播消息: {}", message);
|
return userConnectionManager.broadcastMessage(message);
|
}
|
|
/**
|
* 广播JSON格式消息到所有连接
|
* @param messageType 消息类型
|
* @param data 消息数据
|
* @return 成功发送的连接数
|
*/
|
public int broadcastJsonMessage(String messageType, Object data) {
|
String jsonMessage = buildJsonMessage(messageType, data, null);
|
return broadcastMessage(jsonMessage);
|
}
|
|
/**
|
* 发送系统通知消息
|
* @param message 通知内容
|
* @return 成功发送的连接数
|
*/
|
public int sendSystemNotification(String message) {
|
Map<String, Object> data = new HashMap<>();
|
data.put("message", message);
|
return sendJsonMessageToAllUsers("system_notification", data);
|
}
|
|
/**
|
* 向所有在线用户发送系统通知
|
* @param messageType 消息类型
|
* @param data 消息数据
|
* @return 成功发送的连接数
|
*/
|
public int sendJsonMessageToAllUsers(String messageType, Object data) {
|
Set<String> onlineUserIds = userConnectionManager.getOnlineUserIds();
|
if (onlineUserIds.isEmpty()) {
|
log.warn("没有在线用户,无法发送消息");
|
return 0;
|
}
|
return sendJsonMessageToUsers(new ArrayList<>(onlineUserIds), messageType, data);
|
}
|
|
/**
|
* 发送任务分配消息
|
* @param taskId 任务ID
|
* @param taskName 任务名称
|
* @param assignee 分配人
|
* @param assigneeUserId 分配人用户ID
|
* @return 成功发送的连接数
|
*/
|
public int sendTaskAssignment(String taskId, String taskName, String assignee, String assigneeUserId) {
|
Map<String, Object> taskData = new HashMap<>();
|
taskData.put("taskId", taskId);
|
taskData.put("taskName", taskName);
|
taskData.put("assignee", assignee);
|
taskData.put("assigneeUserId", assigneeUserId);
|
taskData.put("timestamp", System.currentTimeMillis());
|
|
if (assigneeUserId != null) {
|
// 发送给指定用户
|
return sendJsonMessageToUser(assigneeUserId, "task_assignment", taskData);
|
} else {
|
// 广播给所有用户
|
return broadcastJsonMessage("task_assignment", taskData);
|
}
|
}
|
|
/**
|
* 发送状态更新消息
|
* @param status 状态
|
* @param details 详细信息
|
* @param targetUserId 目标用户ID,为null时广播
|
* @return 成功发送的连接数
|
*/
|
public int sendStatusUpdate(String status, String details, String targetUserId) {
|
Map<String, Object> statusData = new HashMap<>();
|
statusData.put("status", status);
|
statusData.put("details", details);
|
statusData.put("timestamp", System.currentTimeMillis());
|
|
if (targetUserId != null) {
|
// 发送给指定用户
|
return sendJsonMessageToUser(targetUserId, "status_update", statusData);
|
} else {
|
// 广播给所有用户
|
return broadcastJsonMessage("status_update", statusData);
|
}
|
}
|
|
/**
|
* 发送任务状态更新消息
|
* @param taskId 任务ID
|
* @param taskStatus 任务状态
|
* @param message 消息内容
|
* @param targetUserId 目标用户ID,为null时广播
|
* @return 成功发送的连接数
|
*/
|
public int sendTaskStatusUpdate(String taskId, String taskStatus, String message, String targetUserId) {
|
Map<String, Object> taskStatusData = new HashMap<>();
|
taskStatusData.put("taskId", taskId);
|
taskStatusData.put("taskStatus", taskStatus);
|
taskStatusData.put("message", message);
|
taskStatusData.put("timestamp", System.currentTimeMillis());
|
|
if (targetUserId != null) {
|
// 发送给指定用户
|
return sendJsonMessageToUser(targetUserId, "task_status_update", taskStatusData);
|
} else {
|
// 广播给所有用户
|
return broadcastJsonMessage("task_status_update", taskStatusData);
|
}
|
}
|
|
/**
|
* 发送位置更新消息
|
* @param userId 用户ID
|
* @param longitude 经度
|
* @param latitude 纬度
|
* @param address 地址
|
* @return 成功发送的连接数
|
*/
|
public int sendLocationUpdate(String userId, String longitude, String latitude, String address) {
|
Map<String, Object> locationData = new HashMap<>();
|
locationData.put("longitude", longitude);
|
locationData.put("latitude", latitude);
|
locationData.put("address", address != null ? address : "");
|
locationData.put("timestamp", System.currentTimeMillis());
|
|
return sendJsonMessageToUser(userId, "location_update", locationData);
|
}
|
|
/**
|
* 获取用户连接信息
|
* @param appUserId 用户ID
|
* @return 连接信息
|
*/
|
public Map<String, Object> getUserConnectionInfo(String appUserId) {
|
Map<String, Object> info = new HashMap<>();
|
info.put("appUserId", appUserId);
|
info.put("isOnline", userConnectionManager.isUserOnline(appUserId));
|
info.put("connectionCount", userConnectionManager.getUserConnectionCount(appUserId));
|
return info;
|
}
|
|
/**
|
* 获取所有连接统计信息
|
* @return 统计信息
|
*/
|
public Map<String, Object> getConnectionStats() {
|
return userConnectionManager.getConnectionStats();
|
}
|
|
/**
|
* 检查用户是否在线
|
* @param appUserId 用户ID
|
* @return 是否在线
|
*/
|
public boolean isUserOnline(String appUserId) {
|
return userConnectionManager.isUserOnline(appUserId);
|
}
|
|
/**
|
* 获取在线用户列表
|
* @return 在线用户ID列表
|
*/
|
public Set<String> getOnlineUserIds() {
|
return userConnectionManager.getOnlineUserIds();
|
}
|
|
/**
|
* 获取总连接数
|
* @return 总连接数
|
*/
|
public int getTotalConnectionCount() {
|
return userConnectionManager.getTotalConnectionCount();
|
}
|
|
/**
|
* 获取在线用户数
|
* @return 在线用户数
|
*/
|
public int getOnlineUserCount() {
|
return userConnectionManager.getOnlineUserCount();
|
}
|
|
/**
|
* 构建JSON格式消息
|
* @param messageType 消息类型
|
* @param data 消息数据
|
* @param targetUserId 目标用户ID
|
* @return JSON字符串
|
*/
|
private String buildJsonMessage(String messageType, Object data, String targetUserId) {
|
Map<String, Object> message = new HashMap<>();
|
message.put("type", messageType);
|
message.put("data", data);
|
message.put("timestamp", System.currentTimeMillis());
|
|
if (targetUserId != null) {
|
message.put("targetUserId", targetUserId);
|
}
|
|
return com.alibaba.fastjson.JSON.toJSONString(message);
|
}
|
}
|