优化websocket连接
This commit is contained in:
@@ -0,0 +1,27 @@
|
||||
package com.onekeycall.videotablet.controller.sn;
|
||||
|
||||
import com.onekeycall.videotablet.entity.DeviceInfo;
|
||||
import com.onekeycall.videotablet.result.Result;
|
||||
import com.onekeycall.videotablet.service.DeviceSnService;
|
||||
import org.springframework.beans.factory.annotation.Autowired;
|
||||
import org.springframework.web.bind.annotation.PostMapping;
|
||||
import org.springframework.web.bind.annotation.RequestMapping;
|
||||
import org.springframework.web.bind.annotation.RequestParam;
|
||||
import org.springframework.web.bind.annotation.RestController;
|
||||
|
||||
@RestController
|
||||
@RequestMapping("/sn")
|
||||
public class DevicesInfoController {
|
||||
@Autowired
|
||||
private DeviceSnService deviceSnService;
|
||||
|
||||
@PostMapping("/update_device_info")
|
||||
public Result update_device_info(
|
||||
@RequestParam String sn,
|
||||
@RequestParam(required = false) String device_alias,
|
||||
@RequestParam(required = false) String device_model
|
||||
) {
|
||||
DeviceInfo deviceInfo = deviceSnService.findBySn(sn);
|
||||
return Result.ok();
|
||||
}
|
||||
}
|
||||
@@ -2,12 +2,15 @@ package com.onekeycall.videotablet.handler;
|
||||
|
||||
import lombok.extern.slf4j.Slf4j;
|
||||
import org.springframework.stereotype.Component;
|
||||
import org.springframework.util.MultiValueMap;
|
||||
import org.springframework.web.socket.CloseStatus;
|
||||
import org.springframework.web.socket.TextMessage;
|
||||
import org.springframework.web.socket.WebSocketSession;
|
||||
import org.springframework.web.socket.handler.TextWebSocketHandler;
|
||||
import org.springframework.web.util.UriComponentsBuilder;
|
||||
|
||||
import java.io.IOException;
|
||||
import java.net.URI;
|
||||
import java.util.Map;
|
||||
import java.util.concurrent.ConcurrentHashMap;
|
||||
|
||||
@@ -19,12 +22,23 @@ public class CustomWebSocketHandler extends TextWebSocketHandler {
|
||||
private final Map<String, WebSocketSession> sessions = new ConcurrentHashMap<>();
|
||||
|
||||
|
||||
|
||||
@Override
|
||||
public void afterConnectionEstablished(WebSocketSession session) {
|
||||
public void afterConnectionEstablished(WebSocketSession session) throws IOException {
|
||||
// URI uri = session.getUri();
|
||||
// if (uri == null) {
|
||||
// return;
|
||||
// }
|
||||
// MultiValueMap<String, String> queryParams = UriComponentsBuilder.fromUri(uri).build().getQueryParams();
|
||||
// String sn = queryParams.getFirst("sn");
|
||||
// log.info("sn = " + sn);
|
||||
// if (sn == null) {
|
||||
// session.close(CloseStatus.BAD_DATA.withReason("Missing device SN"));
|
||||
// return;
|
||||
// }
|
||||
String sessionId = session.getId();
|
||||
sessions.put(sessionId, session);
|
||||
log.info("✅ 连接建立: ID={}, 当前连接数: {}", sessionId, sessions.size());
|
||||
String sn = (String) session.getAttributes().get("sn");
|
||||
sessions.put(sn, session);
|
||||
log.info("✅ 连接建立: ID={},sn={},当前连接数: {}", sessionId, sn, sessions.size());
|
||||
}
|
||||
|
||||
@Override
|
||||
@@ -32,6 +46,7 @@ public class CustomWebSocketHandler extends TextWebSocketHandler {
|
||||
String payload = message.getPayload();
|
||||
log.info("📩 收到消息: {}", payload);
|
||||
|
||||
|
||||
// 示例:回复客户端
|
||||
String reply = "服务器已接收: " + payload;
|
||||
try {
|
||||
@@ -47,8 +62,10 @@ public class CustomWebSocketHandler extends TextWebSocketHandler {
|
||||
@Override
|
||||
public void afterConnectionClosed(WebSocketSession session, CloseStatus status) {
|
||||
String sessionId = session.getId();
|
||||
sessions.remove(sessionId);
|
||||
log.info("❌ 连接关闭: ID={}, 原因: {}", sessionId, status.getReason());
|
||||
String sn = (String) session.getAttributes().get("sn");
|
||||
sessions.remove(sn);
|
||||
log.info("❌ 连接关闭: ID={},sn={},Code: {},原因: {},当前连接数: {}", sessionId, sn, status.getCode(), status.getReason(), sessions.size());
|
||||
|
||||
}
|
||||
|
||||
// 广播消息给所有客户端
|
||||
|
||||
@@ -1,13 +1,17 @@
|
||||
package com.onekeycall.videotablet.interceptor;
|
||||
|
||||
import com.onekeycall.videotablet.utils.JwtUtil;
|
||||
import jakarta.servlet.http.HttpServletRequest;
|
||||
import lombok.extern.slf4j.Slf4j;
|
||||
import org.springframework.http.server.ServerHttpRequest;
|
||||
import org.springframework.http.server.ServerHttpResponse;
|
||||
import org.springframework.http.server.ServletServerHttpRequest;
|
||||
import org.springframework.web.socket.WebSocketHandler;
|
||||
import org.springframework.web.socket.server.HandshakeInterceptor;
|
||||
|
||||
import java.util.Map;
|
||||
|
||||
@Slf4j
|
||||
public class AuthHandshakeInterceptor implements HandshakeInterceptor {
|
||||
|
||||
private final JwtUtil jwtUtil;
|
||||
@@ -20,7 +24,15 @@ public class AuthHandshakeInterceptor implements HandshakeInterceptor {
|
||||
public boolean beforeHandshake(ServerHttpRequest request, ServerHttpResponse response, WebSocketHandler wsHandler, Map<String, Object> attributes) throws Exception {
|
||||
String authHeader = request.getHeaders().getFirst("Authorization");
|
||||
String deviceId = request.getHeaders().getFirst("Device-ID");
|
||||
String userId = request.getHeaders().getFirst("user_id");
|
||||
String userId = request.getHeaders().getFirst("User-ID");
|
||||
|
||||
if (request instanceof ServletServerHttpRequest) {
|
||||
ServletServerHttpRequest servletRequest = (ServletServerHttpRequest) request;
|
||||
HttpServletRequest httpRequest = servletRequest.getServletRequest();
|
||||
String sn = httpRequest.getParameter("sn");
|
||||
attributes.put("sn", sn);
|
||||
log.info("Intercepted - sn: " + sn);
|
||||
}
|
||||
|
||||
if (authHeader == null || !authHeader.startsWith("Bearer ") || userId == null) {
|
||||
return false;
|
||||
|
||||
Reference in New Issue
Block a user