use of com.koushikdutta.async.http.WebSocket 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.WebSocket in project AndroidAsync by koush.
the class WebSocketTests method testWebSocket.
public void testWebSocket() throws Exception {
final Semaphore semaphore = new Semaphore(0);
AsyncHttpClient.getDefaultInstance().websocket("http://localhost:5000/ws", null, new WebSocketConnectCallback() {
@Override
public void onCompleted(Exception ex, WebSocket webSocket) {
webSocket.send("hello");
webSocket.setStringCallback(new StringCallback() {
@Override
public void onStringAvailable(String s) {
assertEquals(s, "hello");
semaphore.release();
}
});
}
});
assertTrue(semaphore.tryAcquire(TIMEOUT, TimeUnit.MILLISECONDS));
}
use of com.koushikdutta.async.http.WebSocket in project AndroidAsync by koush.
the class IssueWithWebSocketFuturesTests method testWebSocketFutureWithHandshakeFailureCallback.
//testing that websocket callback gets called with the correct parameters.
public void testWebSocketFutureWithHandshakeFailureCallback() throws Exception {
//creating a faulty server!
AsyncHttpServer httpServer = new AsyncHttpServer();
httpServer.websocket(".*", new AsyncHttpServer.WebSocketRequestCallback() {
@Override
public void onConnected(WebSocket webSocket, AsyncHttpServerRequest request) {
}
});
httpServer.listen(6666);
final Exception[] callbackException = { null };
final WebSocket[] callbackWs = { null };
final CountDownLatch countDownLatch = new CountDownLatch(1);
//for some reason, it fails with a WebSocketHandshakeException.
//But in general, if the handshake fails, the callback must be called with an exception.
Future<WebSocket> wsFuture = AsyncHttpClient.getDefaultInstance().websocket("ws://127.0.0.1:6666", "ws", new AsyncHttpClient.WebSocketConnectCallback() {
@Override
public void onCompleted(Exception ex, WebSocket webSocket) {
callbackException[0] = ex;
callbackWs[0] = webSocket;
countDownLatch.countDown();
}
});
//wait for the future to complete
countDownLatch.await();
//exactly one mut be null
assertTrue(callbackWs[0] == null ^ callbackException[0] == null);
//callback parameters must be the same as the future's result
assertEquals(wsFuture.tryGet(), callbackWs[0]);
assertEquals(wsFuture.tryGetException(), callbackException[0]);
}
use of com.koushikdutta.async.http.WebSocket in project K6nele by Kaljurand.
the class WebSocketRecognitionService method startSocket.
/**
* Opens the socket and starts recording/sending.
*
* @param url Webservice URL
*/
void startSocket(String url) {
mIsEosSent = false;
Log.i(url);
AsyncHttpClient client = AsyncHttpClient.getDefaultInstance();
if (false) {
//http://stackoverflow.com/questions/37804816/androidasync-how-to-create-ssl-client-in-websocket-connection
AsyncSSLSocketMiddleware sslSocketMiddleware = new AsyncSSLSocketMiddleware(client);
SSLContext sslContext = null;
try {
sslContext = getSSLContext();
} catch (NoSuchAlgorithmException e) {
e.printStackTrace();
} catch (KeyManagementException e) {
e.printStackTrace();
}
sslSocketMiddleware.setSSLContext(sslContext);
client.insertMiddleware(sslSocketMiddleware);
}
client.websocket(url, PROTOCOL, new AsyncHttpClient.WebSocketConnectCallback() {
@Override
public void onCompleted(Exception ex, final WebSocket webSocket) {
mWebSocket = webSocket;
if (ex != null) {
handleException(ex);
return;
}
webSocket.setStringCallback(new WebSocket.StringCallback() {
public void onStringAvailable(String s) {
Log.i(s);
handleResult(s);
}
});
webSocket.setClosedCallback(new CompletedCallback() {
@Override
public void onCompleted(Exception ex) {
if (ex == null) {
Log.e("ClosedCallback");
handleFinish(mIsEosSent);
} else {
Log.e("ClosedCallback: ", ex);
handleException(ex);
}
}
});
webSocket.setEndCallback(new CompletedCallback() {
@Override
public void onCompleted(Exception ex) {
if (ex == null) {
Log.e("EndCallback");
handleFinish(mIsEosSent);
} else {
Log.e("EndCallback: ", ex);
handleException(ex);
}
}
});
startSending(webSocket);
}
});
}
use of com.koushikdutta.async.http.WebSocket in project AndroidAsync by koush.
the class WebSocketTests method setUp.
@Override
protected void setUp() throws Exception {
super.setUp();
httpServer = new AsyncHttpServer();
httpServer.setErrorCallback(new CompletedCallback() {
@Override
public void onCompleted(Exception ex) {
fail();
}
});
httpServer.listen(AsyncServer.getDefault(), 5000);
httpServer.websocket("/ws", new WebSocketRequestCallback() {
@Override
public void onConnected(final WebSocket webSocket, AsyncHttpServerRequest request) {
webSocket.setStringCallback(new StringCallback() {
@Override
public void onStringAvailable(String s) {
webSocket.send(s);
}
});
}
});
}
Aggregations