Search in sources :

Example 91 with Session

use of javax.websocket.Session in project javaee7-samples by javaee-samples.

the class MyEndpointTest method testEndpointByteArray.

/**
     * The basic test method for the class {
     *
     * @MyEndpointByteArray }
     *
     * @throws DeploymentException
     * @throws IOException
     * @throws URISyntaxException
     */
@Test
public void testEndpointByteArray() throws DeploymentException, IOException, URISyntaxException, InterruptedException {
    MyEndpointClient.latch = new CountDownLatch(1);
    Session session = connectToServer("bytearray");
    assertNotNull(session);
    assertTrue(MyEndpointClient.latch.await(2, TimeUnit.SECONDS));
    assertNotNull(MyEndpointClient.response);
    assertArrayEquals(RESPONSE.getBytes(), MyEndpointClient.response);
}
Also used : CountDownLatch(java.util.concurrent.CountDownLatch) Session(javax.websocket.Session) Test(org.junit.Test)

Example 92 with Session

use of javax.websocket.Session in project javaee7-samples by javaee-samples.

the class MyClientTest method testEndpoint.

@Test
public void testEndpoint() throws URISyntaxException, DeploymentException, IOException, InterruptedException {
    String JSON = "{\"apple\":\"red\",\"banana\":\"yellow\"}";
    Session session = connectToServer(MyClient.class);
    assertNotNull(session);
    assertTrue(MyClient.latch.await(2, TimeUnit.SECONDS));
    assertEquals(JSON, MyClient.response.toString());
}
Also used : Session(javax.websocket.Session) Test(org.junit.Test)

Example 93 with Session

use of javax.websocket.Session in project javaee7-samples by javaee-samples.

the class EncoderEndpointTest method testEndpointEmptyJSONObject.

@Test
public void testEndpointEmptyJSONObject() throws URISyntaxException, DeploymentException, IOException, InterruptedException {
    String JSON = "{\"apple\":\"red\",\"banana\":\"yellow\"}";
    Session session = connectToServer(MyEndpointClientJSONObject.class);
    assertNotNull(session);
    assertTrue(MyEndpointClientJSONObject.latch.await(2, TimeUnit.SECONDS));
    assertEquals(JSON, MyEndpointClientJSONObject.response);
}
Also used : Session(javax.websocket.Session) Test(org.junit.Test)

Example 94 with Session

use of javax.websocket.Session in project javaee7-samples by javaee-samples.

the class EncoderEndpointTest method testEndpointEmptyJSONArray.

@Test
public void testEndpointEmptyJSONArray() throws URISyntaxException, DeploymentException, IOException, InterruptedException {
    final Session session = connectToServer(MyEndpointClientEmptyJSONArray.class);
    assertNotNull(session);
    assertTrue(MyEndpointClientEmptyJSONArray.latch.await(2, TimeUnit.SECONDS));
    assertEquals("{}", MyEndpointClientEmptyJSONArray.response);
}
Also used : Session(javax.websocket.Session) Test(org.junit.Test)

Example 95 with Session

use of javax.websocket.Session in project pinot by linkedin.

the class MeetupRsvpStream method run.

public void run() {
    try {
        final ClientEndpointConfig cec = ClientEndpointConfig.Builder.create().build();
        final KafkaJSONMessageDecoder decoder = new KafkaJSONMessageDecoder();
        decoder.init(null, schema, null);
        client = ClientManager.createClient();
        client.connectToServer(new Endpoint() {

            @Override
            public void onOpen(Session session, EndpointConfig config) {
                try {
                    session.addMessageHandler(new MessageHandler.Whole<String>() {

                        @Override
                        public void onMessage(String message) {
                            try {
                                JSONObject messageJSON = new JSONObject(message);
                                JSONObject extracted = new JSONObject();
                                if (messageJSON.has("venue")) {
                                    JSONObject venue = messageJSON.getJSONObject("venue");
                                    extracted.put("venue_name", venue.getString("venue_name"));
                                }
                                if (messageJSON.has("event")) {
                                    JSONObject event = messageJSON.getJSONObject("event");
                                    extracted.put("event_name", event.getString("event_name"));
                                    extracted.put("event_id", event.getString("event_id"));
                                    extracted.put("event_time", event.getLong("time"));
                                }
                                if (messageJSON.has("group")) {
                                    JSONObject group = messageJSON.getJSONObject("group");
                                    extracted.put("group_city", group.getString("group_city"));
                                    extracted.put("group_country", group.getString("group_country"));
                                    extracted.put("group_id", group.getLong("group_id"));
                                    extracted.put("group_name", group.getString("group_name"));
                                }
                                extracted.put("mtime", messageJSON.getLong("mtime"));
                                extracted.put("rsvp_count", 1);
                                if (keepPublishing) {
                                    KeyedMessage<String, byte[]> data = new KeyedMessage<String, byte[]>("meetupRSVPEvents", extracted.toString().getBytes("UTF-8"));
                                    producer.send(data);
                                }
                            } catch (Exception e) {
                            //LOGGER.error("error processing raw event ", e);
                            }
                        }
                    });
                    session.getBasicRemote().sendText("");
                } catch (IOException e) {
                //LOGGER.error("found an event where data did not have all the fields, don't care about for quickstart");
                }
            }
        }, cec, new URI("ws://stream.meetup.com/2/rsvps"));
    } catch (Exception e) {
    //e.printStackTrace();
    }
}
Also used : KafkaJSONMessageDecoder(com.linkedin.pinot.core.realtime.impl.kafka.KafkaJSONMessageDecoder) IOException(java.io.IOException) URI(java.net.URI) URISyntaxException(java.net.URISyntaxException) IOException(java.io.IOException) Endpoint(javax.websocket.Endpoint) JSONObject(org.json.JSONObject) ClientEndpointConfig(javax.websocket.ClientEndpointConfig) KeyedMessage(kafka.producer.KeyedMessage) EndpointConfig(javax.websocket.EndpointConfig) ClientEndpointConfig(javax.websocket.ClientEndpointConfig) Session(javax.websocket.Session)

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