Search in sources :

Example 11 with Session

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

the class CookiesTest method testCookiesAreSentToServer.

@Test
public void testCookiesAreSentToServer() throws Exception {
    final String cookieName = "name";
    final String cookieValue = "value";
    final String cookieString = cookieName + "=" + cookieValue;
    startServer(new EchoHandler() {

        @Override
        public Object createWebSocket(ServletUpgradeRequest request, ServletUpgradeResponse response) {
            List<HttpCookie> cookies = request.getCookies();
            assertThat("Cookies", cookies, notNullValue());
            assertThat("Cookies", cookies.size(), is(1));
            HttpCookie cookie = cookies.get(0);
            Assert.assertEquals(cookieName, cookie.getName());
            Assert.assertEquals(cookieValue, cookie.getValue());
            Map<String, List<String>> headers = request.getHeaders();
            // Test case insensitivity
            Assert.assertTrue(headers.containsKey("cookie"));
            List<String> values = headers.get("Cookie");
            Assert.assertNotNull(values);
            Assert.assertEquals(1, values.size());
            Assert.assertEquals(cookieString, values.get(0));
            return super.createWebSocket(request, response);
        }
    });
    WebSocketContainer container = ContainerProvider.getWebSocketContainer();
    ClientEndpointConfig.Builder builder = ClientEndpointConfig.Builder.create();
    builder.configurator(new ClientEndpointConfig.Configurator() {

        @Override
        public void beforeRequest(Map<String, List<String>> headers) {
            headers.put("Cookie", Collections.singletonList(cookieString));
        }
    });
    ClientEndpointConfig config = builder.build();
    Endpoint endPoint = new Endpoint() {

        @Override
        public void onOpen(Session session, EndpointConfig config) {
        }
    };
    Session session = container.connectToServer(endPoint, config, URI.create("ws://localhost:" + connector.getLocalPort()));
    session.close();
}
Also used : WebSocketContainer(javax.websocket.WebSocketContainer) ServletUpgradeResponse(org.eclipse.jetty.websocket.servlet.ServletUpgradeResponse) Endpoint(javax.websocket.Endpoint) List(java.util.List) ClientEndpointConfig(javax.websocket.ClientEndpointConfig) HttpCookie(java.net.HttpCookie) Map(java.util.Map) ServletUpgradeRequest(org.eclipse.jetty.websocket.servlet.ServletUpgradeRequest) EndpointConfig(javax.websocket.EndpointConfig) ClientEndpointConfig(javax.websocket.ClientEndpointConfig) Session(javax.websocket.Session) Test(org.junit.Test)

Example 12 with Session

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

the class MessageReceivingTest method testPartialTextMessage.

/**
     * Method tests receiving of text messages by parts.
     *
     * @throws Exception on exception occur
     */
@Test
public void testPartialTextMessage() throws Exception {
    final TestEndpoint echoer = new TestEndpoint(new PartialStringCaptureHandler());
    Assert.assertThat(echoer, instanceOf(javax.websocket.Endpoint.class));
    // Issue connect using instance of class that extends Endpoint
    final Session session = container.connectToServer(echoer, serverUri);
    if (LOG.isDebugEnabled())
        LOG.debug("Client Connected: {}", session);
    session.getBasicRemote().sendText("");
    session.getBasicRemote().sendText("Echo");
    if (LOG.isDebugEnabled())
        LOG.debug("Client Message Sent");
    echoer.handler.getMessageQueue().awaitMessages(2, 1000, TimeUnit.MILLISECONDS);
}
Also used : Endpoint(javax.websocket.Endpoint) Session(javax.websocket.Session) Test(org.junit.Test)

Example 13 with Session

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

the class ExampleClient method run.

