初始化socket组件
parent
e46bbe9259
commit
5f283f24e4
@ -0,0 +1,26 @@
|
|||||||
|
package com.supervision.controller;
|
||||||
|
|
||||||
|
import com.supervision.websocket.cache.WebSocketUserCache;
|
||||||
|
import io.swagger.annotations.Api;
|
||||||
|
import org.springframework.messaging.simp.SimpMessagingTemplate;
|
||||||
|
import org.springframework.web.bind.annotation.RequestMapping;
|
||||||
|
import org.springframework.web.bind.annotation.RestController;
|
||||||
|
import org.springframework.web.socket.TextMessage;
|
||||||
|
import org.springframework.web.socket.WebSocketSession;
|
||||||
|
|
||||||
|
import javax.annotation.Resource;
|
||||||
|
import java.io.IOException;
|
||||||
|
|
||||||
|
@Api(tags = "问诊")
|
||||||
|
@RestController
|
||||||
|
@RequestMapping("/ask/")
|
||||||
|
public class AskController {
|
||||||
|
|
||||||
|
@RequestMapping("/send")
|
||||||
|
public void sendMessage(String message,String id) throws IOException {
|
||||||
|
WebSocketSession session = WebSocketUserCache.getSession(id);
|
||||||
|
session.sendMessage(new TextMessage(message));
|
||||||
|
}
|
||||||
|
|
||||||
|
|
||||||
|
}
|
@ -1,26 +1,40 @@
|
|||||||
package com.supervision.controller;
|
package com.supervision.controller;
|
||||||
|
|
||||||
import cn.hutool.core.util.StrUtil;
|
import cn.hutool.core.util.StrUtil;
|
||||||
|
import cn.hutool.json.JSONUtil;
|
||||||
import com.supervision.domain.UserInfo;
|
import com.supervision.domain.UserInfo;
|
||||||
import com.supervision.exception.BusinessException;
|
import com.supervision.exception.BusinessException;
|
||||||
|
import com.supervision.model.User;
|
||||||
|
import com.supervision.service.UserService;
|
||||||
import com.supervision.util.TokenUtil;
|
import com.supervision.util.TokenUtil;
|
||||||
import io.swagger.annotations.Api;
|
import io.swagger.annotations.Api;
|
||||||
|
import lombok.RequiredArgsConstructor;
|
||||||
import org.springframework.web.bind.annotation.GetMapping;
|
import org.springframework.web.bind.annotation.GetMapping;
|
||||||
import org.springframework.web.bind.annotation.RequestMapping;
|
import org.springframework.web.bind.annotation.RequestMapping;
|
||||||
import org.springframework.web.bind.annotation.RestController;
|
import org.springframework.web.bind.annotation.RestController;
|
||||||
|
|
||||||
|
import java.util.Optional;
|
||||||
|
|
||||||
@Api(tags = "用户管理")
|
@Api(tags = "用户管理")
|
||||||
@RestController
|
@RestController
|
||||||
@RequestMapping("user")
|
@RequestMapping("user")
|
||||||
|
@RequiredArgsConstructor
|
||||||
public class UserController {
|
public class UserController {
|
||||||
|
|
||||||
|
private final UserService userService;
|
||||||
|
|
||||||
@GetMapping("login")
|
@GetMapping("login")
|
||||||
public String login(String userName, String password) {
|
public String login(String userAccount, String password) {
|
||||||
if (StrUtil.isBlank(userName)) {
|
if (!StrUtil.isAllNotBlank(userAccount, password)) {
|
||||||
throw new BusinessException("用户名不能为空");
|
throw new BusinessException("用户名不能为空");
|
||||||
}
|
}
|
||||||
// TODO 后面实现
|
Optional<User> user = userService.lambdaQuery().eq(User::getAccount, userAccount).last("limit 1").oneOpt();
|
||||||
UserInfo userInfo = new UserInfo();
|
if (!user.isPresent()) {
|
||||||
return TokenUtil.creatToken(userInfo);
|
throw new BusinessException("未找到用户");
|
||||||
|
}
|
||||||
|
if (!user.get().getPassword().equals(password)) {
|
||||||
|
throw new BusinessException("密码错误");
|
||||||
|
}
|
||||||
|
return TokenUtil.creatToken(JSONUtil.toJsonStr(user.get()));
|
||||||
}
|
}
|
||||||
}
|
}
|
||||||
|
@ -0,0 +1,38 @@
|
|||||||
|
package com.supervision.websocket.cache;
|
||||||
|
|
||||||
|
import cn.hutool.core.util.ObjectUtil;
|
||||||
|
import com.supervision.exception.BusinessException;
|
||||||
|
import lombok.extern.slf4j.Slf4j;
|
||||||
|
import org.springframework.web.socket.WebSocketSession;
|
||||||
|
|
||||||
|
import java.util.Collection;
|
||||||
|
import java.util.Map;
|
||||||
|
import java.util.Set;
|
||||||
|
import java.util.concurrent.ConcurrentHashMap;
|
||||||
|
import java.util.concurrent.ConcurrentMap;
|
||||||
|
|
||||||
|
@Slf4j
|
||||||
|
public class WebSocketUserCache {
|
||||||
|
|
||||||
|
private static final Map<String, WebSocketSession> map = new ConcurrentHashMap<>();
|
||||||
|
|
||||||
|
|
||||||
|
public static void login(String id, WebSocketSession socketSession) {
|
||||||
|
map.put(id, socketSession);
|
||||||
|
log.info("sessionId:{}注册成功", id);
|
||||||
|
}
|
||||||
|
|
||||||
|
public static void logout(String id) {
|
||||||
|
map.remove(id);
|
||||||
|
log.info("sessionId:{}注销成功", id);
|
||||||
|
}
|
||||||
|
|
||||||
|
public static WebSocketSession getSession(String id){
|
||||||
|
WebSocketSession webSocketSession = map.get(id);
|
||||||
|
if (ObjectUtil.isEmpty(webSocketSession)){
|
||||||
|
throw new BusinessException("未找到socket链接");
|
||||||
|
}
|
||||||
|
return webSocketSession;
|
||||||
|
}
|
||||||
|
|
||||||
|
}
|
@ -0,0 +1,24 @@
|
|||||||
|
package com.supervision.websocket.config;
|
||||||
|
|
||||||
|
import com.supervision.websocket.handler.AskWebSocketHandler;
|
||||||
|
import org.springframework.context.annotation.Bean;
|
||||||
|
import org.springframework.context.annotation.Configuration;
|
||||||
|
import org.springframework.web.socket.WebSocketHandler;
|
||||||
|
import org.springframework.web.socket.config.annotation.EnableWebSocket;
|
||||||
|
import org.springframework.web.socket.config.annotation.WebSocketConfigurer;
|
||||||
|
import org.springframework.web.socket.config.annotation.WebSocketHandlerRegistry;
|
||||||
|
|
||||||
|
@Configuration
|
||||||
|
public class WebSocketConfig implements WebSocketConfigurer {
|
||||||
|
|
||||||
|
@Override
|
||||||
|
public void registerWebSocketHandlers(WebSocketHandlerRegistry registry) {
|
||||||
|
registry.addHandler(askWebSocketHandler(), "/websocket")
|
||||||
|
.setAllowedOrigins("*");
|
||||||
|
}
|
||||||
|
|
||||||
|
@Bean
|
||||||
|
public WebSocketHandler askWebSocketHandler() {
|
||||||
|
return new AskWebSocketHandler();
|
||||||
|
}
|
||||||
|
}
|
@ -0,0 +1,33 @@
|
|||||||
|
package com.supervision.websocket.handler;
|
||||||
|
|
||||||
|
import com.supervision.websocket.cache.WebSocketUserCache;
|
||||||
|
import lombok.extern.slf4j.Slf4j;
|
||||||
|
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;
|
||||||
|
|
||||||
|
@Slf4j
|
||||||
|
public class AskWebSocketHandler extends TextWebSocketHandler {
|
||||||
|
|
||||||
|
@Override
|
||||||
|
public void afterConnectionEstablished(WebSocketSession session) throws Exception {
|
||||||
|
// 获取本次
|
||||||
|
String id = session.getId();
|
||||||
|
WebSocketUserCache.login(id, session);
|
||||||
|
// 连接建立时的处理逻辑
|
||||||
|
}
|
||||||
|
|
||||||
|
@Override
|
||||||
|
protected void handleTextMessage(WebSocketSession session, TextMessage message) throws Exception {
|
||||||
|
// 处理接收到的文本消息
|
||||||
|
log.info("收到消息:{}", message.toString());
|
||||||
|
}
|
||||||
|
|
||||||
|
@Override
|
||||||
|
public void afterConnectionClosed(WebSocketSession session, CloseStatus status) throws Exception {
|
||||||
|
// 连接关闭时的处理逻辑
|
||||||
|
String id = session.getId();
|
||||||
|
WebSocketUserCache.logout(id);
|
||||||
|
}
|
||||||
|
}
|
Loading…
Reference in New Issue