Port of JavaScript client provided by https://github.com/websocket-rails/websocket-rails
Built on top of AndroidAsync
Refer to https://github.com/websocket-rails/websocket-rails to learn more about WebSocketRails
Refer to https://github.com/koush/AndroidAsync to learn more about AndroidAsync
Download the latest JAR or grab via Maven:
<dependency>
<groupId>br.net.bmobile</groupId>
<artifactId>websocketrails-android</artifactId>
<version>(insert latest version)</version>
</dependency>
Gradle:
dependencies {
compile 'br.net.bmobile:websocketrails-android:1.+'
}
Since data exchange is JSON based, it's strongly recommended to use Jackson API to deserialize data.
private WebSocketRailsDispatcher dispatcher;
...
try {
dispatcher = new WebSocketRailsDispatcher(new URL("http://192.168.100.109:3000/websocket"));
dispatcher.connect();
}
catch (MalformedURLException e) {
// TODO Auto-generated catch block
e.printStackTrace();
}
...
private static WebSocketRailsChannel webSocketRailsChannel;
...
webSocketRailsChannel = dispatcher.subscribe("chanelName");
...
private static WebSocketRailsChannel webSocketRailsChannel;
...
Message message = new Message(); // Create a class with getter and setter as in Jackson API
message.setName("Charles");
message.setMessage("Hai");
webSocketRailsChannel.trigger("new_message", message);
...
...
webSocketRailsChannel.bind("new_message", new WebSocketRailsDataCallback() {
@Override
public void onDataAvailable(Object data) {
// Do what you want with the data received.
}
}
...
...
dispatcher.unSubscribe("chanelName");
...
...
dispatcher.disconnect();
...