Created
February 20, 2016 23:49
-
-
Save terrysahaidak/a2827201086a9f8ee01c to your computer and use it in GitHub Desktop.
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
package com.terrysahaidak.faye; | |
import com.google.gson.JsonObject; | |
import com.amatkivskiy.gitter.sdk.async.faye.client.AsyncGitterFayeClient; | |
import com.amatkivskiy.gitter.sdk.async.faye.interfaces.ChannelListener; | |
import com.amatkivskiy.gitter.sdk.async.faye.interfaces.ConnectionListener; | |
import com.amatkivskiy.gitter.sdk.async.faye.interfaces.DisconnectionListener; | |
import com.amatkivskiy.gitter.sdk.async.faye.interfaces.Logger; | |
import com.amatkivskiy.gitter.sdk.async.faye.interfaces.DisconnectionListener; | |
import com.amatkivskiy.gitter.sdk.async.faye.interfaces.FailListener; | |
import com.facebook.react.bridge.ReactApplicationContext; | |
import com.facebook.react.bridge.ReactContextBaseJavaModule; | |
import com.facebook.react.bridge.ReactMethod; | |
import com.facebook.react.bridge.ReadableArray; | |
import com.facebook.react.bridge.Callback; | |
public class FayeGitterModule extends ReactContextBaseJavaModule { | |
private static String accessToken; | |
private static AsyncGitterFayeClient client; | |
public FayeGitterModule(ReactApplicationContext reactContext) { | |
super(reactContext); | |
} | |
@Override | |
public String getName() { | |
return "FayeGitter"; | |
} | |
private String getAccessToken() { | |
return accessToken; | |
} | |
@ReactMethod | |
public void setAccessToken(String token) { | |
accessToken = token; | |
} | |
@ReactMethod | |
public void create(final Callback onDisconectCallback, final Callback onFailCallback) { | |
this.client = new AsyncGitterFayeClient( | |
getAccessToken(), | |
new DisconnectionListener() { | |
@Override | |
public void onDisconnected() { | |
onDisconectCallback.invoke("Disconected"); | |
} | |
}, | |
new FailListener() { | |
@Override | |
public void onFailed(Exception ex) { | |
onFailCallback.invoke("Failed"); | |
} | |
} | |
); | |
} | |
@ReactMethod | |
public void connect(final Callback onConnectedCallback) { | |
this.client.connect(new ConnectionListener() { | |
@Override | |
public void onConnected() { | |
onConnectedCallback.invoke("Connected"); | |
} | |
}); | |
} | |
@ReactMethod | |
public void subscribe( | |
String channelName, | |
final Callback onMessageCallback, | |
final Callback onFailedCallback, | |
final Callback onSubscribedCallback, | |
final Callback onUnSubscribedCallback | |
) { | |
this.client.subscribe(channelName, new ChannelListener() { | |
@Override | |
public void onMessage(String channel,JsonObject message){ | |
onMessageCallback.invoke(message); | |
} | |
@Override | |
public void onFailed(String channel,Exception ex) { | |
onFailedCallback.invoke("Failed to subscribe to channel: " + channel); | |
onFailedCallback.invoke(ex); | |
} | |
@Override | |
public void onSubscribed(String channel) { | |
onSubscribedCallback.invoke("Subscribed to channel " + channel); | |
} | |
@Override | |
public void onUnSubscribed(String channel) { | |
onUnSubscribedCallback.invoke("Unsubscribed from channel: " + channel); | |
} | |
}); | |
} | |
@ReactMethod | |
public void unsubscribe(String channel) { | |
this.client.unSubscribe(channel); | |
} | |
@ReactMethod | |
public void disconnect() { | |
this.client.disconnect(); | |
} | |
@ReactMethod | |
public void logger(final Callback onLogCallback) { | |
this.client.setLogger(new Logger() { | |
@Override | |
public void log(String message) { | |
onLogCallback.invoke(message); | |
} | |
}); | |
} | |
} |
Sign up for free
to join this conversation on GitHub.
Already have an account?
Sign in to comment