Search in sources :

Example 21 with ClientEndpointConfig

use of javax.websocket.ClientEndpointConfig in project undertow by undertow-io.

the class TestMessagesReceivedInOrder method testMessagesReceivedInOrder.

@Test
public void testMessagesReceivedInOrder() throws Exception {
    stacks.clear();
    EchoSocket.receivedEchos = new FutureResult<>();
    final ClientEndpointConfig clientEndpointConfig = ClientEndpointConfig.Builder.create().build();
    final CountDownLatch done = new CountDownLatch(1);
    final AtomicReference<String> error = new AtomicReference<>();
    ContainerProvider.getWebSocketContainer().connectToServer(new Endpoint() {

        @Override
        public void onOpen(final Session session, EndpointConfig endpointConfig) {
            try {
                RemoteEndpoint.Basic rem = session.getBasicRemote();
                List<String> messages = new ArrayList<>();
                for (int i = 0; i < MESSAGES; i++) {
                    byte[] data = new byte[2048];
                    (new Random()).nextBytes(data);
                    String crc = md5(data);
                    rem.sendBinary(ByteBuffer.wrap(data));
                    messages.add(crc);
                }
                List<String> received = EchoSocket.receivedEchos.getIoFuture().get();
                StringBuilder sb = new StringBuilder();
                boolean fail = false;
                for (int i = 0; i < messages.size(); i++) {
                    if (received.size() <= i) {
                        fail = true;
                        sb.append(i + ": should be " + messages.get(i) + " but is empty.");
                    } else {
                        if (!messages.get(i).equals(received.get(i))) {
                            fail = true;
                            sb.append(i + ": should be " + messages.get(i) + " but is " + received.get(i) + " (but found at " + received.indexOf(messages.get(i)) + ").");
                        }
                    }
                }
                if (fail) {
                    error.set(sb.toString());
                }
                done.countDown();
            } catch (Throwable t) {
                System.out.println(t);
            }
        }
    }, clientEndpointConfig, new URI(DefaultServer.getDefaultServerURL() + "/webSocket"));
    done.await(30, TimeUnit.SECONDS);
    if (error.get() != null) {
        Assert.fail(error.get());
    }
}
Also used : AtomicReference(java.util.concurrent.atomic.AtomicReference) CountDownLatch(java.util.concurrent.CountDownLatch) URI(java.net.URI) Endpoint(javax.websocket.Endpoint) RemoteEndpoint(javax.websocket.RemoteEndpoint) ServerEndpoint(javax.websocket.server.ServerEndpoint) Random(java.util.Random) ArrayList(java.util.ArrayList) List(java.util.List) CopyOnWriteArrayList(java.util.concurrent.CopyOnWriteArrayList) ClientEndpointConfig(javax.websocket.ClientEndpointConfig) ClientEndpointConfig(javax.websocket.ClientEndpointConfig) EndpointConfig(javax.websocket.EndpointConfig) Session(javax.websocket.Session) Test(org.junit.Test)

Example 22 with ClientEndpointConfig

use of javax.websocket.ClientEndpointConfig in project undertow by undertow-io.

the class BinaryEndpointTest method testBytesOnMessage.

@org.junit.Test
public void testBytesOnMessage() throws Exception {
    SSLContext context = DefaultServer.getClientSSLContext();
    ProgramaticClientEndpoint endpoint = new ProgramaticClientEndpoint();
    ClientEndpointConfig clientEndpointConfig = ClientEndpointConfig.Builder.create().build();
    clientEndpointConfig.getUserProperties().put(DefaultWebSocketClientSslProvider.SSL_CONTEXT, context);
    ContainerProvider.getWebSocketContainer().connectToServer(endpoint, clientEndpointConfig, new URI("wss://" + DefaultServer.getHostAddress("default") + ":" + DefaultServer.getHostSSLPort("default") + "/partial"));
    Assert.assertArrayEquals(bytes, endpoint.getResponses().poll(15, TimeUnit.SECONDS));
    endpoint.session.close();
    endpoint.closeLatch.await(10, TimeUnit.SECONDS);
}
Also used : SSLContext(javax.net.ssl.SSLContext) ClientEndpointConfig(javax.websocket.ClientEndpointConfig) URI(java.net.URI)

