whycxzp
2022-03-28 eaa8cb8cd5d2d5d5c95ac774b7cf07e9c83a2d04
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
package com.whyc.webSocket;
 
import com.whyc.config.WebSocketConfig;
import com.whyc.dto.Response;
import com.whyc.service.LoginService;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.stereotype.Component;
 
import javax.servlet.http.HttpSession;
import javax.websocket.EncodeException;
import javax.websocket.EndpointConfig;
import javax.websocket.OnOpen;
import javax.websocket.Session;
import javax.websocket.server.ServerEndpoint;
import java.io.IOException;
 
/**
 * 账号重复登录检查
 */
@Component
@ServerEndpoint(value = "/loginCheck",encoders = WebSocketEncoder.class,configurator = WebSocketConfig.class)
public class LoginCheckSocket {
 
    private Session session;
 
    private static LoginService loginService;
 
    @Autowired
    public void setLoginService(LoginService loginService) {
        LoginCheckSocket.loginService = loginService;
    }
 
    @OnOpen
    public void onOpen(Session session, EndpointConfig config){
        this.session = session;
        HttpSession httpSession = (HttpSession) config.getUserProperties().get("httpSession");
        Response response = loginService.checkUserWebSocket(httpSession);
        try {
            session.getBasicRemote().sendObject(response);
        } catch (IOException e) {
            e.printStackTrace();
        } catch (EncodeException e) {
            e.printStackTrace();
        }
 
    }
 
}