基于Android Okhttp3 webSocket 错误重连和保证长连接(两个线程交替进行)

这几天遇到一个需求---使用websock进行实时通信,并且保证长时间连接。

采取的方案是利用线程来定时进行webSocket重新连接(即断开后重新连接)。这种方法觉得比较笨,但是这种方法比较有效。

1、定时连接线程,比较简单的一个继承Thread的类

public class ReconnectionWSThread extends Thread {

    public volatile boolean isStop = false; //停止线程标记

    @Override
    public void run() {
        super.run();

        while (!isStop) {
            try {
                LogUtils.e("ReconnectionWSThread", " --- ReconnectionWSRThread 启动 ");
                Thread.sleep(10 * 60 * 1000);              
                //重连操作
            } catch (InterruptedException e) {
                e.printStackTrace();
            }
        }
    }
}

2、错误重连线程

public class ReconnectionWSErrorThread extends Thread {

    public volatile boolean isStop = false; //停止线程标记

    @Override
    public void run() {
        super.run();

        while (!isStop) {
            try {

                LogUtils.e("ReconnectionWSErrorThread", " --- ReconnectionWSErrorThread 启动 ");

                Thread.sleep(2 * 1000); 

                //重连操作

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


正常情况下的长连接线程(ReconnectionWSThread),在一个合适的位置开启就可以,主要与错误重连(ReconnectionWSErrorThread)交替运行。


在okhttp上支持websocket通信:如果通信过程中出现错误或者连接断开会回掉在onFailure方法中

    @Override
    public void onFailure(WebSocket webSocket, Throwable t, Response response) {
        super.onFailure(webSocket, t, response);
        BaseApplication.isReconnect = true;//错误重连

        BaseApplication.BaseReconnectionWSThread.isStop = true; //停止长连接线程 

        if (BaseApplication.isErrorReconnect) { //第一次进入onFailure回掉
            BaseApplication.BaseReconnectionWSErrorThread.start(); //启动错误重连
            BaseApplication.isErrorReconnect = false;
        } else {
            if (!BaseApplication.BaseReconnectionWSErrorThread.isAlive()) { //判断线程run方法是否还在运行中,注意当run方法运行结束后,相当于线程结束 
                BaseApplication.BaseReconnectionWSErrorThread = new ReconnectionWSErrorThread(); //启动新的线程 
                BaseApplication.BaseReconnectionWSErrorThread.start();
            }
        }
    }

在onMessage回掉中:

        //错误重连
        if (BaseApplication.isReconnect) {

            BaseApplication.BaseReconnectionWSErrorThread.isStop = true;

            BaseApplication.isReconnect = false;

            BaseApplication.BaseReconnectionWSThread =  new ReconnectionWSThread(); //重新启动线程
            BaseApplication.BaseReconnectionWSThread.start();
        }

BaseApplition:

public class BaseApplication extends Application {

    public static boolean isReconnect = false;
    public static boolean isErrorReconnect = true;
    public static ReconnectionWSThread BaseReconnectionWSThread;
    public static ReconnectionWSErrorThread BaseReconnectionWSErrorThread;

    @Override
    public void onCreate() {

        BaseReconnectionWSThread = new ReconnectionWSThread();
        BaseReconnectionWSErrorThread = new ReconnectionWSErrorThread();
        super.onCreate();
    }

    public static BaseRequest getBaseRequestInstance(){

        return BaseRequestInstance;
    }
}

附:如果有更好的方法欢迎讨论指正,感谢感谢!!!






你可能感兴趣的:(Android)