Search in sources :

Example 1 with WebSocketTransport

use of com.koushikdutta.async.http.socketio.transport.WebSocketTransport in project AndroidAsync by koush.

the class SocketIOConnection method reconnect.

void reconnect(final DependentCancellable child) {
    if (isConnected()) {
        return;
    }
    // if a connection is in progress, just wait.
    if (connecting != null && !connecting.isDone() && !connecting.isCancelled()) {
        if (child != null)
            child.setParent(connecting);
        return;
    }
    request.logi("Reconnecting socket.io");
    connecting = httpClient.executeString(request, null).then(new TransformFuture<SocketIOTransport, String>() {

        @Override
        protected void transform(String result) throws Exception {
            String[] parts = result.split(":");
            final String sessionId = parts[0];
            if (!"".equals(parts[1]))
                heartbeat = Integer.parseInt(parts[1]) / 2 * 1000;
            else
                heartbeat = 0;
            String transportsLine = parts[3];
            String[] transports = transportsLine.split(",");
            HashSet<String> set = new HashSet<String>(Arrays.asList(transports));
            final SimpleFuture<SocketIOTransport> transport = new SimpleFuture<SocketIOTransport>();
            if (set.contains("websocket")) {
                final String sessionUrl = Uri.parse(request.getUri().toString()).buildUpon().appendPath("websocket").appendPath(sessionId).build().toString();
                httpClient.websocket(sessionUrl, null, null).setCallback(new FutureCallback<WebSocket>() {

                    @Override
                    public void onCompleted(Exception e, WebSocket result) {
                        if (e != null) {
                            transport.setComplete(e);
                            return;
                        }
                        transport.setComplete(new WebSocketTransport(result, sessionId));
                    }
                });
            } else if (set.contains("xhr-polling")) {
                final String sessionUrl = Uri.parse(request.getUri().toString()).buildUpon().appendPath("xhr-polling").appendPath(sessionId).build().toString();
                XHRPollingTransport xhrPolling = new XHRPollingTransport(httpClient, sessionUrl, sessionId);
                transport.setComplete(xhrPolling);
            } else {
                throw new SocketIOException("transport not supported");
            }
            setComplete(transport);
        }
    }).setCallback(new FutureCallback<SocketIOTransport>() {

        @Override
        public void onCompleted(Exception e, SocketIOTransport result) {
            if (e != null) {
                reportDisconnect(e);
                return;
            }
            reconnectDelay = request.config.reconnectDelay;
            SocketIOConnection.this.transport = result;
            attach();
        }
    });
    if (child != null)
        child.setParent(connecting);
}
Also used : WebSocket(com.koushikdutta.async.http.WebSocket) SocketIOTransport(com.koushikdutta.async.http.socketio.transport.SocketIOTransport) WebSocketTransport(com.koushikdutta.async.http.socketio.transport.WebSocketTransport) XHRPollingTransport(com.koushikdutta.async.http.socketio.transport.XHRPollingTransport) FutureCallback(com.koushikdutta.async.future.FutureCallback) HashSet(java.util.HashSet) SimpleFuture(com.koushikdutta.async.future.SimpleFuture)

Aggregations

FutureCallback (com.koushikdutta.async.future.FutureCallback)1 SimpleFuture (com.koushikdutta.async.future.SimpleFuture)1 WebSocket (com.koushikdutta.async.http.WebSocket)1 SocketIOTransport (com.koushikdutta.async.http.socketio.transport.SocketIOTransport)1 WebSocketTransport (com.koushikdutta.async.http.socketio.transport.WebSocketTransport)1 XHRPollingTransport (com.koushikdutta.async.http.socketio.transport.XHRPollingTransport)1 HashSet (java.util.HashSet)1