package com.dashitech.callcenter.webSocket.service.demo;

import com.fasterxml.jackson.databind.ObjectMapper;
import org.atmosphere.cache.UUIDBroadcasterCache;
import org.atmosphere.client.TrackMessageSizeInterceptor;
import org.atmosphere.config.service.WebSocketHandlerService;
import org.atmosphere.interceptor.HeartbeatInterceptor;
import org.atmosphere.util.SimpleBroadcaster;
import org.atmosphere.websocket.WebSocket;
import org.atmosphere.websocket.WebSocketProcessor;
import org.atmosphere.websocket.WebSocketStreamingHandlerAdapter;

import java.io.BufferedReader;
import java.io.IOException;
import java.io.Reader;

/**
 * Websocket示例 <br/>
 * <p>
 * 事件优先级：会先触发onClose事件，在触发订阅的事件
 */
@WebSocketHandlerService(path = "/chat/A", interceptors = {
        TrackMessageSizeInterceptor.class,  //校验消息大小，验证数据完整性，需要前端参数配合
        HeartbeatInterceptor.class},    //保持心跳，维持链接，需要前端参数配合
        broadcaster = SimpleBroadcaster.class,  //简单广播
        broadcasterCache = UUIDBroadcasterCache.class,//加入缓存，防止消息丢失
        atmosphereConfig = {"org.atmosphere.websocket.WebSocketProtocol=org.atmosphere.websocket.protocol.StreamingHttpProtocol"})
public class WebSocketChat extends WebSocketStreamingHandlerAdapter {
    private final ObjectMapper mapper = new ObjectMapper();

    @Override
    public void onOpen(WebSocket webSocket) throws IOException {
        System.out.println("A   onOpen   ");
        //对当前会话进行事件注册
//        webSocket.resource().addEventListener(new WebSocketEventListenerAdapter() {
//            @Override
//            public void onDisconnect(AtmosphereResourceEvent event) {
//                if (event.isCancelled()) {
//                    System.out.println("A   isCancelled    " + event.getResource().uuid());
//                } else if (event.isClosedByClient()) {
//                    System.out.println("A   isClosedByClient    " + event.getResource().uuid());
//                }
//            }
//        });
    }

    @Override
    public void onClose(WebSocket webSocket) {
        System.out.println("A   onClose   "+ webSocket.resource().uuid());
    }

    @Override
    public void onError(WebSocket webSocket, WebSocketProcessor.WebSocketException t) {
        System.out.println("A   onError   " + t.toString());
    }

    @Override
    public void onTextStream(WebSocket webSocket, Reader reader) throws IOException {
        System.out.println("A   onTextStream   " + webSocket.resource().uuid());
        webSocket.broadcast(mapper.writeValueAsString(mapper.readValue(new BufferedReader(reader).readLine(), Data.class)));

        System.out.println("AAAAA " + webSocket.resource().getRequest().toString());
        webSocket.resource().getResponse().getWriter().print("aaaaaaaaaaaaaa");

        try {
           //BossScreen.webSocketSender.identityBroadcastMessage(JsonUtil.toJSONObject(new Data("AAAA","11111")).toString());
           // BossScreen.webSocketSender.identityMessage("123", JsonUtil.toJSONObject(new Data("BBBBBB","222222")).toString());
           // BossScreen.webSocketSender.identityMessage("321", JsonUtil.toJSONObject(new Data("CCCCCCC","33333")).toString());
        } catch (Exception ex) {
            ex.printStackTrace();
        }

    }

}
