title: Using OkHttp3 WebSocket with Retrofit to continuously track an android devices location
body: <p>I have the following java code that I'd like to use in an android app to query an api for continuous lat/lng changes of a device that is running a client app, I want to track the device.  I believe the WebSocketCall method I'm attempting to use is deprecated.  From what I can tell, there's a problem with how I'm trying to use the webSocket call to create the retrofit client and enqueue the data from the WebSocketListner into retrofit. I've researched several WebSocketListener examples and being a total n00b, I haven't been able to figure out the code.  My idea is to keep the connection open to the api via WebSocket and process the data response using retrofit. Any assistance would be greatly appreciated.</p>
  
  <pre><code>private WebSocketCall webSocket;
  
      private void createWebSocket() {
          final MainApplication application = (MainApplication) getActivity().getApplication();
          application.getServiceAsync(new MainApplication.GetServiceCallback() {
              @Override
              public void onServiceReady(final OkHttpClient client, final Retrofit retrofit, WebService service) {
                  User user = application.getUser();
                  map.moveCamera(CameraUpdateFactory.newLatLngZoom(
                          new LatLng(user.getLatitude(), user.getLongitude()), user.getZoom()));
                  service.getDevices().enqueue(new WebServiceCallback&lt;List&lt;Device&gt;&gt;(getContext()) {
                      @Override
                      public void onSuccess(retrofit2.Response&lt;List&lt;Device&gt;&gt; response) {
                          for (Device device : response.body()) {
                              if (device != null) {
                                  devices.put(device.getId(), device);
                              }
                          }
  
                          Request request = new Request.Builder().url(retrofit.baseUrl().url().toString() + "api/socket").build();
                          webSocket = WebSocketCall.create(client, request);
                          webSocket.enqueue(new WebSocketListener() {
                              @Override
                              public void onOpen(WebSocket webSocket, Response response) {
                              }
  
                              @Override
                              public void onFailure(IOException e, Response response) {
                                  reconnectWebSocket();
                              }
  
                              @Override
                              public void onMessage(ResponseBody message) throws IOException {
                                  final String data = message.string();
                                  handler.post(new Runnable() {
                                      @Override
                                      public void run() {
                                          try {
                                              handleMessage(data);
                                          } catch (IOException e) {
                                              Log.w(MainFragment.class.getSimpleName(), e);
                                          }
                                      }
                                  });
                              }
  
                              @Override
                              public void onClose(int code, String reason) {
                                  reconnectWebSocket();
                              }
                          });
                      }
                  });
              }
  
              @Override
              public boolean onFailure() {
                  return false;
              }
          });
      }
  </code></pre>