Android WebSocket灰色保活实现长连接(含客户端&服务端)

发布于 2021-05-22  16 次阅读


应用开发中,如果要时时更新某一数据,我就会选择跟服务端建立长连接,这样既能保证时效性,也能更好地节约性能。总不能while(true)的调用后台接口吧?这样会被打死的(手动滑稽)。比如直播间观众人数,直播评论等我都会选择WebSockst来接收服务端数据。

说巧不巧,最近又遇到这个需求了。虽然没什么难度,也还是记录一下吧(真的没有更博素材了)。

文章最后附上源码

首先是安卓客户端

1.添加依赖

 implementation "org.java-websocket:Java-WebSocket:1.4.0"

2.创建WebSocket服务类

package com.wl.assistcloud.socket;

import android.annotation.SuppressLint;
import android.app.Notification;
import android.app.NotificationChannel;
import android.app.NotificationManager;
import android.app.Service;
import android.content.Context;
import android.content.Intent;
import android.graphics.Color;
import android.os.Binder;
import android.os.Build;
import android.os.Handler;
import android.os.IBinder;
import android.os.PowerManager;
import android.util.Log;

import androidx.annotation.RequiresApi;
import androidx.core.app.NotificationCompat;

import com.wl.assistcloud.MainActivity;
import com.wl.assistcloud.R;
import com.wl.assistcloud.event.MsgEvent;
import com.wl.assistcloud.floatutil.StudyFloatUtilData;
import com.wl.assistcloud.floatutil.StudyFloatUtilHelper;
import com.wl.assistcloud.helper.RxBus;
import com.wl.assistcloud.helper.Util;

import org.greenrobot.eventbus.EventBus;
import org.java_websocket.handshake.ServerHandshake;

import java.net.URI;


public class WebSocketClientService extends Service {
    public MyWebSocketClient client;
    private JWebSocketClientBinder mBinder = new JWebSocketClientBinder();
    private final static int GRAY_SERVICE_ID = 1001;

    //灰色保活
    public static class GrayInnerService extends Service {

        @Override
        public int onStartCommand(Intent intent, int flags, int startId) {
            startForeground(GRAY_SERVICE_ID, new Notification());
            stopForeground(true);
            stopSelf();
            return super.onStartCommand(intent, flags, startId);
        }

        @Override
        public IBinder onBind(Intent intent) {
            return null;
        }
    }

    PowerManager.WakeLock wakeLock;//锁屏唤醒

    //获取电源锁,保持该服务在屏幕熄灭时仍然获取CPU时,保持运行
    @SuppressLint("InvalidWakeLockTag")
    private void acquireWakeLock() {
        if (null == wakeLock) {
            PowerManager pm = (PowerManager) this.getSystemService(Context.POWER_SERVICE);
            wakeLock = pm.newWakeLock(PowerManager.PARTIAL_WAKE_LOCK | PowerManager.ON_AFTER_RELEASE, "PostLocationService");
            if (null != wakeLock) {
                wakeLock.acquire();
            }
        }
    }

    //用于Activity和service通讯
    public class JWebSocketClientBinder extends Binder {
        public WebSocketClientService getService() {
            return WebSocketClientService.this;
        }
    }

    @Override
    public IBinder onBind(Intent intent) {
        return mBinder;
    }

    @Override
    public void onCreate() {
        super.onCreate();
    }

    @Override
    public int onStartCommand(Intent intent, int flags, int startId) {
        //初始化websocket
        initSocketClient();
        mHandler.postDelayed(heartBeatRunnable, HEART_BEAT_RATE);//开启心跳检测

        //设置service为前台服务,提高优先级
        if (Build.VERSION.SDK_INT >= Build.VERSION_CODES.O) {
            startMyOwnForeground();
        } else {
            startForeground(GRAY_SERVICE_ID, new Notification());
        }
        acquireWakeLock();
        return START_STICKY;
    }

    @RequiresApi(api = Build.VERSION_CODES.O)
    private void startMyOwnForeground() {
        Log.e("YYY","前台进程");
        String NOTIFICATION_CHANNEL_ID = "com.wl.assistcloud";
        String channelName = "My Background Service";
        NotificationChannel chan = new NotificationChannel(NOTIFICATION_CHANNEL_ID, channelName, NotificationManager.IMPORTANCE_NONE);
        chan.setLightColor(Color.BLUE);
        chan.setLockscreenVisibility(Notification.VISIBILITY_PRIVATE);
        NotificationManager manager = (NotificationManager) getSystemService(Context.NOTIFICATION_SERVICE);
        assert manager != null;
        manager.createNotificationChannel(chan);

        NotificationCompat.Builder notificationBuilder = new NotificationCompat.Builder(this, NOTIFICATION_CHANNEL_ID);
        Notification notification = notificationBuilder.setOngoing(true)
                .setSmallIcon(R.drawable.ic_launcher_background)
                .setContentTitle("开启云服务保活...")
                .setPriority(NotificationManager.IMPORTANCE_MIN)
                .setCategory(Notification.CATEGORY_SERVICE)
                .build();
        startForeground(2, notification);
    }


