code: protected WebSocketAdapter createWebSocketAdapter(@NonNull final WeakReference<SocketStateListener> stateListenerWeakReference) {
  
          return new WebSocketAdapter() {
  
              @Override
              public void onConnected(WebSocket websocket, Map<String, List<String>> headers) throws Exception {
                  super.onConnected(websocket, headers);
                  final SocketStateListener stateListener = stateListenerWeakReference.get();
                  if (stateListener != null) {
                      stateListener.onConnected();
                  }
              }
  
              @Override
              public void onConnectError(WebSocket websocket, WebSocketException exception) throws Exception {
                  super.onConnectError(websocket, exception);
                  final SocketStateListener stateListener = stateListenerWeakReference.get();
                  if (stateListener != null) {
                      stateListener.onError(uri.getRawPath(), proxyAddress, exception);
                  }
              }
  
              @Override
              public void onDisconnected(WebSocket websocket, WebSocketFrame serverCloseFrame, WebSocketFrame clientCloseFrame, boolean closedByServer) throws Exception {
                  super.onDisconnected(websocket, serverCloseFrame, clientCloseFrame, closedByServer);
                  final SocketStateListener stateListener = stateListenerWeakReference.get();
                  if (stateListener != null) {
                      stateListener.onDisconnected();
                  }
              }
  
              @Override
              public void onTextMessage(WebSocket websocket, String text) throws Exception {
                  super.onTextMessage(websocket, text);
                  log.d("Socket message received = " + text);
                  messageListener.onMessage(text);
              }
  
              @Override
              public void onBinaryMessage(WebSocket websocket, byte[] binary) throws Exception {
                  super.onBinaryMessage(websocket, binary);
                  log.d("Socket binary message received.");
              }
          };
      }
docs: Create adapter for websocket library events.
  
  @param stateListenerWeakReference Listener for socket state changes.
  @return Adapter for websocket library events.