// Path: app/src/main/java/com/demo/amazing/net/WSCallBack.java
// public interface WSCallBack {
// 
//     void isConnected();
// 
//     void onMessage(String text);
// 
//     void onFail(int code, String err);
// 
//     void close();
// }
// 
// Path: app/src/main/java/com/demo/amazing/net/WSListener.java
// public class WSListener extends WebSocketListener {
// 
//     WSCallBack callBack;
// 
//     public WSListener(WSCallBack callBack) {
//         this.callBack = callBack;
//     }
// 
//     @Override
//     public void onOpen(WebSocket webSocket, Response response) {
//         callBack.isConnected();
//         LogUtil.e("client onOpen");
//     }
// 
//     @Override
//     public void onMessage(WebSocket webSocket, String text) {
//         callBack.onMessage(text);
//         LogUtil.e("client onMessage=" + text);
//     }
// 
//     @Override
//     public void onMessage(WebSocket webSocket, ByteString bytes) {
//     }
// 
//     @Override
//     public void onClosed(WebSocket webSocket, int code, String reason) {
//         LogUtil.e("client onClosed: code=" + code + " reason=" + reason);
//     }
// 
//     @Override
//     public void onClosing(WebSocket webSocket, int code, String reason) {
//     }
// 
//     @Override
//     public void onFailure(WebSocket webSocket, Throwable t, Response response) {
//         LogUtil.e("client onFailure: t=" + t.getMessage());
//         callBack.onFail(WSErrorCode.BROKEN, "net closed");
//     }
// }
// 
// Path: app/src/main/java/com/demo/amazing/net/config/WSErrorCode.java
// public interface WSErrorCode {
// 
//     int NO_ERROR = 0;
// 
//     int CANCEL = 1;
// 
//     int SUCCESS = 200;
// 
//     int NET_CLOSE = 404;
// 
//     int BROKEN = 1000;
// 
//     int WS_URL_NOT_EXIST = 1001;
// }


import com.demo.amazing.net.WSCallBack;
import com.demo.amazing.net.WSListener;
import com.demo.amazing.net.config.WSErrorCode;
import okhttp3.OkHttpClient;
import okhttp3.Request;
import okhttp3.WebSocket;
package com.demo.amazing.net.client;



/**
 * web socket client
 * Created by hzsunyj on 2017/8/28.
 */

public final class WSClient {

    /**
     * ok http client
     */
    private OkHttpClient httpClient;

    /**
     * web socket
     */
    private WebSocket webSocket;

    /**
     *
     */
    private WSClient() {
        httpClient = OkHttp.getInstance().getHttpClient();

    }

    /**
     * @return
     */
    public static WSClient getInstance() {
        return new WSClient();
    }

    /**
     * connect
     *
     * @param wsUrl
     * @param callBack
     */
    public void connect(String wsUrl, final WSCallBack callBack) {
        Request request = new Request.Builder().url(wsUrl).build();
        webSocket = httpClient.newWebSocket(request, new WSListener(callBack));
    }


    /**
     * dis connect
     */
    public void disConnect() {
        if (webSocket != null) {
            webSocket.close(WSErrorCode.BROKEN, "broken");
        }
    }

    /**
     * enqueue
     *
     * @param text
     * @return
     */
// Path: app/src/main/java/com/demo/amazing/net/WSCallBack.java
// public interface WSCallBack {
// 
//     void isConnected();
// 
//     void onMessage(String text);
// 
//     void onFail(int code, String err);
// 
//     void close();
// }
// 
// Path: app/src/main/java/com/demo/amazing/net/WSListener.java
// public class WSListener extends WebSocketListener {
// 
//     WSCallBack callBack;
// 
//     public WSListener(WSCallBack callBack) {
//         this.callBack = callBack;
//     }
// 
//     @Override
//     public void onOpen(WebSocket webSocket, Response response) {
//         callBack.isConnected();
//         LogUtil.e("client onOpen");
//     }
// 
//     @Override
//     public void onMessage(WebSocket webSocket, String text) {
//         callBack.onMessage(text);
//         LogUtil.e("client onMessage=" + text);
//     }
// 
//     @Override
//     public void onMessage(WebSocket webSocket, ByteString bytes) {
//     }
// 
//     @Override
//     public void onClosed(WebSocket webSocket, int code, String reason) {
//         LogUtil.e("client onClosed: code=" + code + " reason=" + reason);
//     }
// 
//     @Override
//     public void onClosing(WebSocket webSocket, int code, String reason) {
//     }
// 
//     @Override
//     public void onFailure(WebSocket webSocket, Throwable t, Response response) {
//         LogUtil.e("client onFailure: t=" + t.getMessage());
//         callBack.onFail(WSErrorCode.BROKEN, "net closed");
//     }
// }
// 
// Path: app/src/main/java/com/demo/amazing/net/config/WSErrorCode.java
// public interface WSErrorCode {
// 
//     int NO_ERROR = 0;
// 
//     int CANCEL = 1;
// 
//     int SUCCESS = 200;
// 
//     int NET_CLOSE = 404;
// 
//     int BROKEN = 1000;
// 
//     int WS_URL_NOT_EXIST = 1001;
// }


// Path: app/src/main/java/com/demo/amazing/net/client/WSClient.java
import com.demo.amazing.net.WSCallBack;
import com.demo.amazing.net.WSListener;
import com.demo.amazing.net.config.WSErrorCode;
import okhttp3.OkHttpClient;
import okhttp3.Request;
import okhttp3.WebSocket;

package com.demo.amazing.net.client;



/**
 * web socket client
 * Created by hzsunyj on 2017/8/28.
 */

public final class WSClient {

    /**
     * ok http client
     */
    private OkHttpClient httpClient;

    /**
     * web socket
     */
    private WebSocket webSocket;

    /**
     *
     */
    private WSClient() {
        httpClient = OkHttp.getInstance().getHttpClient();

    }

    /**
     * @return
     */
    public static WSClient getInstance() {
        return new WSClient();
    }

    /**
     * connect
     *
     * @param wsUrl
     * @param callBack
     */
    public void connect(String wsUrl, final WSCallBack callBack) {
        Request request = new Request.Builder().url(wsUrl).build();
        webSocket = httpClient.newWebSocket(request, new WSListener(callBack));
    }


    /**
     * dis connect
     */
    public void disConnect() {
        if (webSocket != null) {
            webSocket.close(WSErrorCode.BROKEN, "broken");
        }
    }

    /**
     * enqueue
     *
     * @param text
     * @return
     */
    public boolean send(String text) {