1. 程式人生 > >Spring Boot WebSocket 單節點模擬實現單點登入擠退

Spring Boot WebSocket 單節點模擬實現單點登入擠退

1、建立WebSocketServer

@ServerEndpoint("/websocket/{sid}")
@Component  // 成分、元件
public class WebSocketServer {
    //靜態變數,用來記錄當前線上連線數。應該把它設計成執行緒安全的。
    private static int onlineCount = 0;
    
    //用來存放每個客戶端對應的WebSocket物件。
    private static CopyOnWriteArraySet<WebSocketServer> webSocketSet = new CopyOnWriteArraySet<WebSocketServer>();

    //與某個客戶端的連線會話,需要通過它來給客戶端傳送資料
    private Session session;

    //接收sid
    private String sid="";
    
    /**
     * 連線建立成功呼叫的方法
     */
    @OnOpen
    public void onOpen(Session session,@PathParam("sid") String sid) {
        this.sid=sid;
        this.session = session;
        webSocketSet.add(this);     //加入set中
        addOnlineCount();           //線上數加1
        LogUtil.log.info("有新視窗開始監聽:"+sid+",當前線上人數為" + getOnlineCount());
        
        try {
            sendMessage("連線成功...");
        } catch (IOException e) {
            LogUtil.log.error("websocket IO異常");
        }
    }

    /**
     * 連線關閉呼叫的方法
     */
    @OnClose
    public void onClose() {
        webSocketSet.remove(this);  //從set中刪除
        subOnlineCount();           //線上數減1
        LogUtil.log.info("有一連線關閉!當前線上人數為" + getOnlineCount());
    }

    /**
     * 收到客戶端訊息後呼叫的方法
     * @param message 客戶端傳送過來的訊息
     */
    @OnMessage
    public void onMessage(String message, Session session) {
        LogUtil.log.info("收到來自視窗"+sid+"的資訊:"+message);
        //群發訊息
        for (WebSocketServer item : webSocketSet) {
            try {
                item.sendMessage(message);
            } catch (IOException e) {
                e.printStackTrace();
            }
        }
    }

    /**
     * @param session
     * @param error
     */
    @OnError
    public void onError(Session session, Throwable error) {
        LogUtil.log.error("發生錯誤");
        error.printStackTrace();
    }
    
    /**
     * 實現伺服器主動推送
     */
    public void sendMessage(String message) throws IOException {
        this.session.getBasicRemote().sendText(message);
    }

    /**
     * 群發自定義訊息
     */
    public static void sendInfo(String message,@PathParam("sid") String sid) throws IOException {
        LogUtil.log.info("推送訊息到視窗"+sid+",推送內容:"+message);
        for (WebSocketServer item : webSocketSet) {
            try {
                //這裡可以設定只推送給這個sid的,為null則全部推送
                if(sid==null) {
                    item.sendMessage(message);
                }else if(item.sid.equals(sid)){
                    item.sendMessage(message);
                }
            } catch (IOException e) {
                continue;
            }
        }
    }

    public static synchronized int getOnlineCount() {
        return onlineCount;
    }

    public static synchronized void addOnlineCount() {
        WebSocketServer.onlineCount++;
    }

    public static synchronized void subOnlineCount() {
        WebSocketServer.onlineCount--;
    }

    public static WebSocketServer getWebSocket(String sid) {
        if (webSocketSet == null || webSocketSet.size() <= 0) {
            return null;
        }
        
        for (WebSocketServer item : webSocketSet) {
            if (sid.equals(item.sid)) {
                return item;
            }
        }
        return null;
    }
}

 

2、使用者登入成功後判斷

WebSocketServer wss = WebSocketServer.getWebSocket(String.valueOf(adminInfo.getAid()));
if (wss != null) {  // 如果已經登入,則傳送擠退資訊
    try {
        wss.sendMessage("101");
    } catch (IOException e) {
        e.printStackTrace();
    }
}

3、使用者在前臺登入成功後,傳送連線資訊給伺服器

function openWebSocket(sid) { // sid為當前登入使用者的id
    var socket;
    if(typeof(WebSocket) == undefined) {
        console.log("您的瀏覽器不支援WebSocket");
    }else{
        //實現化WebSocket物件,指定要連線的伺服器地址與埠  建立連線
        socket = new WebSocket("ws://127.0.0.1:8080/EduMagSys/websocket/"+sid);

        //開啟事件
        socket.onopen = function() {
            console.log("Socket 已開啟");
            //socket.send("這是來自客戶端的訊息" + location.href + new Date());
        };

        //獲得訊息事件
        socket.onmessage = function(msg) {
            if (msg.data == "101") {
                alert("對不起,你的賬號已經在其它地方登入,若非本人操作,請及時更換密碼...");
                location.href="../login.html";
                return;
            }
        };

        //關閉事件
        socket.onclose = function() {
            console.log("Socket已關閉");
        };

        //發生了錯誤事件
        socket.onerror = function() {
            alert("Socket發生了錯誤");
        };

        $(window).unload(function(){
            socket.close();
        });
    }
}
 

注意:我這裡是通過判斷當前登入使用者的id是否已經在服務列表webSocketSet 中存在,如果存在說明此使用者已經在其它地方登入,則獲取到對應的WebSocketServer物件,傳送擠退資訊。