Examples of WebSocketClient


Examples of org.java_websocket.client.WebSocketClient

**/
public class FragmentedFramesExample {
  public static void main( String[] args ) throws URISyntaxException , IOException , InterruptedException {
    // WebSocketImpl.DEBUG = true; // will give extra output

    WebSocketClient websocket = new ExampleClient( new URI( "ws://localhost:8887" ), new Draft_17() ); // Draft_17 is implementation of rfc6455
    if( !websocket.connectBlocking() ) {
      System.err.println( "Could not connect to the server." );
      return;
    }

    System.out.println( "This example shows how to send fragmented(continuous) messages." );

    BufferedReader stdin = new BufferedReader( new InputStreamReader( System.in ) );
    while ( websocket.isOpen() ) {
      System.out.println( "Please type in a loooooong line(which then will be send in 2 byte fragments):" );
      String longline = stdin.readLine();
      ByteBuffer longelinebuffer = ByteBuffer.wrap( longline.getBytes() );
      longelinebuffer.rewind();

      for( int position = 2 ; ; position += 2 ) {
        if( position < longelinebuffer.capacity() ) {
          longelinebuffer.limit( position );
          websocket.sendFragmentedFrame( Opcode.TEXT, longelinebuffer, false );// when sending binary data one should use Opcode.BINARY
          assert ( longelinebuffer.remaining() == 0 );
          // after calling sendFragmentedFrame one may reuse the buffer given to the method immediately
        } else {
          longelinebuffer.limit( longelinebuffer.capacity() );
          websocket.sendFragmentedFrame( Opcode.TEXT, longelinebuffer, true );// sending the last frame
          break;
        }

      }
      System.out.println( "You can not type in the next long message or press Ctr-C to exit." );
View Full Code Here

Examples of org.java_websocket.client.WebSocketClient

    } catch ( URISyntaxException e ) {
      e.printStackTrace();
    }
    int totalclients = clients.getValue();
    while ( websockets.size() < totalclients ) {
      WebSocketClient cl = new ExampleClient( uri ) {
        @Override
        public void onClose( int code, String reason, boolean remote ) {
          System.out.println( "Closed duo " + code + " " + reason );
          clients.setValue( websockets.size() );
          websockets.remove( this );
        }
      };

      cl.connect();
      clients.setValue( websockets.size() );
      websockets.add( cl );
      Thread.sleep( joinrate.getValue() );
    }
    while ( websockets.size() > clients.getValue() ) {
View Full Code Here

Examples of org.java_websocket.client.WebSocketClient

      }

    } else if( e.getSource() == connect ) {
      try {
        // cc = new ChatClient(new URI(uriField.getText()), area, ( Draft ) draft.getSelectedItem() );
        cc = new WebSocketClient( new URI( uriField.getText() ), (Draft) draft.getSelectedItem() ) {

          @Override
          public void onMessage( String message ) {
            ta.append( "got: " + message + "\n" );
            ta.setCaretPosition( ta.getDocument().getLength() );
View Full Code Here

Examples of org.xbib.elasticsearch.websocket.client.WebSocketClient

                    .field("channel", channelId)
                    .rawField("message", builder.bytes())
                    .endObject();
            final TextWebSocketFrame frame = new NettyInteractiveResponse("forward", forwardBuilder).response();
            // use a websocket client pool
            WebSocketClient client = nodeChannels.get(websocketNodeAddress);
            if (client == null) {
                final URI uri = new URI("ws:/" + websocketNodeAddress + "/websocket");
                client = clientFactory.newClient(uri, new WebSocketActionListener() {
                    @Override
                    public void onConnect(WebSocketClient client) {
                        nodeChannels.put(websocketNodeAddress, client);
                        client.send(frame);
                    }

                    @Override
                    public void onDisconnect(WebSocketClient client) {
                        logger.warn("node disconnected: {}", uri);
                        nodeChannels.remove(websocketNodeAddress);
                    }

                    @Override
                    public void onMessage(WebSocketClient client, WebSocketFrame frame) {
                        logger.info("unexpected response {}", frame);
                    }

                    @Override
                    public void onError(Throwable t) {
                        logger.error(t.getMessage(), t);
                        nodeChannels.remove(websocketNodeAddress);
                    }
                });
                client.connect();
            } else {
                client.send(frame);
            }
        } catch (Exception e) {
            logger.error(e.getMessage(), e);
        }
    }
View Full Code Here

Examples of org.xbib.elasticsearch.websocket.client.WebSocketClient

            final WebSocketClientRequest publish = clientFactory.newRequest()
                    .type("publish").data(jsonBuilder().startObject()
                    .field("message", "Hello World")
                    .field("topic", topic).endObject());
           
            WebSocketClient client = clientFactory.newClient(getAddressOfNode("1"),
                    new WebSocketActionListener.Adapter() {
                        @Override
                        public void onConnect(WebSocketClient client) throws IOException {
                                logger.info("sending subscribe command, channel = {}", client.channel());
                                subscribe.send(client);
                                logger.info("sending publish command (to ourselves), channel = {}", client.channel());
                                publish.send(client);
                        }

                        @Override
                        public void onMessage(WebSocketClient client, WebSocketFrame frame) {
                            logger.info("frame received: " + frame);
                        }
                    });
            client.connect().await(1000, TimeUnit.MILLISECONDS);           
            Thread.sleep(1000);
            client.send(new CloseWebSocketFrame());
            Thread.sleep(1000);
            client.disconnect();
           
            clientFactory.shutdown();
           
        } catch (Exception e) {
            logger.error(e.getMessage(), e);
View Full Code Here

Examples of org.xbib.elasticsearch.websocket.client.WebSocketClient

                    .type("publish").data(jsonBuilder().startObject()
                    .field("message", "Hi there, I'm another client")
                    .field("topic", topic).endObject());
           
            // open first client
            WebSocketClient subscribingClient = clientFactory.newClient(getAddressOfNode("1"),
                    new WebSocketActionListener.Adapter() {
                        @Override
                        public void onConnect(WebSocketClient client) {
                            try {
                                logger.info("sending subscribe command, channel {}", client.channel());
                                subscribe.send(client);
                            } catch (Exception e) {
                            }
                        }

                        @Override
                        public void onMessage(WebSocketClient client, WebSocketFrame frame) {
                            logger.info("frame {} received for subscribing client {}", frame, client.channel());
                        }

                    });
           
            // open two client
            WebSocketClient publishingClient = clientFactory.newClient(getAddressOfNode("1"),
                    new WebSocketActionListener.Adapter() {
                        @Override
                        public void onConnect(WebSocketClient client) {
                            try {
                                logger.info("sending publish command, channel = {}", client.channel());
                                publish.send(client);
                            } catch (Exception e) {
                            }
                        }

                        @Override
                        public void onMessage(WebSocketClient client, WebSocketFrame frame) {
                            logger.info("frame {} received for publishing client {}", frame, client.channel());
                        }

                    });
           
            // connect both clients to node
            subscribingClient.connect().await(1000, TimeUnit.MILLISECONDS);           
            publishingClient.connect().await(1000, TimeUnit.MILLISECONDS);           
           
            // wait for publish/subscribe
            Thread.sleep(1000);
           
            // close first client
            publishingClient.send(new CloseWebSocketFrame());
            publishingClient.disconnect();

            // close second client
            subscribingClient.send(new CloseWebSocketFrame());
            subscribingClient.disconnect();
           
View Full Code Here

Examples of org.xbib.elasticsearch.websocket.client.WebSocketClient

     */
    @Test
    public void subscribeToOurselves() {
        try {
            WebSocketClientFactory clientFactory = new NettyWebSocketClientFactory();
            WebSocketClient client = clientFactory.newClient(getAddressOfNode("1"),
                    new WebSocketActionListener() {
                        @Override
                        public void onConnect(WebSocketClient client) {
                            try {
                                logger.info("sending subscribe command");
                                client.send(new TextWebSocketFrame("{\"type\":\"subscribe\",\"data\":{\"subscriber\":\"mypubsubdemo\",\"topic\":\"demo\"}}"));
                                Thread.sleep(500);
                                logger.info("sending publish command (to ourselves)");
                                client.send(new TextWebSocketFrame("{\"type\":\"publish\",\"data\":{\"message\":\"Hello World\",\"topic\":\"demo\"}}"));
                            } catch (Exception e) {
                                logger.error(e.getMessage(), e);
                            }
                        }

                        @Override
                        public void onDisconnect(WebSocketClient client) {
                            logger.info("web socket disconnected");
                        }

                        @Override
                        public void onMessage(WebSocketClient client, WebSocketFrame frame) {
                            logger.info("frame received: {}", frame);
                        }

                        @Override
                        public void onError(Throwable t) {
                            logger.error(t.getMessage(), t);
                        }
                    });
            client.connect().await(1000, TimeUnit.MILLISECONDS);           
            Thread.sleep(1000);
            client.send(new CloseWebSocketFrame());
            Thread.sleep(1000);
            client.disconnect();
        } catch (Exception e) {
            logger.error(e.getMessage(), e);
        }
    }
View Full Code Here

Examples of org.xbib.elasticsearch.websocket.client.WebSocketClient

    @Test
    public void testBulk() {
        try {
            final WebSocketClientFactory clientFactory = new NettyWebSocketClientFactory();
            WebSocketClient client = clientFactory.newClient(
                    getAddressOfNode("1"),
                    new WebSocketActionListener.Adapter() {
                        @Override
                        public void onConnect(WebSocketClient client) {
                            try {
                                logger.info("sending some index requests (longer than a single bulk size)");
                                for (int i = 0; i < 250; i++) {
                                    clientFactory.indexRequest()
                                            .data(jsonBuilder()                                           
                                            .startObject()
                                            .field("index", "test")
                                            .field("type", "test")
                                            .field("id", Integer.toString(i))
                                            .startObject("data")
                                            .field("field1", "value" + i)
                                            .field("field2", "value" + i)
                                            .endObject()
                                            .endObject())
                                            .send(client);
                                }
                                // more bulks could be added here ...
                                logger.info("at the end, let us flush the bulk");
                                clientFactory.flushRequest().send(client);
                            } catch (Exception e) {
                                onError(e);
                            }
                        }

                        @Override
                        public void onDisconnect(WebSocketClient client) {
                            logger.info("disconnected");
                        }

                        @Override
                        public void onMessage(WebSocketClient client, WebSocketFrame frame) {
                            logger.info("frame received: {}", frame);
                        }

                        @Override
                        public void onError(Throwable t) {
                            logger.error(t.getMessage(), t);
                        }
                    });
            client.connect().await(1000, TimeUnit.MILLISECONDS);
            Thread.sleep(1000);
            logger.info("closing bulk client");
            client.send(new CloseWebSocketFrame());
            Thread.sleep(1000);
            client.disconnect();
            clientFactory.shutdown();
        } catch (Exception e) {
            logger.error(e.getMessage(), e);
        }
View Full Code Here

Examples of org.xbib.elasticsearch.websocket.client.WebSocketClient

     */
    @Test
    public void helloWorld() {
        try {
            WebSocketClientFactory clientFactory = new NettyWebSocketClientFactory();
            WebSocketClient client = clientFactory.newClient(getAddressOfNode("1"),
                    new WebSocketActionListener() {
                        @Override
                        public void onConnect(WebSocketClient client) {
                            logger.info("web socket connected");
                            String s = "{\"Hello\":\"World\"}";
                            client.send(new TextWebSocketFrame(s));
                            logger.info("sent " + s);
                        }

                        @Override
                        public void onDisconnect(WebSocketClient client) {
                            logger.info("web socket disconnected");
                        }

                        @Override
                        public void onMessage(WebSocketClient client, WebSocketFrame frame) {
                            logger.info("frame received: " + frame);
                        }

                        @Override
                        public void onError(Throwable t) {
                            logger.error(t.getMessage(), t);
                        }
                    });
            client.connect().await(1000, TimeUnit.MILLISECONDS);
            Thread.sleep(1000);
            client.send(new CloseWebSocketFrame());
            Thread.sleep(1000);
            client.disconnect();
        } catch (Exception e) {
            logger.error(e.getMessage(), e);
        }
    }
View Full Code Here

Examples of se.cgbystrom.netty.http.websocket.WebSocketClient

        startServer(new WebSocketServerHandler());

        WebSocketClientFactory clientFactory = new WebSocketClientFactory();
        final TestClient callback = new TestClient();

        WebSocketClient client = clientFactory.newClient(new URI("ws://localhost:" + port + "/websocket"), callback);

        client.connect().awaitUninterruptibly();
        Thread.sleep(1000);

        assertTrue(callback.connected);
        assertEquals(TestClient.TEST_MESSAGE.toUpperCase(), callback.messageReceived);
        client.disconnect();
        Thread.sleep(1000);

        assertFalse(callback.connected);
    }
View Full Code Here
TOP
Copyright © 2018 www.massapi.com. All rights reserved.
All source code are property of their respective owners. Java is a trademark of Sun Microsystems, Inc and owned by ORACLE Inc. Contact coftware#gmail.com.