private void run() throws DeploymentException, IOException, URISyntaxException, InterruptedException {
    WebSocketContainer container = ContainerProvider.getWebSocketContainer();
    System.out.printf("WebSocketContainer Impl: %s%n", container.getClass().getName());
    ExampleSocket socket = new ExampleSocket();
    URI uri = new URI("ws://echo.websocket.org/");
    Session session = container.connectToServer(socket, uri);
    RemoteEndpoint.Basic remote = session.getBasicRemote();
    String msg = "Hello world";
    System.out.printf("Sending: %s%n", Objects.toString(msg));
    remote.sendText(msg);
    // give remote 1 second to respond
    socket.messageLatch.await(1, TimeUnit.SECONDS);
    session.close();
    // give remote 1 second to acknowledge response
    socket.closeLatch.await(1, TimeUnit.SECONDS);
    System.out.println("Socket is closed");
}
Also used : WebSocketContainer(javax.websocket.WebSocketContainer) RemoteEndpoint(javax.websocket.RemoteEndpoint) URI(java.net.URI) Session(javax.websocket.Session)

Example 14 with Session

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

the class EndpointEchoTest method testAbstractEchoClassref.

@Test
public void testAbstractEchoClassref() throws Exception {
    WebSocketContainer container = ContainerProvider.getWebSocketContainer();
    // Issue connect using class reference (class that extends abstract that extends Endpoint)
    Session session = container.connectToServer(EchoStringEndpoint.class, serverUri);
    if (LOG.isDebugEnabled())
        LOG.debug("Client Connected: {}", session);
    session.getBasicRemote().sendText("Echo");
    if (LOG.isDebugEnabled())
        LOG.debug("Client Message Sent");
// TODO: figure out echo verification.
// echoer.messageQueue.awaitMessages(1,1000,TimeUnit.MILLISECONDS);
}
Also used : WebSocketContainer(javax.websocket.WebSocketContainer) Session(javax.websocket.Session) Test(org.junit.Test)

Example 15 with Session

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

the class EndpointEchoTest method testBasicEchoInstance.

@Test
public void testBasicEchoInstance() throws Exception {
    WebSocketContainer container = ContainerProvider.getWebSocketContainer();
    EndpointEchoClient echoer = new EndpointEchoClient();
    Assert.assertThat(echoer, instanceOf(javax.websocket.Endpoint.class));
    // Issue connect using instance of class that extends Endpoint
    Session session = container.connectToServer(echoer, serverUri);
    if (LOG.isDebugEnabled())
        LOG.debug("Client Connected: {}", session);
    session.getBasicRemote().sendText("Echo");
    if (LOG.isDebugEnabled())
        LOG.debug("Client Message Sent");
    echoer.textCapture.messageQueue.awaitMessages(1, 1000, TimeUnit.MILLISECONDS);
}
Also used : WebSocketContainer(javax.websocket.WebSocketContainer) EchoStringEndpoint(org.eclipse.jetty.websocket.jsr356.samples.EchoStringEndpoint) Session(javax.websocket.Session) Test(org.junit.Test)

Aggregations

Session (javax.websocket.Session)119 Test (org.junit.Test)93 URI (java.net.URI)74 WebSocketContainer (javax.websocket.WebSocketContainer)43 Endpoint (javax.websocket.Endpoint)39 CountDownLatch (java.util.concurrent.CountDownLatch)35 Context (org.apache.catalina.Context)31 DefaultServlet (org.apache.catalina.servlets.DefaultServlet)31 Tomcat (org.apache.catalina.startup.Tomcat)31 EndpointConfig (javax.websocket.EndpointConfig)29 ClientEndpointConfig (javax.websocket.ClientEndpointConfig)27 UndertowSession (io.undertow.websockets.jsr.UndertowSession)25 ServerEndpointConfig (javax.websocket.server.ServerEndpointConfig)23 ServerEndpoint (javax.websocket.server.ServerEndpoint)20 IOException (java.io.IOException)17 ServerWebSocketContainer (io.undertow.websockets.jsr.ServerWebSocketContainer)16 AnnotatedClientEndpoint (io.undertow.websockets.jsr.test.annotated.AnnotatedClientEndpoint)15 AtomicBoolean (java.util.concurrent.atomic.AtomicBoolean)15 AtomicReference (java.util.concurrent.atomic.AtomicReference)15 FrameChecker (io.undertow.websockets.utils.FrameChecker)14