GithubHelp home page GithubHelp logo

netty-dovakin-android-client's Introduction

关于SDK

Dovakin Client SDK For Android,对Android应用与集成Dovakin推送的服务之间的连接调用进行了良好的封装,通过基于EventBus 3.0的事件总线机制,提供了包括TCP连接状态改变、数据获取等回调事件,无需关注更多细节

依赖

你可以在这里下载SDK的最新release版本 通过File->Project Structure->Dependencies->Add Jardependency操作来添加SDK依赖

除了SDK本身,你仍然需要在你的build.gradle中添加如下代码,添加Netty,   GsonEventBus3.0的支持

Gradle

    compile 'io.netty:netty-all:4.1.13.Final'
    compile 'com.google.code.gson:gson:2.8.1'
    compile 'org.greenrobot:eventbus:3.0.0'

如何开始

Step 1: 注册EventBus

你需要将你的Service类注册到EventBus总线上,并通过接收 BaseEventChannelEvent 事件,来编写SDK事件的回调处理

EventBus.getDefault().register(this);

数据接收回调处理:

    @Subscribe(threadMode = ThreadMode.MAIN)
    public void handleNotify(BaseEvent baseEvent){
        switch (baseEvent.getEventType()){
            case EventType.AUTH:
                // do something
                break;
            case EventType.AUTH_SUCCESS:
                // do something
                break;
            case EventType.AUTH_FAILED:
                // do something
                break;
            case EventType.HEART:
                // do something
                break;
            case EventType.PUSH:
                // do something
                break;
        }
    }

TCP连接状态改变回调处理:

    @Subscribe(threadMode = ThreadMode.MAIN)
    public void handleChannelEvent(ChannelEvent channelEvent){
        String eventType = channelEvent.getEventType();
        if(eventType.equals(EventType.ON_ACTIVE)){
            
        } else if (eventType.equals(EventType.ON_INACTIVE)){
            
        } else if(eventType.equals(EventType.ON_EXCEPTION)){

        }
    }

SDK中提供了几种内置事件状态,可以通过比对 EventType 中的静态变量来对事件类型进行判别 EventType

type 类型说明 类型
PUSH 推送 Integer
AUTH 鉴权 Integer
AUTH_SUCCESS 鉴权成功 Integer
AUTH_FAILED   鉴权失败 Integer
HEART 心跳回执 Integer
ON_ACTIVE TCP连接建立 String
ON_INACTIVE TCP连接断开 String
ON_EXCEPTION 服务器异常 String

Step 2: 启动NGLSClient

NGLS是Dovakin内置的一个简易自定义推送协议,它提供了最简单的推送协议功能

NGLSClient的启动过程是异步非阻塞的,所以你可以自由选择合适的时机在主线程中进行启动,启动代码很简单,下面是一个完整的例子

client = NGLSClient.init(MainActivity.this)
    .address("192.168.0.1", 9999)
    .setHeartBeatTimeIdle(160);
client.start();

NGLSClient中提供了一些链式配置函数

函数名 函数作用 参数 是否必须
init 初始化 上下文实例
address 配置远程服务HOST及PORT HOST/PORT
setHeartBeatTimeIdle 设置心跳间隔 心跳间隔(单位:秒) 否 
addListener 通过添加实现了NotifyService接口的实例来以内置回调的方式监听各类回调事件 实现了NotifyService接口的实例

Step 3: 通过NGLSClient.login()鉴权(注册NGLS连接)

关于鉴权的逻辑,根据集成Dovakin服务的后端逻辑不同而不通,目前仅提供最简单的实现,即明文传输UserName Password

    AuthAction authAction = new AuthAction();
    authAction.setClientId(/** your username*/);
    authAction.setPassword(/** your password*/);
    try {
        client.login(authAction);
    } catch (AuthParamInvailbleException e) {
        e.printStackTrace();
    } catch (ClientInitFailedException e) {
        e.printStackTrace();
    }

netty-dovakin-android-client's People

Contributors

dovakinlink avatar

Stargazers

 avatar

Watchers

 avatar  avatar

Recommend Projects

  • React photo React

    A declarative, efficient, and flexible JavaScript library for building user interfaces.

  • Vue.js photo Vue.js

    🖖 Vue.js is a progressive, incrementally-adoptable JavaScript framework for building UI on the web.

  • Typescript photo Typescript

    TypeScript is a superset of JavaScript that compiles to clean JavaScript output.

  • TensorFlow photo TensorFlow

    An Open Source Machine Learning Framework for Everyone

  • Django photo Django

    The Web framework for perfectionists with deadlines.

  • D3 photo D3

    Bring data to life with SVG, Canvas and HTML. 📊📈🎉

Recommend Topics

  • javascript

    JavaScript (JS) is a lightweight interpreted programming language with first-class functions.

  • web

    Some thing interesting about web. New door for the world.

  • server

    A server is a program made to process requests and deliver data to clients.

  • Machine learning

    Machine learning is a way of modeling and interpreting data that allows a piece of software to respond intelligently.

  • Game

    Some thing interesting about game, make everyone happy.

Recommend Org

  • Facebook photo Facebook

    We are working to build community through open source technology. NB: members must have two-factor auth.

  • Microsoft photo Microsoft

    Open source projects and samples from Microsoft.

  • Google photo Google

    Google ❤️ Open Source for everyone.

  • D3 photo D3

    Data-Driven Documents codes.