This is a very simple Primus client for Android. I've only tested with Primus setup using the SockJS transformer.
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
.
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>
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);
}
}
try {
JSONObject j = new JSONObject();
j.put("some", "data");
primus.send(j);
} catch (JSONException e) {
e.printStackTrace();
}
primus.end();
- Auto reconnecting using exponential backoff
- heartbeat
- converting messages to a
JSONObject
- waiting until connection is ready to send messages