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);
}
}
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);
}
}
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));
}
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();
}
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);
}
Aggregations