简体   繁体   中英

JavaScript WebSocket: Cant connect to localhost

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.

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.

The only message I get is:

Firefox kann keine Verbindung zu dem Server unter ws://localhost:8080/toUpper/ aufbauen. game.js:837:13

Which translates to: Firefox can't establish a connection to the server ws://localhost:8080/toUpper/

The certain line (line 837) says: webSocket = new WebSocket("ws://localhost:8080/toUpper/");

There are two classes which belong to this call.

@WebServlet(urlPatterns = "/toUpper")
public class ToUpperWebSocketServlet extends WebSocketServlet {
    /**
     * 
     */
    private static final long serialVersionUID = 9106843498328099741L;
    @Override
    public void configure(WebSocketServletFactory factory) {
        factory.register(ToUpperWebSocket.class);
    }
}

and this one

@WebSocket
public class ToUpperWebSocket {
    public static ArrayList<String> allgames = new ArrayList<String>();
    public static ArrayList<Session> clientstogame = new ArrayList<Session>();
    public static ArrayList<Integer> allUniqueGames = new ArrayList<Integer>();
    public static ArrayList<Session> allUniqueGamesSessions = new ArrayList<Session>();
    public static volatile ArrayList<Long> allUniqueGamesDates = new ArrayList<Long>();
    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 < 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 < allUniqueGames.size(); i++) {
                    if (response.contains(allUniqueGames.get(i) + "")) {
                        allUniqueGamesDates.set(i, System.currentTimeMillis());
                    }
                }

                for (int i = 0; i < 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 < allUniqueGamesDates.size(); i++) {
                    long millisOfHost = allUniqueGamesDates.get(i);
                    long currentMillis = System.currentTimeMillis();
                    if (currentMillis - millisOfHost > 10000) {
                        int idOfClosedGame = allUniqueGames.get(i);
                        for (int j = 0; j < 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) {
                }
            }
        }
    }
}

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.

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.

Here my pom.xml if it helps:

<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">
    <modelVersion>4.0.0</modelVersion>
    <groupId>OurGroup</groupId>
    <artifactId>TheTitle</artifactId>
    <packaging>war</packaging>
    <version>0.0.1-SNAPSHOT</version>
    <name>the Maven Webapp</name>
    <url>http://maven.apache.org</url>

    <properties>
        <jetty.version>9.3.9.v20160517</jetty.version>
    </properties>

    <dependencies>
        <!--Jetty dependencies start here -->
        <dependency>
            <groupId>org.eclipse.jetty</groupId>
            <artifactId>jetty-server</artifactId>
            <version>${jetty.version}</version>
        </dependency>
        <dependency>
            <groupId>org.eclipse.jetty</groupId>
            <artifactId>jetty-servlet</artifactId>
            <version>${jetty.version}</version>
        </dependency>
        <!--Jetty dependencies end here -->
        <!--Jetty Websocket server side dependencies start here -->
        <!--Jetty JSR-356 Websocket server side dependency -->
        <dependency>
            <groupId>org.eclipse.jetty.websocket</groupId>
            <artifactId>javax-websocket-server-impl</artifactId>
            <version>${jetty.version}</version>
        </dependency>
        <!--Jetty Websocket API server side dependency -->
        <dependency>
            <groupId>org.eclipse.jetty.websocket</groupId>
            <artifactId>websocket-server</artifactId>
            <version>${jetty.version}</version>
        </dependency>
        <!--Jetty Websocket server dependencies end here -->
        <!--Jetty Websocket client side dependencies start here -->
        <!--JSR-356 Websocket client side depencency -->
        <dependency>
            <groupId>org.eclipse.jetty.websocket</groupId>
            <artifactId>javax-websocket-client-impl</artifactId>
            <version>${jetty.version}</version>
        </dependency>
        <!--Jetty Websocket API client side dependency -->
        <dependency>
            <groupId>org.eclipse.jetty.websocket</groupId>
            <artifactId>websocket-client</artifactId>
            <version>${jetty.version}</version>
        </dependency>
        <!--Jetty Websocket client side dependencies end here -->
    </dependencies>
    <build>
        <plugins>
            <plugin>
                <groupId>org.apache.maven.plugins</groupId>
                <artifactId>maven-war-plugin</artifactId>
                <version>2.3</version>
                <configuration>
                    <failOnMissingWebXml>false</failOnMissingWebXml>
                </configuration>
            </plugin>
            <plugin>
                <groupId>org.eclipse.jetty</groupId>
                <artifactId>jetty-maven-plugin</artifactId>
                <version>${jetty.version}</version>
                <configuration>
                    <scanIntervalSeconds>5</scanIntervalSeconds>
                </configuration>
            </plugin>
        </plugins>
        <finalName>theName</finalName>
    </build>
</project>

I am hoping for some help. Regards, J.

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.

I think i got it this time on my own.

I commented the Webservlet annotation with the url pattern out and wrote the servlet mappings into the web.xml. The web.xml was empty and didn't exist before I converted the project into a maven project. Somehow it worked before with these annotations, but I didn't compile that version. Anyway it should work now.

Although someone said to me the servlet annotation with the Url-Pattern in the .java files should work. but in my case I didn't see why that didn't work. Therefore i will go with the web.xml and put the servlet mappings in there.

The technical post webpages of this site follow the CC BY-SA 4.0 protocol. If you need to reprint, please indicate the site URL or the original address.Any question please contact:yoyou2525@163.com.

 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM