WebSocketServer

package com.seemygo.shop.cloud.core;

import org.apache.commons.lang.StringUtils;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.stereotype.Component;

import javax.websocket.*;
import javax.websocket.server.PathParam;
import javax.websocket.server.ServerEndpoint;

/**
 * WebSocket 服务连接实例
 * ws://localhost:8095/111111
 */
@ServerEndpoint("/{uuid}")
@Component
public class WebSocketServer {

    /**
     * 当有客户端建立连接时,进入此方法
     *
     * @param uuid
     * @param session
     */
    @OnOpen
    public void onOpen(@PathParam("uuid") String uuid, Session session) {
        System.out.println("客户端:" + uuid + "连接上来了");
        WebSocketSessionManager.INSTANCE.put(uuid, session);
    }

    @OnClose
    public void onClose(@PathParam("uuid") String uuid) {
        System.out.println("客户端:" + uuid + "关闭连接了");
        if (!StringUtils.isEmpty(uuid)) {
            WebSocketSessionManager.INSTANCE.remove(uuid);
        }
    }

    @OnError
    public void onError(@PathParam("uuid") String uuid, Throwable throwable) {
        System.out.println("客户端:" + uuid + "出现异常了");
        throwable.printStackTrace();
    }
}

 

posted @ 2021-12-12 19:37  白芷100  阅读(426)  评论(0)    收藏  举报