WebSocket client API

Andrej Golovnin andrej.golovnin at gmail.com
Thu Sep 3 08:28:34 UTC 2015


Hi Michael,

> Can you explain why you need j.n.Proxy rather than the
> String/InetSocketAddress
> combination proposed for HttpClient?

String is not type safe. And allowing to define proxy only for the
specific protocol is not enough from my experience. Sometimes you have
to choose proxy based on the host of the URL you try to connect to.
What HttpClient.Builder.proxy() should really take as parameter, is
the j.n.ProxySelector. The most code, I have seen so far, does not
really care about proxies. But everyone expects that it still works
with proxies. j.n.URL.openConnection() gives us this magic because at
some point in sun.net.www.protocol.http.HttpURLConnection the method
ProxySelector.getDefault().select(URI) is called for the URL and the
connection is established using the proxy. The same behaviour I expect
from HttpClient and WebSocket. Therefore I think you have to deal with
j.n.Proxy and j.n.ProxySelector in the HttpClient code anyway. And
that's why I think you should use them in the Public API of HttpClient
and WebSocket as well.

Here is a small summary of the API I would like to see:

/**
 * Defines a selector to be used by this client to select the proxy
server for a request.
 * If no selector is defined for this client, the client will use
{@link j.n.ProxySelector.getDefault()} as it's selector.
 *
 * @param selector    the selector to be used by this client; may not be null.
 *
 * @throws IllegalArgumentException if selector is null.
 */
j.n.HttpClient.Builder.proxySelector(j.n.ProxySelector selector)

/**
 * Defines the proxy to be used by this request.
 * If no proxy is defined for this request, the selector from the
HttpClient will be used to select the proxy.
 *
 * @param proxy   the proxy to be used by this request; may not be null.
 *
 * @throws IllegalArgumentException if proxy is null.
 */
j.n.HttpRequest.Builder.proxy(j.n.Proxy proxy)

/**
 * Defines the proxy to be used by this WebSocket.
 * If no proxy is defined for this WebSocket, the default selector
{@link j.n.ProxySelector.getDefault()} will be used to select the
proxy.
 *
 * @param proxy   the proxy to be used by this WebSocket; may not be null.
 *
 * @throws IllegalArgumentException if proxy is null.
 */
j.n.WebSocket.Builder.proxy(j.n.Proxy proxy)


As always fell free to use it or to ignore it. My code is already in
production and works. :-)

I hope this helps.

Best regards,
Andrej Golovnin


More information about the net-dev mailing list