use of com.koushikdutta.async.http.socketio.transport.SocketIOTransport 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);
}
use of com.koushikdutta.async.http.socketio.transport.SocketIOTransport in project AndroidAsync by koush.
the class SocketIOConnection method setupHeartbeat.
void setupHeartbeat() {
Runnable heartbeatRunner = new Runnable() {
@Override
public void run() {
final SocketIOTransport ts = transport;
if (heartbeat <= 0 || ts == null || !ts.isConnected())
return;
ts.send("2:::");
ts.getServer().postDelayed(this, heartbeat);
}
};
heartbeatRunner.run();
}
use of com.koushikdutta.async.http.socketio.transport.SocketIOTransport in project AndroidAsync by koush.
the class SocketIOConnection method acknowledge.
private Acknowledge acknowledge(final String _messageId, final String endpoint) {
if (TextUtils.isEmpty(_messageId))
return null;
final String messageId = _messageId.replaceAll("\\+$", "");
return new Acknowledge() {
@Override
public void acknowledge(JSONArray arguments) {
String data = "";
if (arguments != null)
data += "+" + arguments.toString();
SocketIOTransport transport = SocketIOConnection.this.transport;
if (transport == null) {
final Exception e = new SocketIOException("not connected to server");
select(endpoint, new SelectCallback() {
@Override
public void onSelect(SocketIOClient client) {
ExceptionCallback callback = client.exceptionCallback;
if (callback != null)
callback.onException(e);
}
});
return;
}
transport.send(String.format(Locale.ENGLISH, "6:::%s%s", messageId, data));
}
};
}
use of com.koushikdutta.async.http.socketio.transport.SocketIOTransport in project AndroidAsync by koush.
the class SocketIOConnection method disconnect.
public void disconnect(SocketIOClient client) {
clients.remove(client);
// see if we can leave this endpoint completely
boolean needsEndpointDisconnect = true;
for (SocketIOClient other : clients) {
// we can't disconnect
if (TextUtils.equals(other.endpoint, client.endpoint) || TextUtils.isEmpty(client.endpoint)) {
needsEndpointDisconnect = false;
break;
}
}
final SocketIOTransport ts = transport;
if (needsEndpointDisconnect && ts != null)
ts.send(String.format(Locale.ENGLISH, "0::%s", client.endpoint));
// and see if we can disconnect the socket completely
if (clients.size() > 0 || ts == null)
return;
ts.setStringCallback(null);
ts.setClosedCallback(null);
ts.disconnect();
transport = null;
}
Aggregations