Example 23 with ClientEndpointConfig

use of javax.websocket.ClientEndpointConfig in project spring-framework by spring-projects.

the class StandardWebSocketClient method executeInternal.

private Mono<Void> executeInternal(URI url, HttpHeaders requestHeaders, WebSocketHandler handler) {
    MonoProcessor<Void> completionMono = MonoProcessor.create();
    return Mono.fromCallable(() -> {
        String[] subProtocols = beforeHandshake(url, requestHeaders, handler);
        DefaultConfigurator configurator = new DefaultConfigurator(requestHeaders);
        Endpoint endpoint = createEndpoint(url, handler, completionMono, configurator);
        ClientEndpointConfig config = createEndpointConfig(configurator, subProtocols);
        return this.webSocketContainer.connectToServer(endpoint, config, url);
    }).subscribeOn(// connectToServer is blocking
    Schedulers.elastic()).then(completionMono);
}
Also used : Endpoint(javax.websocket.Endpoint) ClientEndpointConfig(javax.websocket.ClientEndpointConfig)

Example 24 with ClientEndpointConfig

use of javax.websocket.ClientEndpointConfig in project tomcat by apache.

the class TestShutdown method testShutdownBufferedMessages.

@Test
public void testShutdownBufferedMessages() throws Exception {
    Tomcat tomcat = getTomcatInstance();
    // No file system docBase required
    Context ctx = tomcat.addContext("", null);
    ctx.addApplicationListener(EchoBufferedConfig.class.getName());
    Tomcat.addServlet(ctx, "default", new DefaultServlet());
    ctx.addServletMappingDecoded("/", "default");
    tomcat.start();
    WebSocketContainer wsContainer = ContainerProvider.getWebSocketContainer();
    ClientEndpointConfig clientEndpointConfig = ClientEndpointConfig.Builder.create().build();
    Session wsSession = wsContainer.connectToServer(TesterProgrammaticEndpoint.class, clientEndpointConfig, new URI("ws://localhost:" + getPort() + "/test"));
    CountDownLatch latch = new CountDownLatch(1);
    BasicText handler = new BasicText(latch);
    wsSession.addMessageHandler(handler);
    wsSession.getBasicRemote().sendText("Hello");
    int count = 0;
    while (count < 10 && EchoBufferedEndpoint.messageCount.get() == 0) {
        Thread.sleep(200);
        count++;
    }
    Assert.assertNotEquals("Message not received by server", EchoBufferedEndpoint.messageCount.get(), 0);
    tomcat.stop();
    Assert.assertTrue("Latch expired waiting for message", latch.await(10, TimeUnit.SECONDS));
}
Also used : Context(org.apache.catalina.Context) BasicText(org.apache.tomcat.websocket.TesterMessageCountClient.BasicText) Tomcat(org.apache.catalina.startup.Tomcat) WebSocketContainer(javax.websocket.WebSocketContainer) DefaultServlet(org.apache.catalina.servlets.DefaultServlet) ClientEndpointConfig(javax.websocket.ClientEndpointConfig) CountDownLatch(java.util.concurrent.CountDownLatch) URI(java.net.URI) TesterProgrammaticEndpoint(org.apache.tomcat.websocket.TesterMessageCountClient.TesterProgrammaticEndpoint) ServerEndpoint(javax.websocket.server.ServerEndpoint) Session(javax.websocket.Session) Test(org.junit.Test) WebSocketBaseTest(org.apache.tomcat.websocket.WebSocketBaseTest)

Example 25 with ClientEndpointConfig

use of javax.websocket.ClientEndpointConfig in project jetty.project by eclipse.

the class ClientContainer method getClientEndpointMetadata.

