这块请求都会被重定向到登陆页面
This commit is contained in:
@@ -1,14 +1,21 @@
|
||||
package com.klp.web.controller.websocket;
|
||||
|
||||
import cn.dev33.satoken.annotation.SaIgnore;
|
||||
import com.klp.common.core.controller.BaseController;
|
||||
import com.klp.common.core.domain.R;
|
||||
import com.klp.framework.websocket.WebSocketUsers;
|
||||
import org.springframework.web.bind.annotation.*;
|
||||
|
||||
import javax.websocket.Session;
|
||||
import java.util.List;
|
||||
import java.util.Map;
|
||||
|
||||
/**
|
||||
* WebSocket测试控制器
|
||||
*
|
||||
* @author klp
|
||||
*/
|
||||
@SaIgnore
|
||||
@RestController
|
||||
@RequestMapping("/websocket/test")
|
||||
public class WebSocketTestController extends BaseController {
|
||||
@@ -16,17 +23,75 @@ public class WebSocketTestController extends BaseController {
|
||||
/**
|
||||
* 测试WebSocket端点是否可用
|
||||
*/
|
||||
@SaIgnore
|
||||
@GetMapping("/status")
|
||||
public R<String> getWebSocketStatus() {
|
||||
System.out.println("=== WebSocket状态接口被调用 ===");
|
||||
System.out.println("=== 当前线程: " + Thread.currentThread().getName() + " ===");
|
||||
System.out.println("=== 请求路径: /websocket/test/status ===");
|
||||
return R.ok("WebSocket端点可用,连接地址: ws://localhost:8080/websocket/message");
|
||||
}
|
||||
|
||||
/**
|
||||
* 获取当前连接数
|
||||
*/
|
||||
@SaIgnore
|
||||
@GetMapping("/connections")
|
||||
public R<Integer> getConnectionCount() {
|
||||
// 这里可以调用WebSocketUsers.getUsers().size()来获取实际连接数
|
||||
return R.ok(0);
|
||||
try {
|
||||
int count = WebSocketUsers.getUsers().size();
|
||||
return R.ok(count);
|
||||
} catch (Exception e) {
|
||||
return R.ok(0);
|
||||
}
|
||||
}
|
||||
}
|
||||
|
||||
/**
|
||||
* 发送广播消息
|
||||
*/
|
||||
@SaIgnore
|
||||
@PostMapping("/broadcast")
|
||||
public R<String> sendBroadcast(@RequestParam String message) {
|
||||
try {
|
||||
WebSocketUsers.sendMessageToUsersByText("系统广播: " + message);
|
||||
return R.ok("广播消息发送成功");
|
||||
} catch (Exception e) {
|
||||
return R.fail("广播消息发送失败: " + e.getMessage());
|
||||
}
|
||||
}
|
||||
|
||||
/**
|
||||
* 发送消息给指定用户
|
||||
*/
|
||||
@SaIgnore
|
||||
@PostMapping("/sendToUser")
|
||||
public R<String> sendToUser(@RequestParam String sessionId, @RequestParam String message) {
|
||||
try {
|
||||
Map<String, Session> users = WebSocketUsers.getUsers();
|
||||
Session session = users.get(sessionId);
|
||||
if (session != null) {
|
||||
WebSocketUsers.sendMessageToUserByText(session, "私信: " + message);
|
||||
return R.ok("消息发送成功");
|
||||
} else {
|
||||
return R.fail("用户不在线或会话ID无效");
|
||||
}
|
||||
} catch (Exception e) {
|
||||
return R.fail("消息发送失败: " + e.getMessage());
|
||||
}
|
||||
}
|
||||
|
||||
/**
|
||||
* 获取所有在线用户会话ID
|
||||
*/
|
||||
@SaIgnore
|
||||
@GetMapping("/users")
|
||||
public R<List<String>> getOnlineUsers() {
|
||||
try {
|
||||
Map<String, Session> users = WebSocketUsers.getUsers();
|
||||
List<String> sessionIds = new java.util.ArrayList<>(users.keySet());
|
||||
return R.ok(sessionIds);
|
||||
} catch (Exception e) {
|
||||
return R.ok(new java.util.ArrayList<>());
|
||||
}
|
||||
}
|
||||
}
|
||||
|
||||
@@ -143,6 +143,11 @@ security:
|
||||
# WebSocket路径
|
||||
- /websocket/**
|
||||
- /wms/websocket/**
|
||||
# WebSocket测试接口
|
||||
- /websocket/test/**
|
||||
- /websocket/direct/**
|
||||
# 测试接口
|
||||
- /test/**
|
||||
|
||||
|
||||
# MyBatisPlus配置
|
||||
|
||||
Reference in New Issue
Block a user