Trying to implement this in application. Slack RTM After call to rtm.start I get JSON-response and extract wss url. After it, following documentation I need to open websocket. Tried two libraries
compile 'com.koushikdutta.async:androidasync:2.+'
compile 'org.java-websocket:Java-WebSocket:1.3.0'
First gives TimeoutException, second - enters onClose method with i = -1, b = true (onError or onOpen are not called, see code below).
connection = (HttpURLConnection) new URL(getQuery(URL + "rtm.start",
new Pair<>("token", TOKEN))).openConnection();
final String response = readIs(connection.getInputStream());
JSONObject jResponse = new JSONObject(response);
String uri = (String) jResponse.get("url");
With Java-WebSocket I've tried this code (standard from example on github):
connectWebSocket(uri);
private void connectWebSocket(String uriStr) {
URI uri;
try {
uri = new URI(uriStr);
} catch (URISyntaxException e) {
e.printStackTrace();
return;
}
log(uri.toString());
log(uri.toASCIIString());
mWebSocketClient = new WebSocketClient(uri) {
@Override
public void onOpen(ServerHandshake serverHandshake) {
log("Opened");
mWebSocketClient.send("Hello from " + Build.MANUFACTURER + " " + Build.MODEL);
}
@Override
public void onMessage(String s) {
log(s);
}
@Override
public void onClose(int i, String s, boolean b) {
log("Closed " + s + " " + b + " number: " + i);
}
@Override
public void onError(Exception e) {
log("Error " + e.getMessage());
}
};
mWebSocketClient.connect();
}
As I've said above - only onClose is called.
With AndroidAsync tried standard code from example too:
AsyncHttpClient.getDefaultInstance().websocket(uri, "wss", (ex, webSocket) -> {
if (ex != null) {
log(ex.getMessage());
return;
}
webSocket.send("a string");
});
I've never worked with websockets on android before, and can't understand what am I doing wrong. Will appreciate your help.
Url in JSON is of type of
wss://ms156.slack-msgs.com/websocket/LYBUMUtG-bqj9HkKwEB5Yk_DCyPeRbnZ4viUJYHzrzJnLr-M74d46IQ9khTF8rik-v6ckJ4hqXPRi666hyPR6pismYBZBsggJUade3LOARc=
Finally my coleague managed to open this socket using OkHttp. If somebody is interested - code of test project is provided below: http://pastebin.com/3YhLjAn7 What is interesting - in the end we decided not to use RTM, because load for one connection is too high - you receive events from all chats and there is no way to set filter only for one specific.