public EndpointMetadata getClientEndpointMetadata(Class<?> endpoint, EndpointConfig config) {
    synchronized (endpointClientMetadataCache) {
        EndpointMetadata metadata = endpointClientMetadataCache.get(endpoint);
        if (metadata != null) {
            return metadata;
        }
        ClientEndpoint anno = endpoint.getAnnotation(ClientEndpoint.class);
        if (anno != null) {
            // Annotated takes precedence here
            AnnotatedClientEndpointMetadata annoMetadata = new AnnotatedClientEndpointMetadata(this, endpoint);
            AnnotatedEndpointScanner<ClientEndpoint, ClientEndpointConfig> scanner = new AnnotatedEndpointScanner<>(annoMetadata);
            scanner.scan();
            metadata = annoMetadata;
        } else if (Endpoint.class.isAssignableFrom(endpoint)) {
            // extends Endpoint
            @SuppressWarnings("unchecked") Class<? extends Endpoint> eendpoint = (Class<? extends Endpoint>) endpoint;
            metadata = new SimpleEndpointMetadata(eendpoint, config);
        } else {
            StringBuilder err = new StringBuilder();
            err.append("Not a recognized websocket [");
            err.append(endpoint.getName());
            err.append("] does not extend @").append(ClientEndpoint.class.getName());
            err.append(" or extend from ").append(Endpoint.class.getName());
            throw new InvalidWebSocketException(err.toString());
        }
        endpointClientMetadataCache.put(endpoint, metadata);
        return metadata;
    }
}
Also used : SimpleEndpointMetadata(org.eclipse.jetty.websocket.jsr356.client.SimpleEndpointMetadata) InvalidWebSocketException(org.eclipse.jetty.websocket.api.InvalidWebSocketException) Endpoint(javax.websocket.Endpoint) ClientEndpoint(javax.websocket.ClientEndpoint) AnnotatedEndpointScanner(org.eclipse.jetty.websocket.jsr356.annotations.AnnotatedEndpointScanner) EmptyClientEndpointConfig(org.eclipse.jetty.websocket.jsr356.client.EmptyClientEndpointConfig) ClientEndpointConfig(javax.websocket.ClientEndpointConfig) ClientEndpoint(javax.websocket.ClientEndpoint) AnnotatedClientEndpointMetadata(org.eclipse.jetty.websocket.jsr356.client.AnnotatedClientEndpointMetadata) SimpleEndpointMetadata(org.eclipse.jetty.websocket.jsr356.client.SimpleEndpointMetadata) AnnotatedClientEndpointMetadata(org.eclipse.jetty.websocket.jsr356.client.AnnotatedClientEndpointMetadata) EndpointMetadata(org.eclipse.jetty.websocket.jsr356.metadata.EndpointMetadata)

Aggregations

ClientEndpointConfig (javax.websocket.ClientEndpointConfig)44 Test (org.junit.Test)26 URI (java.net.URI)25 Session (javax.websocket.Session)19 Endpoint (javax.websocket.Endpoint)13 CountDownLatch (java.util.concurrent.CountDownLatch)12 WebSocketContainer (javax.websocket.WebSocketContainer)12 ClientEndpoint (javax.websocket.ClientEndpoint)8 Context (org.apache.catalina.Context)7 DefaultServlet (org.apache.catalina.servlets.DefaultServlet)7 Tomcat (org.apache.catalina.startup.Tomcat)7 ArrayList (java.util.ArrayList)6 EndpointConfig (javax.websocket.EndpointConfig)6 Extension (javax.websocket.Extension)6 BasicText (org.apache.tomcat.websocket.TesterMessageCountClient.BasicText)6 AnnotatedClientEndpointMetadata (org.eclipse.jetty.websocket.jsr356.client.AnnotatedClientEndpointMetadata)5 List (java.util.List)4 ServerEndpoint (javax.websocket.server.ServerEndpoint)4 AnnotatedEndpointScanner (org.eclipse.jetty.websocket.jsr356.annotations.AnnotatedEndpointScanner)4 EmptyClientEndpointConfig (org.eclipse.jetty.websocket.jsr356.client.EmptyClientEndpointConfig)4