Uploaded image for project: 'JDK'
  1. JDK
  2. JDK-8178522

WebSockets just receive onOpen and nothing more

XMLWordPrintable

    • generic
    • generic

      FULL PRODUCT VERSION :
      JDK 9-ea Build 164

      ADDITIONAL OS VERSION INFORMATION :
      Mac OS 10.11.7

      A DESCRIPTION OF THE PROBLEM :
      I just played around with the new HTTP/2 and WebSocket-API. Visting the page

      https://www.websocket.org/echo.html

      and wanted to convert the java script / html demo into java code.

      STEPS TO FOLLOW TO REPRODUCE THE PROBLEM :
      Just start the programm

      EXPECTED VERSUS ACTUAL BEHAVIOR :
      EXPECTED -
      A sequence of

      onOpen

      onText

      onClose
      ACTUAL -
      Just

      onOpen

      REPRODUCIBILITY :
      This bug can be reproduced always.

      ---------- BEGIN SOURCE ----------
      import java.net.URI;
      import java.nio.ByteBuffer;
      import java.time.Duration;
      import java.util.concurrent.CompletableFuture;
      import java.util.concurrent.CompletionStage;

      import jdk.incubator.http.HttpClient;
      import jdk.incubator.http.WebSocket;
      import jdk.incubator.http.WebSocket.MessagePart;

      public class WebSocketExample
      {
          public static void main(final String[] args) throws Exception
          {
              final URI uri = URI.create("ws://echo.websocket.org/");

              final WebSocket.Listener webSocketListener = new WebSocket.Listener()
              {
                  @Override
                  public void onOpen(WebSocket webSocket)
                  {
                      writeToScreen("onOpen");
                      webSocket.request(1);
                      System.out.println(webSocket);
                      
                      doSend(webSocket, "WebSocket rocks");
                      System.out.println(webSocket);
                  }

                  @Override
                  public CompletionStage<?> onClose(WebSocket webSocket, int statusCode, String reason)
                  {
                      writeToScreen("onClose");
                      return null;
                  }

                  @Override
                  public CompletionStage<?> onText(WebSocket webSocket, CharSequence message, MessagePart part)
                  {
                      writeToScreen(message);
                      return webSocket.sendClose();
                  }

                  @Override
                  public void onError(WebSocket webSocket, Throwable error) {
                      writeToScreen("ERROR: " + error.getMessage());
                  }
                  
                  CompletionStage<?> doSend(WebSocket webSocket, CharSequence message)
                  {
                      writeToScreen("SENT: " + message);
                      webSocket.sendText(message);
                      return null;
                  }

                  void writeToScreen(CharSequence message)
                  {
                      System.out.println(message);
                  }
              };

              final HttpClient client = HttpClient.newHttpClient();
              final WebSocket.Builder builder = client.newWebSocketBuilder(uri, webSocketListener);

              final CompletableFuture<WebSocket> websocketFuture = builder.connectTimeout(Duration.ofSeconds(5)).buildAsync();
              System.out.println("websocketFuture: " + websocketFuture);
              
              Thread.sleep(10000);
      /*
              websocketFuture.exceptionally(e -> {
                  System.out.println(e.getClass());
                  return null;
              });
              
              Thread.sleep(2000);
              
              websocketFuture.thenAccept(action -> System.out.println("WS2: " + action));
              
              final WebSocket webSocket = websocketFuture.get();
              System.out.println("webSocket: " + webSocket);
              System.out.println("Connected");
              
              System.out.println("Sending Text");
              final CompletableFuture<WebSocket> asyncResponse1 = webSocket.sendText("Hello");

              System.out.println("Sending Ping");
              final ByteBuffer message = ByteBuffer.wrap("message".getBytes());
              final CompletableFuture<WebSocket> asyncResponse2 = webSocket.sendPing(message);

              System.out.println("Sending Binary");
              final CompletableFuture<WebSocket> asyncResponse3 = webSocket.sendBinary(message, true);

              System.out.println("Sending Close");
              final CompletableFuture<WebSocket> asyncResponseLast = webSocket.sendClose();
              
              Thread.sleep(5000);
              */
          }
      }
      ---------- END SOURCE ----------

      SUPPORT :
      YES

            prappo Pavel Rappo
            webbuggrp Webbug Group
            Votes:
            0 Vote for this issue
            Watchers:
            6 Start watching this issue

              Created:
              Updated:
              Resolved: