package com.kmall.common.manager.websocket;

import org.java_websocket.WebSocket;
import org.java_websocket.client.WebSocketClient;
import org.java_websocket.handshake.ServerHandshake;

import java.io.UnsupportedEncodingException;
import java.net.URI;
import java.net.URISyntaxException;
import java.nio.ByteBuffer;
import java.util.HashMap;
import java.util.Map;

/**
 * Created by ken on 2018/12/20.
 */
@Deprecated
public class WebSocketClientHandle {

    static Map<String,WebSocketClient> clientMap=new HashMap<>();
    public static void send(String url, String message, Callback onSuccess, Callback onError){
        WebSocketClient client = clientMap.get(url);
        if(client!=null&&client.isConnecting()&&client.isOpen()){
            client.send(message);
        }
        else
        {
            try {
                client = new WebSocketClient(new URI(url)) {

                    @Override
                    public void onOpen(ServerHandshake arg0) {
                       // System.out.println("打开链接");
                    }

                    @Override
                    public void onMessage(String arg0) {
                        if(onSuccess!=null){
                            onSuccess.run(arg0);
                        }
                    }

                    @Override
                    public void onError(Exception arg0) {
                        if(onError!=null){
                            onError(arg0);
                        }
                    }

                    @Override
                    public void onClose(int arg0, String arg1, boolean arg2) {
                        //System.out.println("链接已关闭");
                    }

                    @Override
                    public void onMessage(ByteBuffer bytes) {
                        try {
                            if(onSuccess!=null){
                                onSuccess.run(new String(bytes.array(),"utf-8"));
                            }
                        } catch (UnsupportedEncodingException e) {
                            e.printStackTrace();
                        }
                    }


                };
                client.connect();
                Integer timeOut=3*60*1000;
                Integer step=10;
                Integer count=0;
                //阻塞以等待OPEN
                while(!client.getReadyState().equals(WebSocket.READYSTATE.OPEN)){
                    try {
                        count+=step;
                        Thread.sleep(step);
                        if(count>=timeOut){
                            onError.run(new Exception("WS服务器连接超时！或服务器已经关闭"));
                            break;
                        }
                    } catch (InterruptedException e) {
                        e.printStackTrace();
                    }
                }

                client.send(message);
                clientMap.put(url,client);

            } catch (URISyntaxException e) {
                e.printStackTrace();
            }


        }

    }


}
