Search in sources :

Example 81 with Session

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

the class TextStreamTest method testEcho.

private void testEcho(int size) throws Exception {
    char[] data = randomChars(size);
    URI uri = URI.create("ws://localhost:" + connector.getLocalPort() + PATH);
    ClientTextStreamer client = new ClientTextStreamer();
    Session session = wsClient.connectToServer(client, uri);
    try (Writer output = session.getBasicRemote().getSendWriter()) {
        output.write(data);
    }
    Assert.assertTrue(client.await(5, TimeUnit.SECONDS));
    Assert.assertArrayEquals(data, client.getEcho());
}
Also used : URI(java.net.URI) Writer(java.io.Writer) Session(javax.websocket.Session)

Example 82 with Session

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

the class JsrBrowserSocket method onMessage.

@OnMessage
public void onMessage(String message) {
    LOG.info("onTextMessage({})", message);
    int idx = message.indexOf(':');
    if (idx > 0) {
        String key = message.substring(0, idx).toLowerCase(Locale.ENGLISH);
        String val = message.substring(idx + 1);
        switch(key) {
            case "info":
                {
                    writeMessage("Using javax.websocket");
                    if (StringUtil.isBlank(userAgent)) {
                        writeMessage("Client has no User-Agent");
                    } else {
                        writeMessage("Client User-Agent: " + this.userAgent);
                    }
                    if (StringUtil.isBlank(requestedExtensions)) {
                        writeMessage("Client requested no Sec-WebSocket-Extensions");
                    } else {
                        writeMessage("Client Sec-WebSocket-Extensions: " + this.requestedExtensions);
                    }
                    Set<Session> openSessions = session.getOpenSessions();
                    writeMessage("OpenSessions.size() = " + openSessions.size());
                    int i = 0;
                    for (Session open : openSessions) {
                        writeMessage("  OpenSession[%d] = %s", i++, open);
                    }
                    break;
                }
            case "many":
                {
                    String[] parts = StringUtil.csvSplit(val);
                    int size = Integer.parseInt(parts[0]);
                    int count = Integer.parseInt(parts[1]);
                    writeManyAsync(size, count);
                    break;
                }
            case "manythreads":
                {
                    String[] parts = StringUtil.csvSplit(val);
                    int threadCount = Integer.parseInt(parts[0]);
                    int size = Integer.parseInt(parts[1]);
                    int count = Integer.parseInt(parts[2]);
                    Thread[] threads = new Thread[threadCount];
                    // Setup threads
                    for (int n = 0; n < threadCount; n++) {
                        threads[n] = new Thread(new WriteMany(remote, size, count), "WriteMany[" + n + "]");
                    }
                    // Execute threads
                    for (Thread thread : threads) {
                        thread.start();
                    }
                    // Drop out of this thread
                    break;
                }
            case "time":
                {
                    Calendar now = Calendar.getInstance();
                    DateFormat sdf = SimpleDateFormat.getDateTimeInstance(SimpleDateFormat.FULL, SimpleDateFormat.FULL);
                    writeMessage("Server time: %s", sdf.format(now.getTime()));
                    break;
                }
            default:
                {
                    writeMessage("key[%s] val[%s]", key, val);
                }
        }
    } else {
        // Not parameterized, echo it back
        writeMessage(message);
    }
}
Also used : Set(java.util.Set) Calendar(java.util.Calendar) SimpleDateFormat(java.text.SimpleDateFormat) DateFormat(java.text.DateFormat) ServerEndpoint(javax.websocket.server.ServerEndpoint) Session(javax.websocket.Session) OnMessage(javax.websocket.OnMessage)

Example 83 with Session

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

the class SessionTrackingTest method assertServerOpenConnectionCount.

private void assertServerOpenConnectionCount(int expectedCount) {
    Set<Session> sessions = serverContainer.getOpenSessions();
    int openCount = 0;
    for (Session session : sessions) {
        Assert.assertThat("Session.isopen: " + session, session.isOpen(), Matchers.is(true));
        openCount++;
    }
    Assert.assertThat("Open Session Count", openCount, Matchers.is(expectedCount));
}
Also used : ServerEndpoint(javax.websocket.server.ServerEndpoint) Endpoint(javax.websocket.Endpoint) WebSocketSession(org.eclipse.jetty.websocket.common.WebSocketSession) Session(javax.websocket.Session)

Example 84 with Session

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

the class BinaryStreamTest method testEcho.

private void testEcho(int size) throws Exception {
    byte[] data = randomBytes(size);
    URI uri = URI.create("ws://localhost:" + connector.getLocalPort() + PATH);
    ClientBinaryStreamer client = new ClientBinaryStreamer();
    Session session = wsClient.connectToServer(client, uri);
    try (OutputStream output = session.getBasicRemote().getSendStream()) {
        output.write(data);
    }
    Assert.assertTrue(client.await(5, TimeUnit.SECONDS));
    Assert.assertArrayEquals(data, client.getEcho());
}
Also used : OutputStream(java.io.OutputStream) ByteArrayOutputStream(java.io.ByteArrayOutputStream) URI(java.net.URI) Session(javax.websocket.Session)

Example 85 with Session

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

the class EndpointEchoTest method testAbstractEchoInstance.

@Test
public void testAbstractEchoInstance() throws Exception {
    WebSocketContainer container = ContainerProvider.getWebSocketContainer();
    EchoStringEndpoint echoer = new EchoStringEndpoint();
    Assert.assertThat(echoer, instanceOf(javax.websocket.Endpoint.class));
    // Issue connect using instance of class that extends abstract that extends Endpoint
    Session session = container.connectToServer(echoer, serverUri);
    if (LOG.isDebugEnabled())
        LOG.debug("Client Connected: {}", session);
    session.getBasicRemote().sendText("Echo");
    if (LOG.isDebugEnabled())
        LOG.debug("Client Message Sent");
    echoer.messageQueue.awaitMessages(1, 1000, TimeUnit.MILLISECONDS);
}
Also used : WebSocketContainer(javax.websocket.WebSocketContainer) EchoStringEndpoint(org.eclipse.jetty.websocket.jsr356.samples.EchoStringEndpoint) EchoStringEndpoint(org.eclipse.jetty.websocket.jsr356.samples.EchoStringEndpoint) Session(javax.websocket.Session) Test(org.junit.Test)

Aggregations

Session (javax.websocket.Session)115 Test (org.junit.Test)92 URI (java.net.URI)73 WebSocketContainer (javax.websocket.WebSocketContainer)42 Endpoint (javax.websocket.Endpoint)39 CountDownLatch (java.util.concurrent.CountDownLatch)34 Context (org.apache.catalina.Context)31 DefaultServlet (org.apache.catalina.servlets.DefaultServlet)31 Tomcat (org.apache.catalina.startup.Tomcat)31 EndpointConfig (javax.websocket.EndpointConfig)29 ClientEndpointConfig (javax.websocket.ClientEndpointConfig)27 UndertowSession (io.undertow.websockets.jsr.UndertowSession)25 ServerEndpointConfig (javax.websocket.server.ServerEndpointConfig)23 ServerEndpoint (javax.websocket.server.ServerEndpoint)20 ServerWebSocketContainer (io.undertow.websockets.jsr.ServerWebSocketContainer)16 IOException (java.io.IOException)16 AnnotatedClientEndpoint (io.undertow.websockets.jsr.test.annotated.AnnotatedClientEndpoint)15 AtomicBoolean (java.util.concurrent.atomic.AtomicBoolean)15 AtomicReference (java.util.concurrent.atomic.AtomicReference)15 FrameChecker (io.undertow.websockets.utils.FrameChecker)14