title: How to instantiate a WebSocketAdapter instance for Jetty websockets
body: <p>I followed the example to create a websocket server:</p>
  
  <pre><code>    Server server = new Server();
      ServerConnector connector = new ServerConnector(server);
      connector.setPort(port);
      server.addConnector(connector);
  
      ServletContextHandler servletContextHandler = new ServletContextHandler(server, "/", true, false);
  
      EventServlet es = injector.getInstance(EventServlet.class);
      servletContextHandler.addServlet(new ServletHolder(es), "/events/*");
  </code></pre>
  
  <p>The EventServlet class looks like:</p>
  
  <pre><code>    public class EventServlet extends WebSocketServlet {
          @Override
          public void configure(WebSocketServletFactory factory) {
              factory.getPolicy().setIdleTimeout(10000);
              factory.register(EventSocketCache.class);
  
          }
      }
  </code></pre>
  
  <p>The EventSocketCache looks like:</p>
  
  <pre><code>public class EventSocketCache extends WebSocketAdapter {
  
  
  private static int i = 0;
  private static int counter = 0;
  
  private static Map&lt;Integer, Session&gt; sessionMap = new HashMap&lt;&gt;();
  
  private final Cache&lt;String, String&gt; testCache;
  
  @Inject
  public EventSocketCache(Cache&lt;String, String&gt; testCache) {
      this.testCache = testCache;
  }
  
  @Override
  public void onWebSocketConnect(Session session) {
      super.onWebSocketConnect(session);
      System.out.println("Socket Connected: " + session);
      System.out.println("Connect: " + session.getRemoteAddress().getAddress());
      try {
          session.getRemote().sendString("Hello Webbrowser");
          session.setIdleTimeout(50000);        
      } catch (IOException e) {
          e.printStackTrace();
      }
  }
  
  
  @Override
  public void onWebSocketText(String message) {
      super.onWebSocketText(message);
      System.out.println("Received TEXT message: " + message);
  }
  
  
  @Override
  public void onWebSocketBinary(byte[] payload, int offset, int len) {
      byte[] newData = Arrays.copyOfRange(payload, offset, offset + len);
  
      try {
          Common.Success success = Common.Success.parseFrom(newData);
          System.err.println("------&gt; " + success.getIsSuccess());
      } catch (InvalidProtocolBufferException e) {
          e.printStackTrace();
      }
  }
  
  
  @Override
  public void onWebSocketClose(int statusCode, String reason) {
      System.err.println("^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^");
      // Remove from the list here....
      super.onWebSocketClose(statusCode, reason);
      System.out.println("Socket Closed: [" + statusCode + "] " + reason);
  }
  
  
  @Override
  public void onWebSocketError(Throwable cause) {
      System.err.println("######################################");
      super.onWebSocketError(cause);
      cause.printStackTrace(System.err);
  }
  </code></pre>
  
  <p>}</p>
  
  <p>Now when I use my client and send a request, I end up getting:</p>
  
  <pre><code>org.eclipse.jetty.websocket.api.UpgradeException: Didn't switch protocols
  at org.eclipse.jetty.websocket.client.io.UpgradeConnection.validateResponse(UpgradeConnection.java:249)
  at org.eclipse.jetty.websocket.client.io.UpgradeConnection.read(UpgradeConnection.java:181)
  at org.eclipse.jetty.websocket.client.io.UpgradeConnection.onFillable(UpgradeConnection.java:126)
  at org.eclipse.jetty.io.AbstractConnection$ReadCallback.run(AbstractConnection.java:358)
  at org.eclipse.jetty.util.thread.QueuedThreadPool.runJob(QueuedThreadPool.java:596)
  at org.eclipse.jetty.util.thread.QueuedThreadPool$3.run(QueuedThreadPool.java:527)
  at java.lang.Thread.run(Thread.java:722)
  Disconnected from the target VM, address: '127.0.0.1:63256', transport: 'socket'
  java.util.concurrent.ExecutionException: org.eclipse.jetty.websocket.api.UpgradeException: Didn't switch protocols
      at org.eclipse.jetty.util.FuturePromise.get(FuturePromise.java:123)
      at com.gamecenter.websockets.EventClient.main(EventClient.java:25)
  Caused by: org.eclipse.jetty.websocket.api.UpgradeException: Didn't switch protocols
      at org.eclipse.jetty.websocket.client.io.UpgradeConnection.validateResponse(UpgradeConnection.java:249)
      at org.eclipse.jetty.websocket.client.io.UpgradeConnection.read(UpgradeConnection.java:181)
      at org.eclipse.jetty.websocket.client.io.UpgradeConnection.onFillable(UpgradeConnection.java:126)
      at org.eclipse.jetty.io.AbstractConnection$ReadCallback.run(AbstractConnection.java:358)
      at org.eclipse.jetty.util.thread.QueuedThreadPool.runJob(QueuedThreadPool.java:596)
      at org.eclipse.jetty.util.thread.QueuedThreadPool$3.run(QueuedThreadPool.java:527)
      at java.lang.Thread.run(Thread.java:722)
  </code></pre>
  
  <p>It seems as there is a problem creating an instance of EventSocketCache; if I don't have the constructor in there, everything works fine.
  I'd like to know how to instantiate EventSocketCache properly and register it with EventServlet so things work?</p>