我正在開發一個項目,在該項目中我必須創建一個Web應用程序。這個Web應用程序與Jetty一起運行在服務器上,該服務器屬於我們的大學。JavaScript WebSocket:無法連接到本地主機
我從服務器上有一個備份,它在服務器上工作。但我想在Eclipse中使用jetty maven插件來處理它。而現在這個項目在我的IDE中不起作用。
我得到的唯一信息是:
Firefox kann keine Verbindung zu dem Server unter ws://localhost:8080/toUpper/ aufbauen. game.js:837:13
翻譯爲: 火狐無法與服務器建立WS的連接://本地主機:8080/TOUPPER/
的某些行(行837)說: webSocket = new WebSocket("ws://localhost:8080/toUpper/");
有兩個類屬於這個調用。
@WebServlet(urlPatterns = "/toUpper")
public class ToUpperWebSocketServlet extends WebSocketServlet {
/**
*
*/
private static final long serialVersionUID = 9106843498328099741L;
@Override
public void configure(WebSocketServletFactory factory) {
factory.register(ToUpperWebSocket.class);
}
}
而這一次
@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) {
}
}
}
}
}
整個客戶端的東西的作品。所有的html,css和Javascript文件,但是這一行。 當我使用maven運行項目時,eclipse控制檯中也沒有錯誤消息。
只有當我在本地主機上運行項目時纔會發生此錯誤。它在服務器上工作。 爲了使其在本地主機上工作,我只是將URI從ws://theserver.com:port/toUpper更改爲ws:// localhost:8080/toUpper 但這顯然沒有幫助。
這裏,如果它可以幫助我的pom.xml:
<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>
我希望一些幫助。 Regards, J.
編輯: 項目仍在服務器上。 .class文件不是由我編譯的。它們是由別人用命令行使用javac編譯的。 當我在本地主機上運行我的項目時使用URI ws://serveraddress.com:1234/toUpper作爲websocket,它似乎工作。
你爲什麼要把碼頭依賴關係放在你的war-WEB-INF/lib中? –
@JoakimErdfelt從哪裏得到?代碼中的大部分來自我手指中的代碼。對不起,我真的沒有得到你想說的話。這樣做是錯誤的嗎? – JWo
不是說它的錯誤,它可能是有效的,取決於你想如何運行你的WAR。 org.eclipse.jetty的''聲明。*'groupIds應該可能有'提供 '(這是典型的設置) –