在 android 中实现 Socket.io 的最佳方式 [英] Best way to implement Socket.io in android

查看:100
本文介绍了在 android 中实现 Socket.io 的最佳方式的处理方法,对大家解决问题具有一定的参考价值,需要的朋友们下面随着小编来一起学习吧!

问题描述

我计划通过 this 库在 android 中实现 Socket.io,用于基于聊天的应用.据我了解,图书馆似乎还不错.我想知道如何在整个应用程序中始终保持套接字连接?在这里我列出了实现的方法,其中我需要最好的和稳定的方法.

I am planning to implement Socket.io in android by this library for a chat based application. As far as I understood the library seems to be pretty good. I want to know how to maintain a single socket connection throughout the app all the time? Here I have listed out ways to achieve, in which I need the best and stable way.

三种方式

MainApplication 类扩展了 Application

这样我们就有了一个很好的范围,即在主线程(或应用程序的生命周期)中维护套接字连接,并且每当需要从活动中获取套接字实例时,我们都可以轻松获取它.但它的主线程也是问题所在.它可能会阻塞主线程.

By this we have a good scope that the socket connection is maintained in the main thread( or application's life cycle) and whenever the socket instance is needed from the activity we can get it easily. But it's main thread which also the problem. It might block the main thread.

绑定服务

通过这种方式,我们可以绑定服务与活动,我们可以简单地使用它.在单独的线程中进行是实现 IO/网络调用的方式.但是跨进程传输比同进程直接访问开销更大.

By this way we can bind the service with the activities and we can simply use it. Doing in separate thread is the way to achieve IO/Network calls. But cross processing transfer is more expensive than directly accessing in same process.

单身

在 Singleton 中保持连接也很有意义.但是我们不知道实例何时被进程杀死,因为它在活动生命周期中不起作用.

Maintaining connection in Singleton also makes sense. But we don't know when the instance is killed by process, because it doesn't work in activity life cycle.

如果我说的有道理,请帮帮我.如果没有注释掉.

If I makes sense please help me out. If not comment it out.

我给出了更适合我的答案.

I have given the answer which is more suitable for me.

推荐答案

维护socket连接的服务

根据 Ofek Ron 提到的 ServiceBroadcaseReceiver 是一个比 BoundService更好的想法.因为保持沟通是一个繁琐的过程.而且我还推荐 pub/sub 广播方式,如 OttoEventBus(我自己建议 Otto by Square,这是干净而精彩的api).

Service for Maintaining socket connection

As per Ofek Ron mentioned Service with BroadcaseReceiver is a better idea than BoundService. Because its a tedious process to maintain communication. And I also recommend pub/sub way of broadcasting, like Otto or EventBus (I myself suggest Otto by Square, which is clean and brilliant api).

OTTO 的优点
1. 更清洁的 API
2. 您可以订阅和发布到任何ActivityFragmentService 类.
3.解耦.(您必须在代码中尽可能少地耦合).

Pros of OTTO
1. Cleaner Api
2. You can subscribe and publish in/to any Activity, Fragment, Service class.
3. Decoupling. (You have to couple as less as possible in your code).

还有一点是在 onStartCommand() 中使用 START_STICKY 在它被销毁后启动服务.请参阅参考.

And one more point is use START_STICKY in the onStartCommand() to start service after it is getting destroyed. See this reference.

最佳做法是在扩展 ApplicationMainApplication 中启动服务.因为当存在内存限制或用户从堆栈中强行关闭应用程序时,应用程序将被杀死.所以 onStartCommand() 不会像我们在 Activity 中实现的那样频繁调用.

It's best practice to start the service in the MainApplication that extends Application. Because the application will be killed when there is a memory constraint or user forcefully closes the app from the stack. So onStartCommand() will not be called frequently like if we have implemented in Activity.

只需在MainApplication类中实现Application.LifeCycleCallbacks即可实现在线状态,该类拥有activity的大部分生命周期回调,回调中会通知.通过这种方式,您无需任何样板代码即可实现 Online 状态.(如果有人需要帮助,请告诉我).

You can implement online status simply by implementing Application.LifeCycleCallbacks in the MainApplication class, which has most of the life cycle callbacks of activity and will be notified in the callback. By that you can implement Online status simply without any boiler plate codes. (If anyone need help here let me know).

最佳实践是通过 IntentService 实现,因为它运行在一个单独的线程中.我保证这将提供最佳性能,因为它是由 android 本身处理的,而不是像我们创建的线程那样.

Best practice is to implement by IntentService because it's running in the a separate thread. I promise which will give the best performance because it is handled by android itself, not like threads created by us.

这篇关于在 android 中实现 Socket.io 的最佳方式的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持IT屋!

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