title: JavaScript WebSocket: Cant connect to localhost
body: <p>I am working on a project, in which I have to create a webapplication. This webapp runs with Jetty on a server, which belongs to our university.</p>
  
  <p>I have a backup from this server, which works on the server. But I wanted to work on it in eclipse with the jetty maven plugin. And now the project doesn't work in my IDE.</p>
  
  <p>The only message I get is:
  <code><p>Firefox kann keine Verbindung zu dem Server unter ws://localhost:8080/toUpper/ aufbauen. game.js:837:13</p></code></p>
  
  <p>Which translates to:
  Firefox can't establish a connection to the server ws://localhost:8080/toUpper/</p>
  
  <p>The certain line (line 837) says:
  <code>webSocket = new WebSocket("ws://localhost:8080/toUpper/");</code></p>
  
  <p>There are two classes which belong to this call.</p>
  
  <pre><code>@WebServlet(urlPatterns = "/toUpper")
  public class ToUpperWebSocketServlet extends WebSocketServlet {
      /**
       * 
       */
      private static final long serialVersionUID = 9106843498328099741L;
      @Override
      public void configure(WebSocketServletFactory factory) {
          factory.register(ToUpperWebSocket.class);
      }
  }
  </code></pre>
  
  <p>and this one</p>
  
  <pre><code>@WebSocket
  public class ToUpperWebSocket {
      public static ArrayList&lt;String&gt; allgames = new ArrayList&lt;String&gt;();
      public static ArrayList&lt;Session&gt; clientstogame = new ArrayList&lt;Session&gt;();
      public static ArrayList&lt;Integer&gt; allUniqueGames = new ArrayList&lt;Integer&gt;();
      public static ArrayList&lt;Session&gt; allUniqueGamesSessions = new ArrayList&lt;Session&gt;();
      public static volatile ArrayList&lt;Long&gt; allUniqueGamesDates = new ArrayList&lt;Long&gt;();
      public static int allUniqueGamesCounter = 0;
      public static int counter;
      public static boolean checkEnabled = false;
  
      @OnWebSocketMessage
      public void onText(Session session, String message) throws IOException {
          System.out.println("Message received:" + message);
          if (session.isOpen()) {
              String response = message.toUpperCase();
              if (response.contains("GENERATE SPECTATOR ID")) {
                  if (!checkEnabled) {
                      checkEnabled = true;
                      Thread t1 = new Thread(new AvailableChecker());
                      t1.start();
                  }
                  allUniqueGamesCounter++;
                  allUniqueGames.add(allUniqueGamesCounter);
                  allUniqueGamesDates.add(System.currentTimeMillis());
                  allUniqueGamesSessions.add(session);
                  session.getRemote().sendString("NEW SPECTATOR ID" + allUniqueGamesCounter);
              }
  
              if (response.contains("NEW SPECTATOR ")) {
                  response = response.replace("NEW SPECTATOR ", "");
                  int responseInt = Integer.parseInt(response);
  
                  if (allUniqueGames.contains(responseInt)) {
                      for (int j = 0; j &lt; allUniqueGames.size(); j++) {
                          String stringValue = allUniqueGames.get(j) + "";
                          if (response.contains(stringValue)) {
                              allgames.add(response);
                              clientstogame.add(session);
                              session.getRemote().sendString("SPIEL GUCKEN MIT ID: " + response);
                          }
                      }
  
                  } else {
                      session.getRemote().sendString("SPIEL NICHT VORHANDEN MIT ID: " + response);
  
                  }
              }
              if (response.contains("POINTS") | response.contains("STATUS") | response.contains("GAME")) {
                  for (int i = 0; i &lt; allUniqueGames.size(); i++) {
                      if (response.contains(allUniqueGames.get(i) + "")) {
                          allUniqueGamesDates.set(i, System.currentTimeMillis());
                      }
                  }
  
                  for (int i = 0; i &lt; allgames.size(); i++) {
                      if (response.contains(allgames.get(i))) {
                          try {
                              session.getRemote().sendString(response);
                              clientstogame.get(i).getRemote().sendString(response);
                              // clientstogame.get(i).getRemote().sendString("DEBUG
                              // ANZAHL SPECTATOR INSGESAMT: "+allgames.size());
                          } catch (Exception e) {
                              allgames.remove(i);
                              clientstogame.remove(i);
                          }
                      }
                  }
                  session.getRemote().sendString("ES WILL JEMAND SPIEL ANBIETEN MIT ID: " + response);
              }
          }
      }
  
      @OnWebSocketConnect
      public void onConnect(Session session) throws IOException {    
          System.out.println(session.getRemoteAddress().getHostString() + " connected!");
      }
  
      @OnWebSocketClose
      public void onClose(Session session, int status, String reason) {
  
          System.out.println(session.getRemoteAddress().getHostString() + " closed!");
      }
  
      public class AvailableChecker implements Runnable {
          public void run() {
              while (true) {
                  for (int i = 0; i &lt; allUniqueGamesDates.size(); i++) {
                      long millisOfHost = allUniqueGamesDates.get(i);
                      long currentMillis = System.currentTimeMillis();
                      if (currentMillis - millisOfHost &gt; 10000) {
                          int idOfClosedGame = allUniqueGames.get(i);
                          for (int j = 0; j &lt; allgames.size(); j++) {
                              if (allgames.contains(idOfClosedGame + "")) {
                                  try {
                                          clientstogame.get(j).getRemote().sendString("SPIEL WURDE GESCHLOSSEN");
                                      clientstogame.remove(j);
                                      allgames.remove(j);
                                  } catch (Exception e) {
  
                                  }
                              }
                          }
                      }
                  }
                  try {
                      Thread.sleep(1000);
                  } catch (Exception e) {
                  }
              }
          }
      }
  }
  </code></pre>
  
  <p>The whole client side stuff works. all html, css and Javascript files, but this one line.
  When I run the project with maven there is no error message in the eclipse console, too.</p>
  
  <p>This Error only occurs when I run the project on localhost. On the server it works.
  To make it work on localhost i just changed the URI's from ws://theserver.com:port/toUpper to ws://localhost:8080/toUpper
  But that obviously didn't help.</p>
  
  <p>Here my pom.xml if it helps:</p>
  
  <pre><code>&lt;project xmlns="http://maven.apache.org/POM/4.0.0" xmlns:xsi="http://www.w3.org/2001/XMLSchema-instance" xsi:schemaLocation="http://maven.apache.org/POM/4.0.0 http://maven.apache.org/maven-v4_0_0.xsd"&gt;
      &lt;modelVersion&gt;4.0.0&lt;/modelVersion&gt;
      &lt;groupId&gt;OurGroup&lt;/groupId&gt;
      &lt;artifactId&gt;TheTitle&lt;/artifactId&gt;
      &lt;packaging&gt;war&lt;/packaging&gt;
      &lt;version&gt;0.0.1-SNAPSHOT&lt;/version&gt;
      &lt;name&gt;the Maven Webapp&lt;/name&gt;
      &lt;url&gt;http://maven.apache.org&lt;/url&gt;
  
      &lt;properties&gt;
          &lt;jetty.version&gt;9.3.9.v20160517&lt;/jetty.version&gt;
      &lt;/properties&gt;
  
      &lt;dependencies&gt;
          &lt;!--Jetty dependencies start here --&gt;
          &lt;dependency&gt;
              &lt;groupId&gt;org.eclipse.jetty&lt;/groupId&gt;
              &lt;artifactId&gt;jetty-server&lt;/artifactId&gt;
              &lt;version&gt;${jetty.version}&lt;/version&gt;
          &lt;/dependency&gt;
          &lt;dependency&gt;
              &lt;groupId&gt;org.eclipse.jetty&lt;/groupId&gt;
              &lt;artifactId&gt;jetty-servlet&lt;/artifactId&gt;
              &lt;version&gt;${jetty.version}&lt;/version&gt;
          &lt;/dependency&gt;
          &lt;!--Jetty dependencies end here --&gt;
          &lt;!--Jetty Websocket server side dependencies start here --&gt;
          &lt;!--Jetty JSR-356 Websocket server side dependency --&gt;
          &lt;dependency&gt;
              &lt;groupId&gt;org.eclipse.jetty.websocket&lt;/groupId&gt;
              &lt;artifactId&gt;javax-websocket-server-impl&lt;/artifactId&gt;
              &lt;version&gt;${jetty.version}&lt;/version&gt;
          &lt;/dependency&gt;
          &lt;!--Jetty Websocket API server side dependency --&gt;
          &lt;dependency&gt;
              &lt;groupId&gt;org.eclipse.jetty.websocket&lt;/groupId&gt;
              &lt;artifactId&gt;websocket-server&lt;/artifactId&gt;
              &lt;version&gt;${jetty.version}&lt;/version&gt;
          &lt;/dependency&gt;
          &lt;!--Jetty Websocket server dependencies end here --&gt;
          &lt;!--Jetty Websocket client side dependencies start here --&gt;
          &lt;!--JSR-356 Websocket client side depencency --&gt;
          &lt;dependency&gt;
              &lt;groupId&gt;org.eclipse.jetty.websocket&lt;/groupId&gt;
              &lt;artifactId&gt;javax-websocket-client-impl&lt;/artifactId&gt;
              &lt;version&gt;${jetty.version}&lt;/version&gt;
          &lt;/dependency&gt;
          &lt;!--Jetty Websocket API client side dependency --&gt;
          &lt;dependency&gt;
              &lt;groupId&gt;org.eclipse.jetty.websocket&lt;/groupId&gt;
              &lt;artifactId&gt;websocket-client&lt;/artifactId&gt;
              &lt;version&gt;${jetty.version}&lt;/version&gt;
          &lt;/dependency&gt;
          &lt;!--Jetty Websocket client side dependencies end here --&gt;
      &lt;/dependencies&gt;
      &lt;build&gt;
          &lt;plugins&gt;
              &lt;plugin&gt;
                  &lt;groupId&gt;org.apache.maven.plugins&lt;/groupId&gt;
                  &lt;artifactId&gt;maven-war-plugin&lt;/artifactId&gt;
                  &lt;version&gt;2.3&lt;/version&gt;
                  &lt;configuration&gt;
                      &lt;failOnMissingWebXml&gt;false&lt;/failOnMissingWebXml&gt;
                  &lt;/configuration&gt;
              &lt;/plugin&gt;
              &lt;plugin&gt;
                  &lt;groupId&gt;org.eclipse.jetty&lt;/groupId&gt;
                  &lt;artifactId&gt;jetty-maven-plugin&lt;/artifactId&gt;
                  &lt;version&gt;${jetty.version}&lt;/version&gt;
                  &lt;configuration&gt;
                      &lt;scanIntervalSeconds&gt;5&lt;/scanIntervalSeconds&gt;
                  &lt;/configuration&gt;
              &lt;/plugin&gt;
          &lt;/plugins&gt;
          &lt;finalName&gt;theName&lt;/finalName&gt;
      &lt;/build&gt;
  &lt;/project&gt;
  </code></pre>
  
  <p>I am hoping for some help.
  Regards,
  J.</p>
  
  <p>EDIT:
  The project is still on the server. The .class files weren't compiled by me. They were compiled by someone else with the command line using javac.
  When I use the URI ws://serveraddress.com:1234/toUpper for the websocket when I run my project on localhost it seems to work.</p>