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

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

问题描述

我计划实施Socket.io Android中通过基于聊了一会这个库应用。据我了解图书馆似乎是pretty的好。我想知道如何保持插座在整个应用程序所有的时间连接?在这里,我列出了的方式来实现,在此我需要最好的和稳定的方式。

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 类扩展的应用

通过这一点,我们有一个很好的范围,该套接字连接保持在主线(或应用程序的生命周期),每当插座实例从活动的需要,我们可以很容易地得到它。但它的主线程也问题。它可能会阻止主线程。

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.

BoundService

通过这种方式,我们可以绑定与活动的服务,我们可以简单地使用它。在做单独的线程是实现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.

辛格尔顿

在辛格尔顿维护方面也是情理之中。但是,我们不知道什么时候该实例被杀害的过程,因为它不活动的生命周期的工作。

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.

推荐答案

根据奥菲克罗恩提到服务与BroadcaseReceivers是的想法比 BoundService 。因为它是一个繁琐的过程,以保持沟通。我还建议广电的pub / sub 的方式,如奥托 EventBus (我自己认为的奥托按广场,这是干净的,辉煌的API)。

Service for Maintaining socket connection

As per Ofek Ron mentioned Service with BroadcaseReceivers 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).

奥托
的优点 1.更简洁的API
2.您可以订阅和发布/任何活动,片段,服务类。
3. 去耦。 (你要对夫妇尽可能少在code)。

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).

还有一处是使用 START_STICKY onStartCommand 后启动服务它是被破坏掉了。请参见参考。

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

这是要启动 MainApplication 扩展应用服务的最佳实践。因为当有内存限制或用户强行关闭从堆栈中的应用程序的应用程序将被杀死。因此, onStartCommand 将不会被频繁调用一样,如果我们的活动已经实施。

It's best practice to start the service in the MainApplication 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.

您可以简单地通过实施 Application.LifeCycleCallbacks MainApplication 类,其中有大部分的实现在线状态活动的生命周期回调,将通知回调。通过,你可以简单地实现在线状态无任何锅炉板codeS。 (如果有人需要帮助这里让我知道)。

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 ,因为它是在一个单独的线程运行来实现。我保证,这将提供最佳性能,因为它是由机器人本身处理,而不是像我们所创建的线程。

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天全站免登陆