package com.dashitech.callcenter.webSocket.core;

import org.apache.commons.lang3.StringUtils;
import org.atmosphere.cpr.AtmosphereResource;
import org.atmosphere.websocket.WebSocketHandlerAdapter;

import java.io.IOException;

/**
 * 带缓存的WebSocket类
 */
public class CacheHandlerAdapter extends WebSocketHandlerAdapter {
    private SessionCache CACHE = new SessionCache();

    public SessionCache getCACHE() {
        return CACHE;
    }

    public void logout(AtmosphereResource resource) {
        CACHE.removeByUUID(resource.uuid());
    }

    public SessionCache login(AtmosphereResource resource, String identity) throws IOException {
        //缓存登录的浏览器，模板：{login:'123'}
        if (StringUtils.isNotEmpty(identity)) {
            if(CACHE.contains(identity)){
                CACHE.removeByUUID(identity);
            }
                CACHE.put(identity,resource);
        }
        return CACHE;
    }
}
