Search in sources :

Example 1 with JsrSession

use of org.eclipse.jetty.websocket.jsr356.JsrSession 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);
        }
    }
}
Also used : IOException(java.io.IOException) Extension(javax.websocket.Extension) UpgradeListener(org.eclipse.jetty.websocket.client.io.UpgradeListener) EmptyClientEndpointConfig(org.eclipse.jetty.websocket.jsr356.client.EmptyClientEndpointConfig) ClientEndpointConfig(javax.websocket.ClientEndpointConfig) ClientUpgradeRequest(org.eclipse.jetty.websocket.client.ClientUpgradeRequest) ExecutionException(java.util.concurrent.ExecutionException) WebSocketSession(org.eclipse.jetty.websocket.common.WebSocketSession) Session(javax.websocket.Session)

Example 2 with JsrSession

use of org.eclipse.jetty.websocket.jsr356.JsrSession 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;
}
Also used : WebSocketPolicy(org.eclipse.jetty.websocket.api.WebSocketPolicy) EventDriverFactory(org.eclipse.jetty.websocket.common.events.EventDriverFactory) ServerEndpointConfig(javax.websocket.server.ServerEndpointConfig) WebSocketContainerScope(org.eclipse.jetty.websocket.common.scopes.WebSocketContainerScope) DummyConnection(org.eclipse.jetty.websocket.common.test.DummyConnection) ClientContainer(org.eclipse.jetty.websocket.jsr356.ClientContainer) EndpointInstance(org.eclipse.jetty.websocket.jsr356.endpoints.EndpointInstance) JsrSession(org.eclipse.jetty.websocket.jsr356.JsrSession) ServerEndpoint(javax.websocket.server.ServerEndpoint) URI(java.net.URI) EventDriver(org.eclipse.jetty.websocket.common.events.EventDriver) AnnotatedEndpointScanner(org.eclipse.jetty.websocket.jsr356.annotations.AnnotatedEndpointScanner) SimpleContainerScope(org.eclipse.jetty.websocket.common.scopes.SimpleContainerScope) EventDriverImpl(org.eclipse.jetty.websocket.common.events.EventDriverImpl)

Example 3 with JsrSession

use of org.eclipse.jetty.websocket.jsr356.JsrSession in project jetty.project by eclipse.

the class ExtensionStackProcessingTest method testDeflateFrameExtension.

@Test
public void testDeflateFrameExtension() throws Exception {
    assumeDeflateFrameAvailable();
    ClientEndpointConfig config = ClientEndpointConfig.Builder.create().extensions(Arrays.<Extension>asList(new JsrExtension("deflate-frame"))).build();
    final String content = "deflate_me";
    final CountDownLatch messageLatch = new CountDownLatch(1);
    URI uri = URI.create("ws://localhost:" + connector.getLocalPort());
    Session session = client.connectToServer(new EndpointAdapter() {

        @Override
        public void onMessage(String message) {
            Assert.assertEquals(content, message);
            messageLatch.countDown();
        }
    }, config, uri);
    // Make sure everything is wired properly.
    OutgoingFrames firstOut = ((JsrSession) session).getOutgoingHandler();
    Assert.assertTrue(firstOut instanceof ExtensionStack);
    ExtensionStack extensionStack = (ExtensionStack) firstOut;
    Assert.assertTrue(extensionStack.isRunning());
    OutgoingFrames secondOut = extensionStack.getNextOutgoing();
    Assert.assertTrue(secondOut instanceof DeflateFrameExtension);
    DeflateFrameExtension deflateExtension = (DeflateFrameExtension) secondOut;
    Assert.assertTrue(deflateExtension.isRunning());
    OutgoingFrames thirdOut = deflateExtension.getNextOutgoing();
    Assert.assertTrue(thirdOut instanceof WebSocketClientConnection);
    final CountDownLatch latch = new CountDownLatch(1);
    session.getAsyncRemote().sendText(content, new SendHandler() {

        @Override
        public void onResult(SendResult result) {
            latch.countDown();
        }
    });
    Assert.assertTrue(latch.await(5, TimeUnit.SECONDS));
    Assert.assertTrue(messageLatch.await(5, TimeUnit.SECONDS));
}
Also used : SendHandler(javax.websocket.SendHandler) WebSocketClientConnection(org.eclipse.jetty.websocket.client.io.WebSocketClientConnection) JsrSession(org.eclipse.jetty.websocket.jsr356.JsrSession) CountDownLatch(java.util.concurrent.CountDownLatch) URI(java.net.URI) ExtensionStack(org.eclipse.jetty.websocket.common.extensions.ExtensionStack) JsrExtension(org.eclipse.jetty.websocket.jsr356.JsrExtension) Extension(javax.websocket.Extension) DeflateFrameExtension(org.eclipse.jetty.websocket.common.extensions.compress.DeflateFrameExtension) JsrExtension(org.eclipse.jetty.websocket.jsr356.JsrExtension) SendResult(javax.websocket.SendResult) OutgoingFrames(org.eclipse.jetty.websocket.api.extensions.OutgoingFrames) ClientEndpointConfig(javax.websocket.ClientEndpointConfig) DeflateFrameExtension(org.eclipse.jetty.websocket.common.extensions.compress.DeflateFrameExtension) Session(javax.websocket.Session) JsrSession(org.eclipse.jetty.websocket.jsr356.JsrSession) Test(org.junit.Test)

