New file |
| | |
| | | package com.whyc.websocket; |
| | | |
| | | import com.whyc.config.WebSocketConfig; |
| | | import com.whyc.pojo.Response; |
| | | import com.whyc.service.UserInfService; |
| | | import org.springframework.beans.factory.annotation.Autowired; |
| | | import org.springframework.stereotype.Component; |
| | | |
| | | import javax.servlet.http.HttpSession; |
| | | import javax.websocket.*; |
| | | import javax.websocket.server.ServerEndpoint; |
| | | import java.io.IOException; |
| | | import java.util.HashMap; |
| | | import java.util.Map; |
| | | |
| | | /** |
| | | * 账号重复登录检查 |
| | | */ |
| | | @Component |
| | | @ServerEndpoint(value = "/loginCheck",encoders = WebSocketEncoder.class,configurator = WebSocketConfig.class) |
| | | public class LoginCheckSocket { |
| | | |
| | | private Session session; |
| | | |
| | | private Thread thread; |
| | | private static UserInfService uinfService; |
| | | |
| | | @Autowired |
| | | public void setLoginService(UserInfService uinfService) { |
| | | LoginCheckSocket.uinfService = uinfService; |
| | | } |
| | | |
| | | @OnOpen |
| | | public void onOpen(Session session, EndpointConfig config){ |
| | | this.session = session; |
| | | HttpSession httpSession = (HttpSession) config.getUserProperties().get("httpSession"); |
| | | if(httpSession == null){ |
| | | Map<String, Response> res = new HashMap<>(); |
| | | Response resp1 = new Response().set(1, false, "登录信息失效,重新登录"); |
| | | res.put("checkLogin", resp1); |
| | | try { |
| | | session.getBasicRemote().sendObject(new Response().set(1,res)); |
| | | } catch (IOException | EncodeException e) { |
| | | e.printStackTrace(); |
| | | } |
| | | }else { |
| | | Thread thread = new Thread() { |
| | | @Override |
| | | public void run() { |
| | | try { |
| | | Map<String, Response> res = new HashMap<>(); |
| | | Response sessionRes = new Response().set(1, httpSession.getId()); |
| | | while (!currentThread().isInterrupted()) { |
| | | Response response = uinfService.checkUserWebSocket(httpSession); |
| | | res.put("checkLogin", response); |
| | | res.put("session", sessionRes); |
| | | session.getBasicRemote().sendObject(new Response().set(1, res)); |
| | | if (response.getData() instanceof Boolean) { |
| | | if(response.getData().equals(false)) { |
| | | this.interrupt(); |
| | | } |
| | | } |
| | | sleep(4000); |
| | | } |
| | | } catch (Exception e) { |
| | | this.interrupt(); |
| | | } |
| | | } |
| | | }; |
| | | thread.start(); |
| | | this.thread = thread; |
| | | } |
| | | } |
| | | |
| | | @OnClose |
| | | public void onClose(CloseReason closeReason) throws IOException { |
| | | System.err.println("closeReason = " + closeReason); |
| | | if(session.isOpen()){ |
| | | session.close(); |
| | | } |
| | | } |
| | | |
| | | @OnError |
| | | public void onError(Throwable error) throws IOException { |
| | | error.printStackTrace(); |
| | | thread.isInterrupted(); |
| | | if(session.isOpen()){ |
| | | session.close(); |
| | | } |
| | | } |
| | | |
| | | } |