Search in sources :

Example 41 with WebSocketContainer

use of javax.websocket.WebSocketContainer in project javaee7-samples by javaee-samples.

the class TestRemoteClient method processRequest.

/**
     * Processes requests for both HTTP
     * <code>GET</code> and
     * <code>POST</code> methods.
     *
     * @param request servlet request
     * @param response servlet response
     * @throws ServletException if a servlet-specific error occurs
     * @throws IOException if an I/O error occurs
     */
protected void processRequest(HttpServletRequest request, HttpServletResponse response) throws ServletException, IOException {
    response.setContentType("text/html;charset=UTF-8");
    try (PrintWriter out = response.getWriter()) {
        out.println("<!DOCTYPE html>");
        out.println("<html>");
        out.println("<head>");
        out.println("<title>Servlet TestRemoteClient</title>");
        out.println("</head>");
        out.println("<body>");
        out.println("<h1>Servlet TestRemoteClient at " + request.getContextPath() + "</h1>");
        WebSocketContainer container = ContainerProvider.getWebSocketContainer();
        String uri = "ws://echo.websocket.org:80/";
        out.println("Connecting to " + uri);
        container.connectToServer(MyClient.class, URI.create(uri));
        out.println("<br><br>Look in server.log for log messages from message exchange between client/server.");
        out.println("</body>");
        out.println("</html>");
    } catch (DeploymentException ex) {
        Logger.getLogger(TestRemoteClient.class.getName()).log(Level.SEVERE, null, ex);
    }
}
Also used : WebSocketContainer(javax.websocket.WebSocketContainer) DeploymentException(javax.websocket.DeploymentException) PrintWriter(java.io.PrintWriter)

Example 42 with WebSocketContainer

use of javax.websocket.WebSocketContainer in project javaee7-samples by javaee-samples.

the class TestClient method processRequest.

/**
     * Processes requests for both HTTP
     * <code>GET</code> and
     * <code>POST</code> methods.
     *
     * @param request servlet request
     * @param response servlet response
     * @throws ServletException if a servlet-specific error occurs
     * @throws IOException if an I/O error occurs
     */
protected void processRequest(HttpServletRequest request, HttpServletResponse response) throws ServletException, IOException {
    response.setContentType("text/html;charset=UTF-8");
    try (PrintWriter out = response.getWriter()) {
        out.println("<html>");
        out.println("<head>");
        out.println("<title>Servlet TestServlet</title>");
        out.println("</head>");
        out.println("<body>");
        out.println("<h1>Servlet TestServlet at " + request.getContextPath() + "</h1>");
        WebSocketContainer container = ContainerProvider.getWebSocketContainer();
        String uri = "ws://localhost:" + request.getLocalPort() + request.getContextPath() + "/websocket";
        out.println("Connecting to " + uri);
        container.connectToServer(MyClient.class, ClientEndpointConfig.Builder.create().configurator(new MyConfigurator()).build(), URI.create(uri));
        out.println("<br><br>Look in server.log for message exchange between client/server and headers from configurator.");
        out.println("</body>");
        out.println("</html>");
    } catch (DeploymentException ex) {
        Logger.getLogger(TestClient.class.getName()).log(Level.SEVERE, null, ex);
    }
}
Also used : WebSocketContainer(javax.websocket.WebSocketContainer) DeploymentException(javax.websocket.DeploymentException) PrintWriter(java.io.PrintWriter)

Example 43 with WebSocketContainer

use of javax.websocket.WebSocketContainer 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 44 with WebSocketContainer

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

the class TestWsRemoteEndpointImplServer method testClientDropsConnection.

/*
     * https://bz.apache.org/bugzilla/show_bug.cgi?id=58624
     *
     * This test requires three breakpoints to be set. Two in this file (marked
     * A & B with comments) and one (C) at the start of
     * WsRemoteEndpointImplServer.doWrite().
     *
     * With the breakpoints in place, run this test.
     * Once breakpoints A & B are reached, progress the thread at breakpoint A
     * one line to close the connection.
     * Once breakpoint C is reached, allow the thread at breakpoint B to
     * continue.
     * Then allow the thread at breakpoint C to continue.
     *
     * In the failure mode, the thread at breakpoint B will not progress past
     * the call to sendObject(). If the issue is fixed, the thread at breakpoint
     * B will continue past sendObject() and terminate with a TimeoutException.
     */
@Test
public void testClientDropsConnection() throws Exception {
    Tomcat tomcat = getTomcatInstance();
    // No file system docBase required
    Context ctx = tomcat.addContext("", null);
    ctx.addApplicationListener(Bug58624Config.class.getName());
    Tomcat.addServlet(ctx, "default", new DefaultServlet());
    ctx.addServletMappingDecoded("/", "default");
    WebSocketContainer wsContainer = ContainerProvider.getWebSocketContainer();
    tomcat.start();
    SimpleClient client = new SimpleClient();
    URI uri = new URI("ws://localhost:" + getPort() + Bug58624Config.PATH);
    Session session = wsContainer.connectToServer(client, uri);
    // Break point A required on following line
    session.close();
}
Also used : Context(org.apache.catalina.Context) Tomcat(org.apache.catalina.startup.Tomcat) WebSocketContainer(javax.websocket.WebSocketContainer) SimpleClient(org.apache.tomcat.websocket.pojo.TesterUtil.SimpleClient) DefaultServlet(org.apache.catalina.servlets.DefaultServlet) URI(java.net.URI) Session(javax.websocket.Session) WebSocketBaseTest(org.apache.tomcat.websocket.WebSocketBaseTest) Test(org.junit.Test)

Example 45 with WebSocketContainer

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

the class AnnotatedEchoTest method testEcho.

@Test
public void testEcho() throws Exception {
    WebSocketContainer container = ContainerProvider.getWebSocketContainer();
    AnnotatedEchoClient echoer = new AnnotatedEchoClient();
    Session session = container.connectToServer(echoer, serverUri);
    session.getBasicRemote().sendText("Echo");
    echoer.messageQueue.awaitMessages(1, 1000, TimeUnit.MILLISECONDS);
}
Also used : WebSocketContainer(javax.websocket.WebSocketContainer) Session(javax.websocket.Session) Test(org.junit.Test)

Aggregations

WebSocketContainer (javax.websocket.WebSocketContainer)68 URI (java.net.URI)47 Session (javax.websocket.Session)43 Test (org.junit.Test)40 Context (org.apache.catalina.Context)32 Tomcat (org.apache.catalina.startup.Tomcat)32 DefaultServlet (org.apache.catalina.servlets.DefaultServlet)30 ClientEndpointConfig (javax.websocket.ClientEndpointConfig)20 Endpoint (javax.websocket.Endpoint)14 CountDownLatch (java.util.concurrent.CountDownLatch)13 ServerEndpoint (javax.websocket.server.ServerEndpoint)13 EndpointConfig (javax.websocket.EndpointConfig)11 TomcatBaseTest (org.apache.catalina.startup.TomcatBaseTest)10 ServerEndpointConfig (javax.websocket.server.ServerEndpointConfig)9 BasicText (org.apache.tomcat.websocket.TesterMessageCountClient.BasicText)9 TesterProgrammaticEndpoint (org.apache.tomcat.websocket.TesterMessageCountClient.TesterProgrammaticEndpoint)9 ClientEndpoint (javax.websocket.ClientEndpoint)8 DeploymentException (javax.websocket.DeploymentException)8 TesterEndpoint (org.apache.tomcat.websocket.TesterMessageCountClient.TesterEndpoint)7 ServerConfigListener (org.apache.tomcat.websocket.pojo.TesterUtil.ServerConfigListener)7