use of javax.websocket.Session in project tomcat by apache.
the class TestClassLoader method testSimple.
/*
* Checks class loader for the server endpoint during onOpen and onMessage
*/
@Test
public void testSimple() throws Exception {
Tomcat tomcat = getTomcatInstance();
// No file system docBase required
Context ctx = tomcat.addContext("", null);
ctx.addApplicationListener(Config.class.getName());
Tomcat.addServlet(ctx, "default", new DefaultServlet());
ctx.addServletMappingDecoded("/", "default");
tomcat.start();
WebSocketContainer wsContainer = ContainerProvider.getWebSocketContainer();
Client client = new Client();
Session wsSession = wsContainer.connectToServer(client, new URI("ws://localhost:" + getPort() + "/test"));
Assert.assertTrue(wsSession.isOpen());
// Wait up to 5s for a message
int count = 0;
while (count < 50 && client.getMsgCount() < 1) {
Thread.sleep(100);
}
// Check it
Assert.assertEquals(1, client.getMsgCount());
Assert.assertFalse(client.hasFailed());
wsSession.getBasicRemote().sendText("Testing");
// Wait up to 5s for a message
count = 0;
while (count < 50 && client.getMsgCount() < 2) {
Thread.sleep(100);
}
Assert.assertEquals(2, client.getMsgCount());
Assert.assertFalse(client.hasFailed());
wsSession.close();
}
use of javax.websocket.Session in project tomcat by apache.
the class TestWebSocketFrameClient method echoTester.
public void echoTester(String path) throws Exception {
WebSocketContainer wsContainer = ContainerProvider.getWebSocketContainer();
ClientEndpointConfig clientEndpointConfig = ClientEndpointConfig.Builder.create().build();
Session wsSession = wsContainer.connectToServer(TesterProgrammaticEndpoint.class, clientEndpointConfig, new URI("ws://localhost:" + getPort() + path));
CountDownLatch latch = new CountDownLatch(1);
BasicText handler = new BasicText(latch);
wsSession.addMessageHandler(handler);
wsSession.getBasicRemote().sendText("Hello");
boolean latchResult = handler.getLatch().await(10, TimeUnit.SECONDS);
Assert.assertTrue(latchResult);
Queue<String> messages = handler.getMessages();
Assert.assertEquals(1, messages.size());
for (String message : messages) {
Assert.assertEquals("Hello", message);
}
wsSession.close();
}
use of javax.websocket.Session in project tomcat by apache.
the class TestWsPingPongMessages method testPingPongMessages.
@Test
public void testPingPongMessages() throws Exception {
Tomcat tomcat = getTomcatInstance();
// No file system docBase required
Context ctx = tomcat.addContext("", null);
ctx.addApplicationListener(TesterEchoServer.Config.class.getName());
Tomcat.addServlet(ctx, "default", new DefaultServlet());
ctx.addServletMappingDecoded("/", "default");
tomcat.start();
WebSocketContainer wsContainer = ContainerProvider.getWebSocketContainer();
Session wsSession = wsContainer.connectToServer(TesterProgrammaticEndpoint.class, ClientEndpointConfig.Builder.create().build(), new URI("ws://localhost:" + getPort() + TesterEchoServer.Config.PATH_ASYNC));
CountDownLatch latch = new CountDownLatch(1);
TesterEndpoint tep = (TesterEndpoint) wsSession.getUserProperties().get("endpoint");
tep.setLatch(latch);
PongMessageHandler handler = new PongMessageHandler(latch);
wsSession.addMessageHandler(handler);
wsSession.getBasicRemote().sendPing(applicationData);
boolean latchResult = handler.getLatch().await(10, TimeUnit.SECONDS);
Assert.assertTrue(latchResult);
Assert.assertArrayEquals(applicationData.array(), (handler.getMessages().peek()).getApplicationData().array());
}
use of javax.websocket.Session in project che by eclipse.
the class MessagesReSender method resend.
public void resend(String endpointId) {
final List<String> messages = messagesMap.remove(endpointId);
if (messages == null || messages.isEmpty()) {
return;
}
final Optional<Session> sessionOptional = registry.get(endpointId);
if (!sessionOptional.isPresent()) {
return;
}
final Session session = sessionOptional.get();
final List<String> backing = new ArrayList<>(messages);
messages.clear();
for (String message : backing) {
if (session.isOpen()) {
session.getAsyncRemote().sendText(message);
} else {
messages.add(message);
}
}
messagesMap.put(endpointId, messages);
}
use of javax.websocket.Session in project jetty.project by eclipse.
the class ClientContainer method connect.
private Session connect(EndpointInstance instance, URI path) throws IOException {
Objects.requireNonNull(instance, "EndpointInstance cannot be null");
Objects.requireNonNull(path, "Path cannot be null");
ClientEndpointConfig config = (ClientEndpointConfig) instance.getConfig();
ClientUpgradeRequest req = new ClientUpgradeRequest();
UpgradeListener upgradeListener = null;
for (Extension ext : config.getExtensions()) {
req.addExtensions(new JsrExtensionConfig(ext));
}
if (config.getPreferredSubprotocols().size() > 0) {
req.setSubProtocols(config.getPreferredSubprotocols());
}
if (config.getConfigurator() != null) {
upgradeListener = new JsrUpgradeListener(config.getConfigurator());
}
Future<org.eclipse.jetty.websocket.api.Session> futSess = client.connect(instance, path, req, upgradeListener);
try {
return (JsrSession) futSess.get();
} catch (InterruptedException e) {
throw new IOException("Connect failure", e);
} catch (ExecutionException e) {
// Unwrap Actual Cause
Throwable cause = e.getCause();
if (cause instanceof IOException) {
// Just rethrow
throw (IOException) cause;
} else {
throw new IOException("Connect failure", cause);
}
}
}
Aggregations