Я уже просмотрел много постов в StackOverflow и их проблемы с git.Проблема почти со всеми из них связана с заголовком для HttpAuthorizer.Но это все еще не работает для меня.Может кто-нибудь помочь мне найти, что не так с моим кодом?Спасибо.
Примечание: я не знаю, есть ли ошибка на стороне сервера, потому что, когда моя команда проверила ее с помощью простого приложения чата из браузера, она работает.
public class PusherService {
private static final String CHANNEL = "private-User." + accountId;
private static PusherService instance;
private Pusher pusher;
public static synchronized PusherService getInstance() {
if (instance == null) {
instance = new PusherService();
}
return instance;
}
private PusherService() {
pusher = initiatePusher();
}
private static Pusher initiatePusher() {
HttpAuthorizer httpAuthorizer = new HttpAuthorizer(BuildConfig.PUSHER_AUTH_URL);
Map<String, String> headers = new HashMap<>();
headers.put("Authorization", Session.getToken());
headers.put("Accept", "application/json");
headers.put("Content-Type", "application/json");
httpAuthorizer.setHeaders(headers);
PusherOptions options = new PusherOptions()
.setCluster(BuildConfig.PUSHER_CLUSTER)
.setEncrypted(true)
.setAuthorizer(httpAuthorizer);
Pusher pusher = new Pusher(BuildConfig.PUSHER_KEY, options);
pusher.connect(new ConnectionEventListener() {
@Override
public void onConnectionStateChange(ConnectionStateChange connectionStateChange) {
Log.i("Pusher", "Connection State Change: " + connectionStateChange.getCurrentState().name());
}
@Override
public void onError(String s, String s1, Exception e) {
Log.i("Pusher", String.format("Connection Error: [%s], exception: [%s]", s, e));
}
});
return pusher;
}
public PrivateChannel getChannel() {
PrivateChannel privateChannel = pusher.getPrivateChannel(CHANNEL);
if (privateChannel == null) {
privateChannel = pusher.subscribePrivate(CHANNEL, new PrivateChannelEventListener() {
@Override
public void onAuthenticationFailure(String s, Exception e) {
// Authentication failed
Log.i("Pusher", String.format("Authentication failure due to [%s], exception: [%s]", s, e));
}
@Override
public void onSubscriptionSucceeded(String s) {
Log.i("Pusher", "Private connection succeeded");
}
@Override
public void onEvent(String channel, String event, String data) {
Log.i("Pusher", data);
}
});
}
return privateChannel;
}
}
Тогдачтобы проверить это, я просто вызвал
PusherService.getInstance (). getChannel ();