Search in sources :

Example 1 with AbstractWebSocket

use of org.jivesoftware.smack.websocket.impl.AbstractWebSocket in project Smack by igniterealtime.

the class WebSocketConnectionAttemptState method establishWebSocketConnection.

private void establishWebSocketConnection(List<? extends WebSocketRemoteConnectionEndpoint> webSocketEndpoints, List<Exception> failures) throws InterruptedException {
    final int endpointCount = webSocketEndpoints.size();
    List<SmackFuture<AbstractWebSocket, Exception>> futures = new ArrayList<>(endpointCount);
    {
        List<AbstractWebSocket> webSockets = new ArrayList<>(endpointCount);
        // First only create the AbstractWebSocket instances, in case a constructor throws.
        for (WebSocketRemoteConnectionEndpoint endpoint : webSocketEndpoints) {
            AbstractWebSocket webSocket = webSocketFactory.create(endpoint, connectionInternal);
            webSockets.add(webSocket);
        }
        for (AbstractWebSocket webSocket : webSockets) {
            SmackFuture<AbstractWebSocket, Exception> future = webSocket.getFuture();
            futures.add(future);
        }
    }
    SmackFuture.await(futures, connectionInternal.connection.getReplyTimeout());
    for (SmackFuture<AbstractWebSocket, Exception> future : futures) {
        AbstractWebSocket connectedWebSocket = future.getIfAvailable();
        if (connectedWebSocket == null) {
            Exception exception = future.getExceptionIfAvailable();
            assert exception != null;
            failures.add(exception);
            continue;
        }
        if (webSocket == null) {
            webSocket = connectedWebSocket;
            // futures and close remaining successfully connected ones.
            continue;
        }
        connectedWebSocket.disconnect(1000, "Using other connection endpoint at " + webSocket.getEndpoint());
    }
}
Also used : SmackFuture(org.jivesoftware.smack.SmackFuture) WebSocketRemoteConnectionEndpoint(org.jivesoftware.smack.websocket.rce.WebSocketRemoteConnectionEndpoint) AbstractWebSocket(org.jivesoftware.smack.websocket.impl.AbstractWebSocket) ArrayList(java.util.ArrayList) List(java.util.List) ArrayList(java.util.ArrayList) WebSocketRemoteConnectionEndpoint(org.jivesoftware.smack.websocket.rce.WebSocketRemoteConnectionEndpoint)

Example 2 with AbstractWebSocket

use of org.jivesoftware.smack.websocket.impl.AbstractWebSocket in project Smack by igniterealtime.

the class WebSocketFactoryServiceTestUtil method createWebSocketTest.

public static void createWebSocketTest(Class<? extends AbstractWebSocket> expected) throws URISyntaxException {
    WebSocketRemoteConnectionEndpoint endpoint = WebSocketRemoteConnectionEndpoint.from("wss://example.org");
    ModularXmppClientToServerConnectionInternal connectionInternal = mock(ModularXmppClientToServerConnectionInternal.class);
    AbstractWebSocket websocket = WebSocketFactoryService.createWebSocket(endpoint, connectionInternal);
    assertEquals(expected, websocket.getClass());
}
Also used : ModularXmppClientToServerConnectionInternal(org.jivesoftware.smack.c2s.internal.ModularXmppClientToServerConnectionInternal) WebSocketRemoteConnectionEndpoint(org.jivesoftware.smack.websocket.rce.WebSocketRemoteConnectionEndpoint) AbstractWebSocket(org.jivesoftware.smack.websocket.impl.AbstractWebSocket)

Aggregations

AbstractWebSocket (org.jivesoftware.smack.websocket.impl.AbstractWebSocket)2 WebSocketRemoteConnectionEndpoint (org.jivesoftware.smack.websocket.rce.WebSocketRemoteConnectionEndpoint)2 ArrayList (java.util.ArrayList)1 List (java.util.List)1 SmackFuture (org.jivesoftware.smack.SmackFuture)1 ModularXmppClientToServerConnectionInternal (org.jivesoftware.smack.c2s.internal.ModularXmppClientToServerConnectionInternal)1