use of org.eclipse.jetty.websocket.jsr356.ClientContainer in project jetty.project by eclipse.
the class OnPartialTest method toEventDriver.
public EventDriver toEventDriver(Object websocket) throws Throwable {
WebSocketPolicy policy = WebSocketPolicy.newServerPolicy();
policy.setInputBufferSize(1024);
policy.setMaxBinaryMessageBufferSize(1024);
policy.setMaxTextMessageBufferSize(1024);
// Create EventDriver
EventDriverImpl driverImpl = new JsrServerEndpointImpl();
Class<?> endpoint = websocket.getClass();
ServerEndpoint anno = endpoint.getAnnotation(ServerEndpoint.class);
Assert.assertThat("Endpoint: " + endpoint + " should be annotated with @ServerEndpoint", anno, notNullValue());
WebSocketContainerScope containerScope = new SimpleContainerScope(policy);
// Event Driver Factory
EventDriverFactory factory = new EventDriverFactory(containerScope);
factory.addImplementation(new JsrServerEndpointImpl());
ServerEndpointConfig config = new BasicServerEndpointConfig(containerScope, endpoint, "/");
AnnotatedServerEndpointMetadata metadata = new AnnotatedServerEndpointMetadata(containerScope, endpoint, config);
AnnotatedEndpointScanner<ServerEndpoint, ServerEndpointConfig> scanner = new AnnotatedEndpointScanner<>(metadata);
scanner.scan();
EndpointInstance ei = new EndpointInstance(websocket, config, metadata);
EventDriver driver = driverImpl.create(ei, policy);
Assert.assertThat("EventDriver", driver, notNullValue());
// Create Local JsrSession
String id = testname.getMethodName();
URI requestURI = URI.create("ws://localhost/" + id);
DummyConnection connection = new DummyConnection();
ClientContainer container = new ClientContainer();
container.start();
@SuppressWarnings("resource") JsrSession session = new JsrSession(container, id, requestURI, driver, connection);
session.setPolicy(policy);
session.start();
session.open();
return driver;
}
use of org.eclipse.jetty.websocket.jsr356.ClientContainer in project jetty.project by eclipse.
the class SessionTrackingTest method testAddRemoveSessions.
@Test
public void testAddRemoveSessions() throws Exception {
// Create Client
ClientContainer clientContainer = new ClientContainer();
QueuedThreadPool clientThreads = new QueuedThreadPool();
clientThreads.setName("client");
clientContainer.getClient().setExecutor(clientThreads);
try {
CountDownLatch openedLatch = new CountDownLatch(2);
CountDownLatch closedLatch = new CountDownLatch(2);
wsServerFactory.addSessionListener(new WebSocketSession.Listener() {
@Override
public void onOpened(WebSocketSession session) {
openedLatch.countDown();
}
@Override
public void onClosed(WebSocketSession session) {
closedLatch.countDown();
}
});
clientContainer.start();
// Establish connections
ClientSocket cli1 = new ClientSocket();
clientContainer.connectToServer(cli1, serverURI.resolve("/test"));
cli1.waitForOpen(1, TimeUnit.SECONDS);
// Establish new connection
ClientSocket cli2 = new ClientSocket();
clientContainer.connectToServer(cli2, serverURI.resolve("/test"));
cli2.waitForOpen(1, TimeUnit.SECONDS);
openedLatch.await(5, TimeUnit.SECONDS);
assertServerOpenConnectionCount(2);
// Establish close both connections
cli1.session.close();
cli2.session.close();
cli1.waitForClose(1, TimeUnit.SECONDS);
cli2.waitForClose(1, TimeUnit.SECONDS);
closedLatch.await(5, TimeUnit.SECONDS);
assertServerOpenConnectionCount(0);
} finally {
clientContainer.stop();
}
}
use of org.eclipse.jetty.websocket.jsr356.ClientContainer in project jetty.project by eclipse.
the class JsrSessionTest method initSession.
@Before
public void initSession() {
container = new ClientContainer();
String id = JsrSessionTest.class.getSimpleName();
URI requestURI = URI.create("ws://localhost/" + id);
WebSocketPolicy policy = WebSocketPolicy.newClientPolicy();
ClientEndpointConfig config = new EmptyClientEndpointConfig();
DummyEndpoint websocket = new DummyEndpoint();
SimpleEndpointMetadata metadata = new SimpleEndpointMetadata(websocket.getClass());
// Executor executor = null;
EndpointInstance ei = new EndpointInstance(websocket, config, metadata);
EventDriver driver = new JsrEndpointEventDriver(policy, ei);
DummyConnection connection = new DummyConnection();
session = new JsrSession(container, id, requestURI, driver, connection);
}
Aggregations