Search code examples
websocketokhttp

Okhttp Websocket, how to get the Socket instance created by its default SSLSocketFactory after connection?


I need to have access to the instance of the Socket class created internally by the Okhttp WebSocket, but couldn't find any way to do such a thing.

I used the bellow code for creating the websocket:

    Request request = new Request.Builder().url("example.com").build();
    WebSocket ws = client.newWebSocket(request, listener);

Is there any public method or private method using reflection to get the created Socket instance?


Solution

  • Create your own subclass of SSLSocketFactory that delegates to a 'real' implementation. Then keep track of the socket instance you returned.