Search in sources :

Example 1 with Session

use of org.eclipse.jetty.websocket.api.Session in project zeppelin by apache.

the class ZeppelinClient method removeAllConnections.

private void removeAllConnections() {
    if (watcherSession != null && watcherSession.isOpen()) {
        watcherSession.close();
    }
    Session noteSession = null;
    for (Map.Entry<String, Session> note : notesConnection.entrySet()) {
        noteSession = note.getValue();
        if (isSessionOpen(noteSession)) {
            noteSession.close();
        }
    }
    notesConnection.clear();
}
Also used : HashMap(java.util.HashMap) Map(java.util.Map) ConcurrentHashMap(java.util.concurrent.ConcurrentHashMap) Session(org.eclipse.jetty.websocket.api.Session)

Example 2 with Session

use of org.eclipse.jetty.websocket.api.Session in project zeppelin by apache.

the class ZeppelinClient method send.

public void send(Message msg, String noteId) {
    Session noteSession = getZeppelinConnection(noteId);
    if (!isSessionOpen(noteSession)) {
        LOG.error("Cannot open websocket connection to Zeppelin note {}", noteId);
        return;
    }
    noteSession.getRemote().sendStringByFuture(serialize(msg));
}
Also used : Session(org.eclipse.jetty.websocket.api.Session)

Example 3 with Session

use of org.eclipse.jetty.websocket.api.Session in project zeppelin by apache.

the class ZeppelinhubClient method connect.

private ZeppelinhubSession connect() {
    ZeppelinhubSession zeppelinSession;
    try {
        ZeppelinhubWebsocket ws = ZeppelinhubWebsocket.newInstance(zeppelinhubToken);
        Future<Session> future = client.connect(ws, zeppelinhubWebsocketUrl, conectionRequest);
        Session session = future.get();
        zeppelinSession = ZeppelinhubSession.createInstance(session, zeppelinhubToken);
    } catch (IOException | InterruptedException | ExecutionException e) {
        LOG.info("Couldnt connect to zeppelinhub - {}", e.toString());
        zeppelinSession = ZeppelinhubSession.EMPTY;
    }
    return zeppelinSession;
}
Also used : ZeppelinhubSession(org.apache.zeppelin.notebook.repo.zeppelinhub.websocket.session.ZeppelinhubSession) IOException(java.io.IOException) ZeppelinhubWebsocket(org.apache.zeppelin.notebook.repo.zeppelinhub.websocket.listener.ZeppelinhubWebsocket) ExecutionException(java.util.concurrent.ExecutionException) Session(org.eclipse.jetty.websocket.api.Session) ZeppelinhubSession(org.apache.zeppelin.notebook.repo.zeppelinhub.websocket.session.ZeppelinhubSession)

Example 4 with Session

use of org.eclipse.jetty.websocket.api.Session in project zeppelin by apache.

the class ZeppelinClientTest method zeppelinConnectionTest.

@Test
public void zeppelinConnectionTest() {
    try {
        // Wait for websocket server to start
        Thread.sleep(2000);
    } catch (InterruptedException e) {
        LOG.warn("Cannot wait for websocket server to start, returning");
        return;
    }
    // Initialize and start Zeppelin client
    ZeppelinClient client = ZeppelinClient.initialize(validWebsocketUrl, "dummy token", null);
    client.start();
    LOG.info("Zeppelin websocket client started");
    // Connection to note AAAA
    Session connectionA = client.getZeppelinConnection("AAAA");
    assertNotNull(connectionA);
    assertTrue(connectionA.isOpen());
    assertEquals(client.countConnectedNotes(), 1);
    assertEquals(connectionA, client.getZeppelinConnection("AAAA"));
    // Connection to note BBBB
    Session connectionB = client.getZeppelinConnection("BBBB");
    assertNotNull(connectionB);
    assertTrue(connectionB.isOpen());
    assertEquals(client.countConnectedNotes(), 2);
    assertEquals(connectionB, client.getZeppelinConnection("BBBB"));
    // Remove connection to note AAAA
    client.removeNoteConnection("AAAA");
    assertEquals(client.countConnectedNotes(), 1);
    assertNotEquals(connectionA, client.getZeppelinConnection("AAAA"));
    assertEquals(client.countConnectedNotes(), 2);
    client.stop();
}
Also used : Session(org.eclipse.jetty.websocket.api.Session) Test(org.junit.Test)

