jdk/src/java.httpclient/share/classes/java/net/http/WebSocket.java
changeset 38864 bf2b41533aed
parent 38856 cc3a0d1e96e0
child 39133 b5641ce64cf7
equal deleted inserted replaced
38863:e031aa31b25f 38864:bf2b41533aed
    50  * String) received}. Or to close abruptly, {@link #abort()} is called. Once
    50  * String) received}. Or to close abruptly, {@link #abort()} is called. Once
    51  * closed it remains closed, cannot be reopened.
    51  * closed it remains closed, cannot be reopened.
    52  *
    52  *
    53  * <p> Messages of type {@code X} are sent through the {@code WebSocket.sendX}
    53  * <p> Messages of type {@code X} are sent through the {@code WebSocket.sendX}
    54  * methods and received through {@link WebSocket.Listener}{@code .onX} methods
    54  * methods and received through {@link WebSocket.Listener}{@code .onX} methods
    55  * asynchronously. Each of the methods begins the operation and returns a {@link
    55  * asynchronously. Each of the methods returns a {@link CompletionStage} which
    56  * CompletionStage} which completes when the operation has completed.
    56  * completes when the operation has completed.
    57  *
    57  *
    58  * <p> Messages are received only if {@linkplain #request(long) requested}.
    58  * <p> Messages are received only if {@linkplain #request(long) requested}.
    59  *
    59  *
    60  * <p> One outstanding send operation is permitted: if another send operation is
    60  * <p> One outstanding send operation is permitted: if another send operation is
    61  * initiated before the previous one has completed, an {@link
    61  * initiated before the previous one has completed, an {@link
    76  * HttpClient}.
    76  * HttpClient}.
    77  *
    77  *
    78  * <p> Unless otherwise noted, passing a {@code null} argument to a constructor
    78  * <p> Unless otherwise noted, passing a {@code null} argument to a constructor
    79  * or method of this type will cause a {@link NullPointerException
    79  * or method of this type will cause a {@link NullPointerException
    80  * NullPointerException} to be thrown.
    80  * NullPointerException} to be thrown.
       
    81  *
       
    82  * @implNote The default implementation's methods do not block before returning
       
    83  * a {@code CompletableFuture}.
    81  *
    84  *
    82  * @since 9
    85  * @since 9
    83  */
    86  */
    84 public interface WebSocket {
    87 public interface WebSocket {
    85 
    88 
   232         Builder connectTimeout(long timeout, TimeUnit unit);
   235         Builder connectTimeout(long timeout, TimeUnit unit);
   233 
   236 
   234         /**
   237         /**
   235          * Builds a {@code WebSocket}.
   238          * Builds a {@code WebSocket}.
   236          *
   239          *
   237          * <p> Returns immediately with a {@code CompletableFuture<WebSocket>}
   240          * <p> Returns a {@code CompletableFuture<WebSocket>} which completes
   238          * which completes with the {@code WebSocket} when it is connected, or
   241          * normally with the {@code WebSocket} when it is connected or completes
   239          * completes exceptionally if an error occurs.
   242          * exceptionally if an error occurs.
   240          *
   243          *
   241          * <p> {@code CompletableFuture} may complete exceptionally with the
   244          * <p> {@code CompletableFuture} may complete exceptionally with the
   242          * following errors:
   245          * following errors:
   243          * <ul>
   246          * <ul>
   244          * <li> {@link IOException}
   247          * <li> {@link IOException}
   250          *          have a {@link java.net.URLPermission} for the WebSocket URI
   253          *          have a {@link java.net.URLPermission} for the WebSocket URI
   251          * <li> {@link WebSocketHandshakeException}
   254          * <li> {@link WebSocketHandshakeException}
   252          *          if the opening handshake fails
   255          *          if the opening handshake fails
   253          * </ul>
   256          * </ul>
   254          *
   257          *
   255          * @return a {@code CompletableFuture} of {@code WebSocket}
   258          * @return a {@code CompletableFuture} with the {@code WebSocket}
   256          */
   259          */
   257         CompletableFuture<WebSocket> buildAsync();
   260         CompletableFuture<WebSocket> buildAsync();
   258     }
   261     }
   259 
   262 
   260     /**
   263     /**
   599     }
   602     }
   600 
   603 
   601     /**
   604     /**
   602      * Sends a Text message with characters from the given {@code CharSequence}.
   605      * Sends a Text message with characters from the given {@code CharSequence}.
   603      *
   606      *
   604      * <p> Returns immediately with a {@code CompletableFuture<Void>} which
   607      * <p> Returns a {@code CompletableFuture<WebSocket>} which completes
   605      * completes normally when the message has been sent, or completes
   608      * normally when the message has been sent or completes exceptionally if an
   606      * exceptionally if an error occurs.
   609      * error occurs.
   607      *
   610      *
   608      * <p> The {@code CharSequence} should not be modified until the returned
   611      * <p> The {@code CharSequence} should not be modified until the returned
   609      * {@code CompletableFuture} completes (either normally or exceptionally).
   612      * {@code CompletableFuture} completes (either normally or exceptionally).
   610      *
   613      *
   611      * <p> The returned {@code CompletableFuture} can complete exceptionally
   614      * <p> The returned {@code CompletableFuture} can complete exceptionally
   612      * with:
   615      * with:
   613      * <ul>
   616      * <ul>
   614      * <li> {@link IOException}
   617      * <li> {@link IOException}
   615      *          if an I/O error occurs during this operation; or the
   618      *          if an I/O error occurs during this operation
   616      *          {@code WebSocket} closes while this operation is in progress;
   619      * <li> {@link IllegalStateException}
   617      *          or the {@code message} is a malformed UTF-16 sequence
   620      *          if the {@code WebSocket} closes while this operation is in progress;
       
   621      *          or if a Close message has been sent already;
       
   622      *          or if there is an outstanding send operation;
       
   623      *          or if a previous Binary message was not sent with {@code isLast == true}
   618      * </ul>
   624      * </ul>
   619      *
   625      *
   620      * @implNote This implementation does not accept partial UTF-16
   626      * @implNote This implementation does not accept partial UTF-16
   621      * sequences. In case such a sequence is passed, a returned {@code
   627      * sequences. In case such a sequence is passed, a returned {@code
   622      * CompletableFuture} completes exceptionally.
   628      * CompletableFuture} completes exceptionally.
   623      *
       
   624      * @param message
       
   625      *         the message
       
   626      * @param isLast
       
   627      *         {@code true} if this is the final part of the message
       
   628      *         {@code false} otherwise
       
   629      *
       
   630      * @return a CompletableFuture of Void
       
   631      *
       
   632      * @throws IllegalStateException
       
   633      *         if the WebSocket is closed
       
   634      * @throws IllegalStateException
       
   635      *         if a Close message has been already sent
       
   636      * @throws IllegalStateException
       
   637      *         if there is an outstanding send operation
       
   638      * @throws IllegalStateException
       
   639      *         if a previous Binary message was not sent
       
   640      *         with {@code isLast == true}
       
   641      */
       
   642     CompletableFuture<Void> sendText(CharSequence message, boolean isLast);
       
   643 
       
   644     /**
       
   645      * Sends a whole Text message with characters from the given {@code
       
   646      * CharSequence}.
       
   647      *
       
   648      * <p> This is a convenience method. For the general case, use {@link
       
   649      * #sendText(CharSequence, boolean)}.
       
   650      *
       
   651      * <p> Returns immediately with a {@code CompletableFuture<Void>} which
       
   652      * completes normally when the message has been sent, or completes
       
   653      * exceptionally if an error occurs.
       
   654      *
       
   655      * <p> The {@code CharSequence} should not be modified until the returned
       
   656      * {@code CompletableFuture} completes (either normally or exceptionally).
       
   657      *
       
   658      * <p> The returned {@code CompletableFuture} can complete exceptionally
       
   659      * with:
       
   660      * <ul>
       
   661      * <li> {@link IOException}
       
   662      *          if an I/O error occurs during this operation;
       
   663      *          or the {@code WebSocket} closes while this operation is in progress;
       
   664      *          or the message is a malformed (or an incomplete) UTF-16 sequence
       
   665      * </ul>
       
   666      *
       
   667      * @param message
       
   668      *         the message
       
   669      *
       
   670      * @return a CompletableFuture of Void
       
   671      *
       
   672      * @throws IllegalStateException
       
   673      *         if the WebSocket is closed
       
   674      * @throws IllegalStateException
       
   675      *         if a Close message has been already sent
       
   676      * @throws IllegalStateException
       
   677      *         if there is an outstanding send operation
       
   678      * @throws IllegalStateException
       
   679      *         if a previous Binary message was not sent
       
   680      *         with {@code isLast == true}
       
   681      */
       
   682     default CompletableFuture<Void> sendText(CharSequence message) {
       
   683         return sendText(message, true);
       
   684     }
       
   685 
       
   686     /**
       
   687      * Sends a whole Text message with characters from {@code
       
   688      * CharacterSequence}s provided by the given {@code Stream}.
       
   689      *
       
   690      * <p> This is a convenience method. For the general case use {@link
       
   691      * #sendText(CharSequence, boolean)}.
       
   692      *
       
   693      * <p> Returns immediately with a {@code CompletableFuture<Void>} which
       
   694      * completes normally when the message has been sent, or completes
       
   695      * exceptionally if an error occurs.
       
   696      *
       
   697      * <p> Streamed character sequences should not be modified until the
       
   698      * returned {@code CompletableFuture} completes (either normally or
       
   699      * exceptionally).
       
   700      *
       
   701      * <p> The returned {@code CompletableFuture} can complete exceptionally
       
   702      * with:
       
   703      * <ul>
       
   704      * <li> {@link IOException}
       
   705      *          if an I/O error occurs during this operation;
       
   706      *          or the {@code WebSocket} closes while this operation is in progress;
       
   707      *          or the message is a malformed (or an incomplete) UTF-16 sequence
       
   708      * </ul>
       
   709      *
       
   710      * @param message
       
   711      *         the message
       
   712      *
       
   713      * @return a CompletableFuture of Void
       
   714      *
       
   715      * @throws IllegalStateException
       
   716      *         if the WebSocket is closed
       
   717      * @throws IllegalStateException
       
   718      *         if a Close message has been already sent
       
   719      * @throws IllegalStateException
       
   720      *         if there is an outstanding send operation
       
   721      * @throws IllegalStateException
       
   722      *         if a previous Binary message was not sent
       
   723      *         with {@code isLast == true}
       
   724      */
       
   725     CompletableFuture<Void> sendText(Stream<? extends CharSequence> message);
       
   726 
       
   727     /**
       
   728      * Sends a Binary message with bytes from the given {@code ByteBuffer}.
       
   729      *
       
   730      * <p> Returns immediately with a {@code CompletableFuture<Void>} which
       
   731      * completes normally when the message has been sent, or completes
       
   732      * exceptionally if an error occurs.
       
   733      *
       
   734      * <p> The returned {@code CompletableFuture} can complete exceptionally
       
   735      * with:
       
   736      * <ul>
       
   737      * <li> {@link IOException}
       
   738      *          if an I/O error occurs during this operation or the
       
   739      *          {@code WebSocket} closes while this operation is in progress
       
   740      * </ul>
       
   741      *
   629      *
   742      * @param message
   630      * @param message
   743      *         the message
   631      *         the message
   744      * @param isLast
   632      * @param isLast
   745      *         {@code true} if this is the final part of the message,
   633      *         {@code true} if this is the final part of the message,
   746      *         {@code false} otherwise
   634      *         {@code false} otherwise
   747      *
   635      *
   748      * @return a CompletableFuture of Void
   636      * @return a CompletableFuture with this WebSocket
   749      *
   637      *
   750      * @throws IllegalStateException
   638      * @throws IllegalArgumentException
   751      *         if the WebSocket is closed
   639      *         if {@code message} is a malformed (or an incomplete) UTF-16 sequence
   752      * @throws IllegalStateException
   640      */
   753      *         if a Close message has been already sent
   641     CompletableFuture<WebSocket> sendText(CharSequence message, boolean isLast);
   754      * @throws IllegalStateException
   642 
   755      *         if there is an outstanding send operation
   643     /**
   756      * @throws IllegalStateException
   644      * Sends a whole Text message with characters from the given {@code
   757      *         if a previous Text message was not sent
   645      * CharSequence}.
   758      *         with {@code isLast == true}
   646      *
   759      */
   647      * <p> This is a convenience method. For the general case, use {@link
   760     CompletableFuture<Void> sendBinary(ByteBuffer message, boolean isLast);
   648      * #sendText(CharSequence, boolean)}.
   761 
   649      *
   762     /**
   650      * <p> Returns a {@code CompletableFuture<WebSocket>} which completes
   763      * Sends a Binary message with bytes from the given {@code byte[]}.
   651      * normally when the message has been sent or completes exceptionally if an
   764      *
   652      * error occurs.
   765      * <p> Returns immediately with a {@code CompletableFuture<Void>} which
   653      *
   766      * completes normally when the message has been sent, or completes
   654      * <p> The {@code CharSequence} should not be modified until the returned
   767      * exceptionally if an error occurs.
   655      * {@code CompletableFuture} completes (either normally or exceptionally).
   768      *
   656      *
   769      * <p> The returned {@code CompletableFuture} can complete exceptionally
   657      * <p> The returned {@code CompletableFuture} can complete exceptionally
   770      * with:
   658      * with:
   771      * <ul>
   659      * <ul>
   772      * <li> {@link IOException}
   660      * <li> {@link IOException}
   773      *          if an I/O error occurs during this operation or the
   661      *          if an I/O error occurs during this operation
   774      *          {@code WebSocket} closes while this operation is in progress
   662      * <li> {@link IllegalStateException}
       
   663      *          if the {@code WebSocket} closes while this operation is in progress;
       
   664      *          or if a Close message has been sent already;
       
   665      *          or if there is an outstanding send operation;
       
   666      *          or if a previous Binary message was not sent with {@code isLast == true}
   775      * </ul>
   667      * </ul>
   776      *
   668      *
   777      * @implSpec This is equivalent to:
   669      * @param message
   778      * <pre>{@code
   670      *         the message
   779      *     sendBinary(ByteBuffer.wrap(message), isLast)
   671      *
   780      * }</pre>
   672      * @return a CompletableFuture with this WebSocket
       
   673      *
       
   674      * @throws IllegalArgumentException
       
   675      *         if {@code message} is a malformed (or an incomplete) UTF-16 sequence
       
   676      */
       
   677     default CompletableFuture<WebSocket> sendText(CharSequence message) {
       
   678         return sendText(message, true);
       
   679     }
       
   680 
       
   681     /**
       
   682      * Sends a whole Text message with characters from {@code
       
   683      * CharacterSequence}s provided by the given {@code Stream}.
       
   684      *
       
   685      * <p> This is a convenience method. For the general case use {@link
       
   686      * #sendText(CharSequence, boolean)}.
       
   687      *
       
   688      * <p> Returns a {@code CompletableFuture<WebSocket>} which completes
       
   689      * normally when the message has been sent or completes exceptionally if an
       
   690      * error occurs.
       
   691      *
       
   692      * <p> Streamed character sequences should not be modified until the
       
   693      * returned {@code CompletableFuture} completes (either normally or
       
   694      * exceptionally).
       
   695      *
       
   696      * <p> The returned {@code CompletableFuture} can complete exceptionally
       
   697      * with:
       
   698      * <ul>
       
   699      * <li> {@link IOException}
       
   700      *          if an I/O error occurs during this operation
       
   701      * <li> {@link IllegalStateException}
       
   702      *          if the {@code WebSocket} closes while this operation is in progress;
       
   703      *          or if a Close message has been sent already;
       
   704      *          or if there is an outstanding send operation;
       
   705      *          or if a previous Binary message was not sent with {@code isLast == true}
       
   706      * </ul>
       
   707      *
       
   708      * @param message
       
   709      *         the message
       
   710      *
       
   711      * @return a CompletableFuture with this WebSocket
       
   712      *
       
   713      * @throws IllegalArgumentException
       
   714      *         if {@code message} is a malformed (or an incomplete) UTF-16 sequence
       
   715      */
       
   716     CompletableFuture<WebSocket> sendText(Stream<? extends CharSequence> message);
       
   717 
       
   718     /**
       
   719      * Sends a Binary message with bytes from the given {@code ByteBuffer}.
       
   720      *
       
   721      * <p> Returns a {@code CompletableFuture<WebSocket>} which completes
       
   722      * normally when the message has been sent or completes exceptionally if an
       
   723      * error occurs.
       
   724      *
       
   725      * <p> The returned {@code CompletableFuture} can complete exceptionally
       
   726      * with:
       
   727      * <ul>
       
   728      * <li> {@link IOException}
       
   729      *          if an I/O error occurs during this operation
       
   730      * <li> {@link IllegalStateException}
       
   731      *          if the {@code WebSocket} closes while this operation is in progress;
       
   732      *          or if a Close message has been sent already;
       
   733      *          or if there is an outstanding send operation;
       
   734      *          or if a previous Text message was not sent with {@code isLast == true}
       
   735      * </ul>
   781      *
   736      *
   782      * @param message
   737      * @param message
   783      *         the message
   738      *         the message
   784      * @param isLast
   739      * @param isLast
   785      *         {@code true} if this is the final part of the message,
   740      *         {@code true} if this is the final part of the message,
   786      *         {@code false} otherwise
   741      *         {@code false} otherwise
   787      *
   742      *
   788      * @return a CompletableFuture of Void
   743      * @return a CompletableFuture with this WebSocket
   789      *
   744      */
   790      * @throws IllegalStateException
   745     CompletableFuture<WebSocket> sendBinary(ByteBuffer message, boolean isLast);
   791      *         if the WebSocket is closed
   746 
   792      * @throws IllegalStateException
   747     /**
   793      *         if a Close message has been already sent
   748      * Sends a Binary message with bytes from the given {@code byte[]}.
   794      * @throws IllegalStateException
   749      *
   795      *         if there is an outstanding send operation
   750      * <p> Returns a {@code CompletableFuture<WebSocket>} which completes
   796      * @throws IllegalStateException
   751      * normally when the message has been sent or completes exceptionally if an
   797      *         if a previous Text message was not sent
   752      * error occurs.
   798      *         with {@code isLast == true}
       
   799      */
       
   800     default CompletableFuture<Void> sendBinary(byte[] message, boolean isLast) {
       
   801         Objects.requireNonNull(message, "message");
       
   802         return sendBinary(ByteBuffer.wrap(message), isLast);
       
   803     }
       
   804 
       
   805     /**
       
   806      * Sends a Ping message.
       
   807      *
       
   808      * <p> Returns immediately with a {@code CompletableFuture<Void>} which
       
   809      * completes normally when the message has been sent, or completes
       
   810      * exceptionally if an error occurs.
       
   811      *
       
   812      * <p> A Ping message may be sent or received by either client or server.
       
   813      * It may serve either as a keepalive or as a means to verify that the
       
   814      * remote endpoint is still responsive.
       
   815      *
       
   816      * <p> The message must consist of not more than {@code 125} bytes: {@code
       
   817      * message.remaining() <= 125}.
       
   818      *
   753      *
   819      * <p> The returned {@code CompletableFuture} can complete exceptionally
   754      * <p> The returned {@code CompletableFuture} can complete exceptionally
   820      * with:
   755      * with:
   821      * <ul>
   756      * <ul>
   822      * <li> {@link IOException}
   757      * <li> {@link IOException}
   823      *          if an I/O error occurs during this operation or the
   758      *          if an I/O error occurs during this operation
   824      *          {@code WebSocket} closes while this operation is in progress
   759      * <li> {@link IllegalStateException}
       
   760      *          if the {@code WebSocket} closes while this operation is in progress;
       
   761      *          or if a Close message has been sent already;
       
   762      *          or if there is an outstanding send operation;
       
   763      *          or if a previous Text message was not sent with {@code isLast == true}
   825      * </ul>
   764      * </ul>
       
   765      *
       
   766      * @implSpec This is equivalent to:
       
   767      * <pre>{@code
       
   768      *     sendBinary(ByteBuffer.wrap(message), isLast)
       
   769      * }</pre>
   826      *
   770      *
   827      * @param message
   771      * @param message
   828      *         the message
   772      *         the message
   829      *
   773      * @param isLast
   830      * @return a CompletableFuture of Void
   774      *         {@code true} if this is the final part of the message,
   831      *
   775      *         {@code false} otherwise
   832      * @throws IllegalStateException
   776      *
   833      *         if the WebSocket is closed
   777      * @return a CompletableFuture with this WebSocket
   834      * @throws IllegalStateException
   778      */
   835      *         if a Close message has been already sent
   779     default CompletableFuture<WebSocket> sendBinary(byte[] message, boolean isLast) {
   836      * @throws IllegalStateException
   780         Objects.requireNonNull(message, "message");
   837      *         if there is an outstanding send operation
   781         return sendBinary(ByteBuffer.wrap(message), isLast);
   838      * @throws IllegalArgumentException
   782     }
   839      *         if {@code message.remaining() > 125}
   783 
   840      */
   784     /**
   841     CompletableFuture<Void> sendPing(ByteBuffer message);
   785      * Sends a Ping message.
   842 
   786      *
   843     /**
   787      * <p> Returns a {@code CompletableFuture<WebSocket>} which completes
   844      * Sends a Pong message.
   788      * normally when the message has been sent or completes exceptionally if an
   845      *
   789      * error occurs.
   846      * <p> Returns immediately with a {@code CompletableFuture<Void>} which
   790      *
   847      * completes normally when the message has been sent, or completes
   791      * <p> A Ping message may be sent or received by either client or server.
   848      * exceptionally if an error occurs.
   792      * It may serve either as a keepalive or as a means to verify that the
   849      *
   793      * remote endpoint is still responsive.
   850      * <p> A Pong message may be unsolicited or may be sent in response to a
       
   851      * previously received Ping. In latter case the contents of the Pong is
       
   852      * identical to the originating Ping.
       
   853      *
   794      *
   854      * <p> The message must consist of not more than {@code 125} bytes: {@code
   795      * <p> The message must consist of not more than {@code 125} bytes: {@code
   855      * message.remaining() <= 125}.
   796      * message.remaining() <= 125}.
   856      *
   797      *
   857      * <p> The returned {@code CompletableFuture} can complete exceptionally
   798      * <p> The returned {@code CompletableFuture} can complete exceptionally
   858      * with:
   799      * with:
   859      * <ul>
   800      * <ul>
   860      * <li> {@link IOException}
   801      * <li> {@link IOException}
   861      *          if an I/O error occurs during this operation or the
   802      *          if an I/O error occurs during this operation
   862      *          {@code WebSocket} closes while this operation is in progress
   803      * <li> {@link IllegalStateException}
       
   804      *          if the {@code WebSocket} closes while this operation is in progress;
       
   805      *          or if a Close message has been sent already;
       
   806      *          or if there is an outstanding send operation
   863      * </ul>
   807      * </ul>
   864      *
   808      *
   865      * @param message
   809      * @param message
   866      *         the message
   810      *         the message
   867      *
   811      *
   868      * @return a CompletableFuture of Void
   812      * @return a CompletableFuture with this WebSocket
   869      *
   813      *
   870      * @throws IllegalStateException
       
   871      *         if the WebSocket is closed
       
   872      * @throws IllegalStateException
       
   873      *         if a Close message has been already sent
       
   874      * @throws IllegalStateException
       
   875      *         if there is an outstanding send operation
       
   876      * @throws IllegalArgumentException
   814      * @throws IllegalArgumentException
   877      *         if {@code message.remaining() > 125}
   815      *         if {@code message.remaining() > 125}
   878      */
   816      */
   879     CompletableFuture<Void> sendPong(ByteBuffer message);
   817     CompletableFuture<WebSocket> sendPing(ByteBuffer message);
       
   818 
       
   819     /**
       
   820      * Sends a Pong message.
       
   821      *
       
   822      * <p> Returns a {@code CompletableFuture<WebSocket>} which completes
       
   823      * normally when the message has been sent or completes exceptionally if an
       
   824      * error occurs.
       
   825      *
       
   826      * <p> A Pong message may be unsolicited or may be sent in response to a
       
   827      * previously received Ping. In latter case the contents of the Pong is
       
   828      * identical to the originating Ping.
       
   829      *
       
   830      * <p> The message must consist of not more than {@code 125} bytes: {@code
       
   831      * message.remaining() <= 125}.
       
   832      *
       
   833      * <p> The returned {@code CompletableFuture} can complete exceptionally
       
   834      * with:
       
   835      * <ul>
       
   836      * <li> {@link IOException}
       
   837      *          if an I/O error occurs during this operation
       
   838      * <li> {@link IllegalStateException}
       
   839      *          if the {@code WebSocket} closes while this operation is in progress;
       
   840      *          or if a Close message has been sent already;
       
   841      *          or if there is an outstanding send operation
       
   842      * </ul>
       
   843      *
       
   844      * @param message
       
   845      *         the message
       
   846      *
       
   847      * @return a CompletableFuture with this WebSocket
       
   848      *
       
   849      * @throws IllegalArgumentException
       
   850      *         if {@code message.remaining() > 125}
       
   851      */
       
   852     CompletableFuture<WebSocket> sendPong(ByteBuffer message);
   880 
   853 
   881     /**
   854     /**
   882      * Sends a Close message with the given close code and the reason.
   855      * Sends a Close message with the given close code and the reason.
   883      *
   856      *
   884      * <p> Returns immediately with a {@code CompletableFuture<Void>} which
   857      * <p> Returns a {@code CompletableFuture<WebSocket>} which completes
   885      * completes normally when the message has been sent, or completes
   858      * normally when the message has been sent or completes exceptionally if an
   886      * exceptionally if an error occurs.
   859      * error occurs.
   887      *
   860      *
   888      * <p> A Close message may consist of a close code and a reason for closing.
   861      * <p> A Close message may consist of a close code and a reason for closing.
   889      * The reason must have a valid UTF-8 representation not longer than {@code
   862      * The reason must have a valid UTF-8 representation not longer than {@code
   890      * 123} bytes. The reason may be useful for debugging or passing information
   863      * 123} bytes. The reason may be useful for debugging or passing information
   891      * relevant to the connection but is not necessarily human readable.
   864      * relevant to the connection but is not necessarily human readable.
   892      *
   865      *
   893      * <p> The returned {@code CompletableFuture} can complete exceptionally
   866      * <p> The returned {@code CompletableFuture} can complete exceptionally
   894      * with:
   867      * with:
   895      * <ul>
   868      * <ul>
   896      * <li> {@link IOException}
   869      * <li> {@link IOException}
   897      *          if an I/O error occurs during this operation or the
   870      *          if an I/O error occurs during this operation
   898      *          {@code WebSocket} closes while this operation is in progress
   871      * <li> {@link IllegalStateException}
       
   872      *          if the {@code WebSocket} closes while this operation is in progress;
       
   873      *          or if a Close message has been sent already;
       
   874      *          or if there is an outstanding send operation
   899      * </ul>
   875      * </ul>
   900      *
   876      *
   901      * @param code
   877      * @param code
   902      *         the close code
   878      *         the close code
   903      * @param reason
   879      * @param reason
   904      *         the reason; can be empty
   880      *         the reason; can be empty
   905      *
   881      *
   906      * @return a CompletableFuture of Void
   882      * @return a CompletableFuture with this WebSocket
   907      *
   883      *
   908      * @throws IllegalStateException
       
   909      *         if the WebSocket is closed
       
   910      * @throws IllegalStateException
       
   911      *         if a Close message has been already sent
       
   912      * @throws IllegalStateException
       
   913      *         if there is an outstanding send operation
       
   914      * @throws IllegalArgumentException
   884      * @throws IllegalArgumentException
   915      *         if the {@code reason} doesn't have a valid UTF-8
   885      *         if {@code reason} doesn't have an UTF-8 representation not longer
   916      *         representation not longer than {@code 123} bytes
   886      *         than {@code 123} bytes
   917      */
   887      */
   918     CompletableFuture<Void> sendClose(CloseCode code, CharSequence reason);
   888     CompletableFuture<WebSocket> sendClose(CloseCode code, CharSequence reason);
   919 
   889 
   920     /**
   890     /**
   921      * Sends an empty Close message.
   891      * Sends an empty Close message.
   922      *
   892      *
   923      * <p> Returns immediately with a {@code CompletableFuture<Void>} which
   893      * <p> Returns a {@code CompletableFuture<WebSocket>} which completes
   924      * completes normally when the message has been sent, or completes
   894      * normally when the message has been sent or completes exceptionally if an
   925      * exceptionally if an error occurs.
   895      * error occurs.
   926      *
   896      *
   927      * <p> The returned {@code CompletableFuture} can complete exceptionally
   897      * <p> The returned {@code CompletableFuture} can complete exceptionally
   928      * with:
   898      * with:
   929      * <ul>
   899      * <ul>
   930      * <li> {@link IOException}
   900      * <li> {@link IOException}
   931      *          if an I/O error occurs during this operation or the
   901      *          if an I/O error occurs during this operation
   932      *          {@code WebSocket} closes while this operation is in progress
   902      * <li> {@link IllegalStateException}
       
   903      *          if the {@code WebSocket} closes while this operation is in progress;
       
   904      *          or if a Close message has been sent already;
       
   905      *          or if there is an outstanding send operation
   933      * </ul>
   906      * </ul>
   934      *
   907      *
   935      * @return a CompletableFuture of Void
   908      * @return a CompletableFuture with this WebSocket
   936      *
   909      */
   937      * @throws IllegalStateException
   910     CompletableFuture<WebSocket> sendClose();
   938      *         if the WebSocket is closed
       
   939      * @throws IllegalStateException
       
   940      *         if a Close message has been already sent
       
   941      * @throws IllegalStateException
       
   942      *         if there is an outstanding send operation
       
   943      */
       
   944     CompletableFuture<Void> sendClose();
       
   945 
   911 
   946     /**
   912     /**
   947      * Requests {@code n} more messages to be received by the {@link Listener
   913      * Requests {@code n} more messages to be received by the {@link Listener
   948      * Listener}.
   914      * Listener}.
   949      *
   915      *