Coder Social home page Coder Social logo

primus-android's Introduction

Primus Android Library

This is a very simple Primus client for Android. I've only tested with Primus setup using the SockJS transformer.

Installation

Add the following to your build.gradle.

dependencies {
  compile 'io.cine:primus:0.0.4'
}

Ensure Maven central is included in your build.gradle. This should happen by default when building a project with Google's recommended Android IDE, Android Studio.

apply plugin: 'android'
buildscript {
  repositories {
    mavenCentral()
  }
}
repositories {
  mavenCentral()
}

Download primus-android to your application with ./gradlew build.

Usage

AndroidManifest.xml

Primus uses the internet. We need permission for that.

<manifest xmlns:android="http://schemas.android.com/apk/res/android"
  package="com.example.yourpackage">
  <!-- Make sure to include the internet permission -->
  <uses-permission android:name="android.permission.INTERNET" />
</manifest>

Initialization

import io.cine.primus.Primus;

public class MainActivity extends Activity {
  private Primus primus;
  private final String PRIMUS_URL = "http://example.com/primus";
  @Override
  protected void onCreate(Bundle savedInstanceState) {
    primus = Primus.connect(this, PRIMUS_URL);
    Primus.PrimusOpenCallback openCallback = new Primus.PrimusOpenCallback() {
      @Override
      public void onOpen() {
        // Websocket open
      }
    };
    Primus.PrimusDataCallback dataCallback = new Primus.PrimusDataCallback() {
      @Override
      public void onData(JSONObject data) {
        //got data
      }
    });
    primus.setOpenCallback(openCallback);
    primus.setDataCallback(dataCallback);
  }
}

Sending data

try {
  JSONObject j = new JSONObject();
  j.put("some", "data");
  primus.send(j);
} catch (JSONException e) {
  e.printStackTrace();
}

Ending

  primus.end();

Features

  1. Auto reconnecting using exponential backoff
  • heartbeat
  • converting messages to a JSONObject
  • waiting until connection is ready to send messages

primus-android's People

Contributors

trshafer avatar

Watchers

 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.