android 应用程序关闭时如何保持网络套接字连接处于活动状态 [英] How to keep web socket connection active when android app is closed

查看:36
本文介绍了android 应用程序关闭时如何保持网络套接字连接处于活动状态的处理方法,对大家解决问题具有一定的参考价值,需要的朋友们下面随着小编来一起学习吧!

问题描述

我正在开发一个应用程序,该应用程序可以向 websocket 发送消息,以便在计算机内部特定参数发生更改时启用通知.计算机中的参数发生变化,通过 websocket 传输到我的应用程序.这部分是正确的,对吧?

I'm looking to make an app that sends a message to a websocket to enable notifications for when a particular parameter changes inside a computer. The parameter changes in the computer, travels through the websocket, and goes to my application. This part is correct, right?

即使我的应用程序关闭,我如何接收这些 websocket 通知?这就是背景数据吧?这应该很简单吗?我的应用程序在关闭时不需要向 Web 套接字发送消息.它需要做的就是在关闭时接收消息.理想情况下,它会说在参数发生变化时告诉我!"安装,然后在设备上安装时永远保持连接.

How do I receive these websocket notifications even when my app is closed? This is what background data is, right? Should this be simple? My application doesn't need to send messages to the web socket when it's closed. All it needs to do is receive messages when it's closed. Ideally, it would say 'Tell me when parameter changes!' on installation, then stay connected forever and ever while it's installed on the device.

此外,网络套接字由第三方托管,因此我无法访问它可能提供的任何服务器设置.

Also, the web socket is hosted by a third party, so I don't have access to any server settings it might offer.

我觉得我在所有这些中都缺少一个重要的概念.消息系统在关闭时如何接收推送通知?通过网络套接字,对吗?因此,即使消息应用关闭,它也会接收数据?

I feel like i'm missing an important concept in all of this. How do messaging systems receive push notifications when they're off? Through a web socket, right? So even when a messaging app is closed, it receives data?

推荐答案

过去几周我一直在研究类似的问题.这就是我所做的:

I have been working on a similar problem the past couple of weeks. This is what I have done:

  1. 我使用 AutoBahn Android 来实现 WebSocket.我怀疑在任何其他实现中的工作方式应该有点相似.
  2. 基本上有 3 个调用来实现 websocket 处理程序:
  1. I am using AutoBahn Android for WebSocket implementation. I suspect the workings should be somewhat similar in any other implemention.
  2. There are basically 3 calls to implement the websocket handler:
  1. onOpen()
  2. onReceiveMessage()
  3. onClose()

  • 您在 websocket 连接中使用的 3 个调用:

  • And 3 calls that you use in a websocket connection:

    1. connect()
    2. 断开连接()
    3. sendMessage()

  • 现在这就是棘手的地方!您必须保持连接处于活动状态.想到的第一个解决方案是明显的Service.但是 Service 本身并不是一个很好的方式,因为它会在你的应用程序的 UI 线程上工作.大多数情况下,它会在不打扰您或对您造成任何伤害的情况下做自己的事情.但是,当您确实必须打开应用程序的用户界面时(我有一个界面,而且它对我很重要),这会让您无休止地抱怨!无论如何,我似乎无法以非常优雅的方式实现它(我的意思是我曾经多次失去连接).
  • 因此,我扩展了一个 AsyncTask,并在 doInBackground() 方法中调用了一个名为 mWSConnectionStart() 的函数,它基本上要求 WebSocketConnection 连接并执行其操作.同时,我从 AsyncTask 中 postUpdate() 以防我需要构建一个 Notification.
  • Now this is where it gets tricky! You have to keep the connection alive. The first solution that comes to mind is an obvious Service. But a Service in itself won't be such a great way because it would be working on the UI thread of your application. Most of the time it would do its thing without disturbing you or causing you any harm. However, when you do have to open your application's User Interface, (I have an interface, and its important to me), it would cause you no end of grievance! And anyways, I couldn't seem to implement it in a very polished manner (by that I mean I used to lose the connection many times).
  • So I extended an AsyncTask and in the doInBackground() method I called a function called mWSConnectionStart() which basically asked the WebSocketConnection to connect and do its thing. In the meanwhile, I postUpdate() from the AsyncTask in case I need to build a Notification.
  • 这对我来说效果很好,我希望这个方法也适合你.

    This has been working well for me, and I hope this method works for you as well.

    PS:我一直无法解决的问题是 CPU 使用率高.祝你过得更好!

    PS: A problem that I have been unable to solve is that of high CPU utilization. May you fare better!

    PSS:更新:我刚刚在我的设备上看到,Whatsapp(我一直在对我的应用程序性能进行基准测试)比我的应用程序具有更高的 CPU 利用率(电池)!可能跟 WebSockets 有关系.

    PSS: UPDATE: I just saw that on my device, Whatsapp (which I have been benchmarking my apps performance against) has a higher CPU utilization (battery) than my app! Maybe it has something to do with WebSockets.

    我真的建议使用 GCM 而不是 WebSocketConnections.我们正计划自己转向 GCM.原因是它不需要太多的服务器端维护,而且它可以自动化我们的很多任务.

    这篇关于android 应用程序关闭时如何保持网络套接字连接处于活动状态的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持IT屋!

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