Example 5 with Session

use of org.eclipse.jetty.websocket.api.Session in project jetty.project by eclipse.

the class LargeContainerTest method testEcho.

@Test
public void testEcho() throws Exception {
    WSServer wsb = new WSServer(testdir, "app");
    wsb.copyWebInf("large-echo-config-web.xml");
    wsb.copyEndpoint(LargeEchoDefaultSocket.class);
    try {
        wsb.start();
        URI uri = wsb.getServerBaseURI();
        WebAppContext webapp = wsb.createWebAppContext();
        wsb.deployWebapp(webapp);
        // wsb.dump();
        WebSocketClient client = new WebSocketClient(bufferPool);
        try {
            client.getPolicy().setMaxTextMessageSize(128 * 1024);
            client.start();
            JettyEchoSocket clientEcho = new JettyEchoSocket();
            Future<Session> foo = client.connect(clientEcho, uri.resolve("echo/large"));
            // wait for connect
            foo.get(1, TimeUnit.SECONDS);
            // The message size should be bigger than default, but smaller than the limit that LargeEchoSocket specifies
            byte[] txt = new byte[100 * 1024];
            Arrays.fill(txt, (byte) 'o');
            String msg = new String(txt, StandardCharsets.UTF_8);
            clientEcho.sendMessage(msg);
            Queue<String> msgs = clientEcho.awaitMessages(1);
            Assert.assertEquals("Expected message", msg, msgs.poll());
        } finally {
            client.stop();
        }
    } finally {
        wsb.stop();
    }
}
Also used : WebAppContext(org.eclipse.jetty.webapp.WebAppContext) WebSocketClient(org.eclipse.jetty.websocket.client.WebSocketClient) URI(java.net.URI) Session(org.eclipse.jetty.websocket.api.Session) Test(org.junit.Test)

Aggregations

Session (org.eclipse.jetty.websocket.api.Session)74 Test (org.junit.Test)57 URI (java.net.URI)46 IBlockheadServerConnection (org.eclipse.jetty.websocket.common.test.IBlockheadServerConnection)32 WebSocketClient (org.eclipse.jetty.websocket.client.WebSocketClient)23 WebSocketSession (org.eclipse.jetty.websocket.common.WebSocketSession)21 ExecutionException (java.util.concurrent.ExecutionException)12 ClientUpgradeRequest (org.eclipse.jetty.websocket.client.ClientUpgradeRequest)9 HashMap (java.util.HashMap)8 Matchers.containsString (org.hamcrest.Matchers.containsString)8 Envelope (com.kixeye.chassis.transport.dto.Envelope)7 MessageSerDe (com.kixeye.chassis.transport.serde.MessageSerDe)7 ProtobufMessageSerDe (com.kixeye.chassis.transport.serde.converter.ProtobufMessageSerDe)7 QueuingWebSocketListener (com.kixeye.chassis.transport.websocket.QueuingWebSocketListener)7 WebSocketMessageRegistry (com.kixeye.chassis.transport.websocket.WebSocketMessageRegistry)7 EndPoint (org.eclipse.jetty.io.EndPoint)7 SocketChannelEndPoint (org.eclipse.jetty.io.SocketChannelEndPoint)7 RemoteEndpoint (org.eclipse.jetty.websocket.api.RemoteEndpoint)7 UpgradeException (org.eclipse.jetty.websocket.api.UpgradeException)7 MapPropertySource (org.springframework.core.env.MapPropertySource)7