September 2019
Intermediate to advanced
816 pages
18h 47m
English
The HTTP Client supports the WebSocket protocol. In API terms, the core of the implementation is the java.net.http.WebSocket interface. This interface exposes a suite of methods for handling WebSocket communication.
Building a WebSocket instance asynchronously can be accomplished via HttpClient.newWebSocketBuilder().buildAsync().
For example, we can connect to the well known Meetup RSVP WebSocket endpoint (ws://stream.meetup.com/2/rsvps), as follows:
HttpClient client = HttpClient.newHttpClient();WebSocket webSocket = client.newWebSocketBuilder() .buildAsync(URI.create("ws://stream.meetup.com/2/rsvps"), wsListener).get(10, TimeUnit.SECONDS);
By its nature, the WebSocket protocol is bidirectional. In order to send data, we ...