use of com.koushikdutta.async.http.WebSocket in project simperium-android by Simperium.
the class AsyncWebSocketProvider method connect.
@Override
public void connect(final WebSocketManager.ConnectionListener listener) {
Uri uri = Uri.parse(String.format(AndroidClient.WEBSOCKET_URL, mAppId));
AsyncHttpRequest request = new AsyncHttpGet(uri);
request.setHeader(AndroidClient.USER_AGENT_HEADER, mSessionId);
// Protocol is null
mAsyncClient.websocket(request, null, new WebSocketConnectCallback() {
@Override
public void onCompleted(Exception ex, final WebSocket webSocket) {
if (ex != null) {
listener.onError(ex);
return;
}
if (webSocket == null) {
listener.onError(new IOException("WebSocket could not be opened"));
return;
}
final WebSocketManager.Connection connection = new WebSocketManager.Connection() {
@Override
public void close() {
mHandler.post(new Runnable() {
@Override
public void run() {
webSocket.close();
}
});
}
@Override
public void send(final String message) {
mHandler.post(new Runnable() {
@Override
public void run() {
webSocket.send(message);
}
});
}
};
webSocket.setStringCallback(new WebSocket.StringCallback() {
@Override
public void onStringAvailable(String s) {
listener.onMessage(s);
}
});
webSocket.setEndCallback(new CompletedCallback() {
@Override
public void onCompleted(Exception ex) {
listener.onDisconnect(ex);
}
});
webSocket.setClosedCallback(new CompletedCallback() {
@Override
public void onCompleted(Exception ex) {
listener.onDisconnect(ex);
}
});
listener.onConnect(connection);
}
});
}
Aggregations