rax-remote-2/vital-signs/src/main/java/com/rax/vital/handler/ChatHandler.java

65 lines
2.2 KiB
Java
Raw Normal View History

2024-04-12 09:58:28 +08:00
package com.rax.vital.handler;
2024-04-17 15:14:49 +08:00
import com.alibaba.fastjson.JSONObject;
import com.rax.vital.medicine.service.ChatService;
import com.rax.vital.util.GetHttpParamUtil;
import jakarta.annotation.Resource;
import org.springframework.security.oauth2.server.authorization.OAuth2Authorization;
import org.springframework.security.oauth2.server.authorization.OAuth2AuthorizationService;
import org.springframework.security.oauth2.server.authorization.OAuth2TokenType;
2024-04-12 09:58:28 +08:00
import org.springframework.web.socket.CloseStatus;
import org.springframework.web.socket.WebSocketHandler;
import org.springframework.web.socket.WebSocketMessage;
import org.springframework.web.socket.WebSocketSession;
2024-04-17 15:14:49 +08:00
import java.net.URLDecoder;
import java.util.Map;
2024-04-12 09:58:28 +08:00
public class ChatHandler implements WebSocketHandler {
2024-04-17 15:14:49 +08:00
@Resource
private OAuth2AuthorizationService authorizationService;
@Resource
private ChatService chatService;
2024-04-12 09:58:28 +08:00
@Override
public void afterConnectionEstablished(WebSocketSession session) throws Exception {
}
@Override
public void handleMessage(WebSocketSession session, WebSocketMessage<?> message) throws Exception {
2024-04-17 15:14:49 +08:00
String decode = URLDecoder.decode(session.getUri().getQuery());
Map params = GetHttpParamUtil.getParams(decode);
String token = (String) params.get("token");
OAuth2Authorization authorization = authorizationService.findByToken(token, OAuth2TokenType.ACCESS_TOKEN);
if (authorization != null) {
String username = authorization.getPrincipalName();
2024-04-12 09:58:28 +08:00
2024-04-17 15:14:49 +08:00
String payload = (String) message.getPayload();
JSONObject jsonObject = JSONObject.parseObject(payload);
String patientName = jsonObject.getString("patientName");
String idNum = jsonObject.getString("idNum");
String date = jsonObject.getString("date");
// 消息内容
String msg = jsonObject.getString("msg");
chatService.sendMessage(username, patientName, idNum, date, session, msg);
} else {
}
2024-04-12 09:58:28 +08:00
}
@Override
public void handleTransportError(WebSocketSession session, Throwable exception) throws Exception {
}
@Override
public void afterConnectionClosed(WebSocketSession session, CloseStatus closeStatus) throws Exception {
2024-04-17 15:14:49 +08:00
chatService.stopTaskMongo(session.getId());
2024-04-12 09:58:28 +08:00
}
@Override
public boolean supportsPartialMessages() {
return false;
}
}