Example 4 with JsrSession

use of org.eclipse.jetty.websocket.jsr356.JsrSession 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);
}
Also used : WebSocketPolicy(org.eclipse.jetty.websocket.api.WebSocketPolicy) DummyEndpoint(org.eclipse.jetty.websocket.jsr356.samples.DummyEndpoint) SimpleEndpointMetadata(org.eclipse.jetty.websocket.jsr356.client.SimpleEndpointMetadata) DummyConnection(org.eclipse.jetty.websocket.common.test.DummyConnection) EndpointInstance(org.eclipse.jetty.websocket.jsr356.endpoints.EndpointInstance) URI(java.net.URI) EmptyClientEndpointConfig(org.eclipse.jetty.websocket.jsr356.client.EmptyClientEndpointConfig) EventDriver(org.eclipse.jetty.websocket.common.events.EventDriver) JsrEndpointEventDriver(org.eclipse.jetty.websocket.jsr356.endpoints.JsrEndpointEventDriver) JsrEndpointEventDriver(org.eclipse.jetty.websocket.jsr356.endpoints.JsrEndpointEventDriver) EmptyClientEndpointConfig(org.eclipse.jetty.websocket.jsr356.client.EmptyClientEndpointConfig) ClientEndpointConfig(javax.websocket.ClientEndpointConfig) Before(org.junit.Before)

Aggregations

URI (java.net.URI)3 ClientEndpointConfig (javax.websocket.ClientEndpointConfig)3 Extension (javax.websocket.Extension)2 Session (javax.websocket.Session)2 WebSocketPolicy (org.eclipse.jetty.websocket.api.WebSocketPolicy)2 EventDriver (org.eclipse.jetty.websocket.common.events.EventDriver)2 DummyConnection (org.eclipse.jetty.websocket.common.test.DummyConnection)2 JsrSession (org.eclipse.jetty.websocket.jsr356.JsrSession)2 EmptyClientEndpointConfig (org.eclipse.jetty.websocket.jsr356.client.EmptyClientEndpointConfig)2 EndpointInstance (org.eclipse.jetty.websocket.jsr356.endpoints.EndpointInstance)2 IOException (java.io.IOException)1 CountDownLatch (java.util.concurrent.CountDownLatch)1 ExecutionException (java.util.concurrent.ExecutionException)1 SendHandler (javax.websocket.SendHandler)1 SendResult (javax.websocket.SendResult)1 ServerEndpoint (javax.websocket.server.ServerEndpoint)1 ServerEndpointConfig (javax.websocket.server.ServerEndpointConfig)1 OutgoingFrames (org.eclipse.jetty.websocket.api.extensions.OutgoingFrames)1 ClientUpgradeRequest (org.eclipse.jetty.websocket.client.ClientUpgradeRequest)1 UpgradeListener (org.eclipse.jetty.websocket.client.io.UpgradeListener)1