82 lines
2.8 KiB
Java
82 lines
2.8 KiB
Java
|
|
package com.timeline.user.controller;
|
|||
|
|
|
|||
|
|
import com.timeline.common.response.ResponseEntity;
|
|||
|
|
import com.timeline.common.utils.UserContextUtils;
|
|||
|
|
import com.timeline.user.ws.WsNotifyService;
|
|||
|
|
import lombok.extern.slf4j.Slf4j;
|
|||
|
|
import org.springframework.beans.factory.annotation.Autowired;
|
|||
|
|
import org.springframework.web.bind.annotation.*;
|
|||
|
|
|
|||
|
|
import java.util.Map;
|
|||
|
|
|
|||
|
|
/**
|
|||
|
|
* WebSocket 测试控制器,用于测试消息推送功能
|
|||
|
|
*/
|
|||
|
|
@Slf4j
|
|||
|
|
@RestController
|
|||
|
|
@RequestMapping("/user/ws/test")
|
|||
|
|
public class WebSocketTestController {
|
|||
|
|
|
|||
|
|
@Autowired
|
|||
|
|
private WsNotifyService wsNotifyService;
|
|||
|
|
|
|||
|
|
/**
|
|||
|
|
* 测试推送通知消息给当前用户
|
|||
|
|
*/
|
|||
|
|
@PostMapping("/notify/self")
|
|||
|
|
public ResponseEntity<String> testNotifySelf(@RequestBody Map<String, Object> message) {
|
|||
|
|
String userId = UserContextUtils.getCurrentUserId();
|
|||
|
|
if (userId == null || userId.isEmpty()) {
|
|||
|
|
return ResponseEntity.error(401, "未获取到用户身份");
|
|||
|
|
}
|
|||
|
|
|
|||
|
|
log.info("测试推送通知给用户: {}", userId);
|
|||
|
|
// 确保消息包含必要字段
|
|||
|
|
if (!message.containsKey("timestamp")) {
|
|||
|
|
message.put("timestamp", System.currentTimeMillis());
|
|||
|
|
}
|
|||
|
|
if (!message.containsKey("type")) {
|
|||
|
|
message.put("type", "test");
|
|||
|
|
}
|
|||
|
|
wsNotifyService.sendNotificationToUser(userId, message);
|
|||
|
|
return ResponseEntity.success("通知已推送给用户: " + userId);
|
|||
|
|
}
|
|||
|
|
|
|||
|
|
/**
|
|||
|
|
* 测试推送通知消息给指定用户
|
|||
|
|
*/
|
|||
|
|
@PostMapping("/notify/{targetUserId}")
|
|||
|
|
public ResponseEntity<String> testNotifyUser(
|
|||
|
|
@PathVariable String targetUserId,
|
|||
|
|
@RequestBody Map<String, Object> message) {
|
|||
|
|
log.info("测试推送通知给用户: {}", targetUserId);
|
|||
|
|
wsNotifyService.sendNotificationToUser(targetUserId, message);
|
|||
|
|
return ResponseEntity.success("通知已推送");
|
|||
|
|
}
|
|||
|
|
|
|||
|
|
/**
|
|||
|
|
* 测试推送好友通知
|
|||
|
|
*/
|
|||
|
|
@PostMapping("/friend/{targetUserId}")
|
|||
|
|
public ResponseEntity<String> testFriendNotify(
|
|||
|
|
@PathVariable String targetUserId,
|
|||
|
|
@RequestBody Map<String, Object> message) {
|
|||
|
|
log.info("测试推送好友通知给用户: {}", targetUserId);
|
|||
|
|
wsNotifyService.sendFriendNotifyToAllChannels(targetUserId, message);
|
|||
|
|
return ResponseEntity.success("好友通知已推送");
|
|||
|
|
}
|
|||
|
|
|
|||
|
|
/**
|
|||
|
|
* 测试推送聊天消息
|
|||
|
|
*/
|
|||
|
|
@PostMapping("/chat/{targetUserId}")
|
|||
|
|
public ResponseEntity<String> testChatMessage(
|
|||
|
|
@PathVariable String targetUserId,
|
|||
|
|
@RequestBody Map<String, Object> message) {
|
|||
|
|
log.info("测试推送聊天消息给用户: {}", targetUserId);
|
|||
|
|
wsNotifyService.sendChatMessage(targetUserId, message);
|
|||
|
|
return ResponseEntity.success("聊天消息已推送");
|
|||
|
|
}
|
|||
|
|
}
|
|||
|
|
|