    @Override
    public void onDestroy() {
        closeConnect();
        super.onDestroy();
    }

    public WebSocketClientService() {
    }


    /**
     * 初始化websocket连接
     */
    private void initSocketClient() {
        URI uri = URI.create(Util.ws);
        client = new MyWebSocketClient(uri) {
            @Override
            public void onMessage(String message) {
                EventBus.getDefault().postSticky(new MsgEvent(message));
            }

            @Override
            public void onOpen(ServerHandshake handshakedata) {
                super.onOpen(handshakedata);
                RxBus.getInstance().post(Util.SOCKET_OPEN);
            }

            @Override
            public void onError(Exception ex) {
                super.onError(ex);
            }
        };
        connect();
    }

    /**
     * 连接webSocket
     */
    private void connect() {
        new Thread() {
            @Override
            public void run() {
                try {
                    //connectBlocking多出一个等待操作,会先连接再发送,否则未连接发送会报错
                    client.connectBlocking();
                } catch (InterruptedException e) {
                    e.printStackTrace();
                }
            }
        }.start();

    }

    /**
     * 发送消息
     *
     * @param msg
     */
    public void sendMsg(String msg) {
        if (null != client) {
            Log.e("JWebSocketClientService", "发送的消息:" + msg);
            //  client.send("{\"name\":\"lolo\", \"text\": \"" + msg + "\"}");
            client.send(msg);
        }
    }

    /**
     * 断开连接
     */
    private void closeConnect() {
        try {
            if (null != client) {
                client.close();
            }
        } catch (Exception e) {
            e.printStackTrace();
        } finally {
            client = null;
        }
    }

    //    -------------------------------------webSocket心跳检测------------------------------------------------
    private static final long HEART_BEAT_RATE = 10 * 1000;//每隔10秒进行一次对长连接的心跳检测
    private Handler mHandler = new Handler();
    private Runnable heartBeatRunnable = new Runnable() {
        @Override
        public void run() {
            if (client != null) {
                if (client.isClosed()) {
                    RxBus.getInstance().post(Util.SOCKET_HEART_BEAT_FAIL);
                    reconnectWs();
                }
            } else {
                //如果client已为空,重新初始化连接
                RxBus.getInstance().post(Util.SOCKET_HEART_BEAT_CLOSE);
                client = null;
                initSocketClient();
            }
            //每隔一定的时间,对长连接进行一次心跳检测
            mHandler.postDelayed(this, HEART_BEAT_RATE);

        }
    };

    /**
     * 开启重连
     */
    private void reconnectWs() {
        mHandler.removeCallbacks(heartBeatRunnable);
        new Thread() {
            @Override
            public void run() {
                try {
                    Log.e("JWebSocketClientService", "开启重连");
                    client.reconnectBlocking();
                } catch (InterruptedException e) {
                    e.printStackTrace();
                }
            }
        }.start();
    }
}

这个WebSocketClientService已经集成了心跳、断线重连、前台进程保活等基本能满足日常需要, 只需要配置URI uri = URI.create(“您的WebSocket服务端地址”);开箱即食。君可满意?

3.别忘了把服务注册到配置清单AndroidManifest.xml

        <service
            android:name=".socket.WebSocketClientService"
            android:enabled="true"
            android:exported="true" />
        <service
            android:name=".socket.WebSocketClientService$GrayInnerService"
            android:enabled="true"
            android:exported="false"
            android:process=":gray" />

4.启动服务

    startWebSClientService();
    //绑定服务
    bindService();


     /**
     * 启动服务(webSocket客户端服务)
     */
    private void startWebSClientService() {
        Intent intent = new Intent(this, WebSocketClientService.class);
        startService(intent);
    }

    /**
     * 绑定服务
     */
    private void bindService() {
        Intent bindIntent = new Intent(this, WebSocketClientService.class);
        bindService(bindIntent, serviceConnection, BIND_AUTO_CREATE);
    }

OK,客户端到此结束。愿君无BUG...有任何问题没运行起来,请下方留言或者查看文章下方项目github仓库。

接下来是服务端

因为后端我不是很擅长,所以马马虎虎得说一下吧。如果你是小白,我说得这么简单,你肯定听得懂。如果你是大佬.....算了,大佬应该没看到这里就已经退出去了。废话不多说,开席!

  1. 打开你的专属IDEA
  2. 下载文章最后的服务端源码
  3. 按需修改websocket链接路径
  4. 修改前端链接联调OK杀青.

服务端源码:

客户端源码:


现在住在心里的人,以后也要住进家里。