Search in sources :

Example 1 with StringCallback

use of com.koushikdutta.async.http.WebSocket.StringCallback 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));
}
Also used : WebSocketConnectCallback(com.koushikdutta.async.http.AsyncHttpClient.WebSocketConnectCallback) StringCallback(com.koushikdutta.async.http.WebSocket.StringCallback) Semaphore(java.util.concurrent.Semaphore) WebSocket(com.koushikdutta.async.http.WebSocket)

Example 2 with StringCallback

use of com.koushikdutta.async.http.WebSocket.StringCallback 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);
                }
            });
        }
    });
}
Also used : CompletedCallback(com.koushikdutta.async.callback.CompletedCallback) WebSocketRequestCallback(com.koushikdutta.async.http.server.AsyncHttpServer.WebSocketRequestCallback) AsyncHttpServerRequest(com.koushikdutta.async.http.server.AsyncHttpServerRequest) AsyncHttpServer(com.koushikdutta.async.http.server.AsyncHttpServer) StringCallback(com.koushikdutta.async.http.WebSocket.StringCallback) WebSocket(com.koushikdutta.async.http.WebSocket)

Aggregations

WebSocket (com.koushikdutta.async.http.WebSocket)2 StringCallback (com.koushikdutta.async.http.WebSocket.StringCallback)2 CompletedCallback (com.koushikdutta.async.callback.CompletedCallback)1 WebSocketConnectCallback (com.koushikdutta.async.http.AsyncHttpClient.WebSocketConnectCallback)1 AsyncHttpServer (com.koushikdutta.async.http.server.AsyncHttpServer)1 WebSocketRequestCallback (com.koushikdutta.async.http.server.AsyncHttpServer.WebSocketRequestCallback)1 AsyncHttpServerRequest (com.koushikdutta.async.http.server.AsyncHttpServerRequest)1 Semaphore (java.util.concurrent.Semaphore)1