Android和iOS上的websockets [英] websockets on Android and iOS

查看:115
本文介绍了Android和iOS上的websockets的处理方法,对大家解决问题具有一定的参考价值,需要的朋友们下面随着小编来一起学习吧!

问题描述

我正在创建一个具有即时消息传递功能的移动应用程序.在聘请某人开发移动组件之前,我想出了一个后端.移动设备(客户端)和服务器之间进行通信的主要方式是通过网络套接字.我听说一旦应用程序不再在移动设备(包括android和iOS)上出现在前台,我就不会关闭websocket.我不想花时间在websockets上开发服务器以意识到我无法使用这项技术为此目的.有人可以协助我解决以下问题:

I'm creating a mobile app that has an instant messaging capability. I am coming up with the backend before I will employ someone to develop the mobile component. The main way for communication between the mobile device (client) and server will be via a websocket. I've heard that once an application is no longer in the foreground on mobile devices (both android and iOS) the websocket closes.I don't wantt to spend the time developing the server around websockets to realise i can't use this technology for this purpose down the line. Could someone please assist me with the following questions:

  1. 如果是这种情况(Web套接字在后台关闭),bumble之类的应用程序又如何继续显示近期通知?

  1. If this is the case (web socket closes in the background) how do applications like bumble and what's app continue to show near time notifications?

套接字如何在后台继续运行?有没有更好的更新技术来做到这一点?

How can the socket continue to run in the background? Is there there better newer technology for doing this?

更新 这里的问题是,当应用程序不在前台时,如何维护一个持久的websocket.我想了解更多有关在最小化应用程序时iOS和android如何处理情况的信息. websocket是否继续运行?还是关闭?是否必须使用其他库才能使其继续运行?

UPDATE The question here is how to maintain a persistent websocket when the applications isn't in the foreground. I would like to know more about how iOS and android handle the situation when an application is minimised. Do the websockets continue to run? or are they closed? Do other libraries have to be used to make this keep running?

推荐答案

ANDROID:

由于Android专注于高效使用电池的方式,因此当应用程序处于后台(依赖)时,系统会剥夺该应用程序的资源.最好使用工作管理器来处理您的后台任务.

As Android is focusing on the efficient way of using the battery, the system deprives the resources of the app when the app is in the background(Dependent). It is better to use the work manager to handle your background tasks.

看看 https://developer.android.com/topic/libraries/architecture/workmanager/basics.html

示例代码

class GetMessages(context: Context, params: WorkerParameters) : Worker(context, params) {

    override fun doWork(): Result {
        getAndSaveMessages()
        return Result.success()
    }

    private fun getAndSaveMessages() {
         // get messages here
    }
}

将此代码放在Singleton类中,以便从JobManager之类的任何地方访问它

Put this code in a Singleton class to access it from anywhere Like JobManager

    object JobManager{
 private fun syncMessages(){
        val loadMessages = PeriodicWorkRequestBuilder<GetMessages>(5, TimeUnit.MINUTES)
        // Add tag to cancel the thread any time
        loadMessages.addTag("MESSAGES") 

        val myConstraints = Constraints.Builder()
                        .setRequiredNetworkType(NetworkType.CONNECTED)
                        // Many other constraints are available, see the
                        // Constraints.Builder reference
                        .build()
        // Add constraints
        loadMessages.setConstraints(myConstraints)

        WorkManager.getInstance().enqueue(loadMessages.buld())
    }

}

现在您可以在任何地方使用JobManager.syncMessages()

Now you can use JobManager.syncMessages() from anywhere

这篇关于Android和iOS上的websockets的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持IT屋!

查看全文
登录 关闭
扫码关注1秒登录
发送“验证码”获取 | 15天全站免登陆