WebSocket client API

Roger Riggs Roger.Riggs at Oracle.com
Tue Sep 1 16:05:24 UTC 2015


Hi Pavel,

A few comments on the API:

  - The Incoming class combines separate functions that would be easier to
    use if the methods were directly on the Builder.  It would also 
allow (not require)
    lambda's to be used or method references if the code was complex.
    The Builder methods can use generic java.util.function interfaces 
with the appropriate parameter types.
    Each of the callbacks should include the websocket, though with 
lambda it could capture
    the WS as needed.  For example,  (also separating close and error 
callbacks)

|WebSocket ws = WebSocket.newBuilder("ws://websocket.example.com") 
.onReceiveText( (ws, text) -> System.out.println(text)) .onError( (ws, 
t) -> t.printStackTrace()) .onClose( (ws, msg) -> 
System.out.println("closing: " + msg))  .create();|

  - The design to wrap the Outgoing data creates an extra step that 
makes the API
    a bit harder to use.  It would be more natural to add the sendAsync  
methods to WebSocket
    and avoid the creation of extra objects.

|ws.sendAsync("hello"); ws.sendAsync(bytebuffer); // an alternate 
signature could use varargs, ByteBuffer data... |


Roger

||

On 8/31/2015 10:30 AM, Pavel Rappo wrote:
> Hi,
>
> I would appreciate if you help to review a WebSocket client API proposed for
> JDK 9. This work is a part of JEP-110 [1] and has started its public path with
> HTTP client review in March this year [2].
>
> Proposed WebSocket API is relatively small and focuses on convenient exchange of
> data in a fully asynchronous fashion. API consists of 6 types located in the
> java.net package [3]:
>
> 1. WebSocket
> 2. WebSocket.Builder
> 3. WebSocket.Incoming
> 4. WebSocket.Incoming.Chunks
> 5. WebSocket.Outgoing
> 6. WebSocketException
>
> Starting point is a class description for java.net.WebSocket. Along with
> in-javadoc examples, several API test samples are provided in the webrev [4] and
> named test/java/net/WebSocket/Example%.java. They are only for informational
> purposes and won't be included in the final version of the API.
>
> I would appreciate any feedback on this API. Thanks.
>
> -------------------------------------------------------------------------------
> [1] http://openjdk.java.net/jeps/110
> [2] http://mail.openjdk.java.net/pipermail/net-dev/2015-March/008932.html
> [3] http://cr.openjdk.java.net/~prappo/8087113/javadoc.00/
> [4] http://cr.openjdk.java.net/~prappo/8087113/webrev.00/
>
> -Pavel



More information about the net-dev mailing list