私はWebアプリケーションを作成する必要があるプロジェクトに取り組んでいます。このWebアプリケーションは、私たちの大学に属するサーバー上でJettyとともに動作します。JavaScript WebSocket:ローカルホストに接続する
私はサーバー上で動作するこのサーバーからのバックアップを持っています。しかし、私はjetty mavenプラグインを使用してEclipseで作業したかったのです。そして今、プロジェクトは私のIDEで動作しません。
私が得る唯一のメッセージは次のとおりです。
Firefox kann keine Verbindung zu dem Server unter ws://localhost:8080/toUpper/ aufbauen. game.js:837:13
に換算さ: FirefoxはサーバWSへの接続を確立することはできません:// localhostを:8080/TOUPPER/
特定の行( 837行)は、次のように述べています。 webSocket = new WebSocket("ws://localhost:8080/toUpper/");
この呼び出しには2つのクラスがあります。
@WebServlet(urlPatterns = "/toUpper")
public class ToUpperWebSocketServlet extends WebSocketServlet {
/**
*
*/
private static final long serialVersionUID = 9106843498328099741L;
@Override
public void configure(WebSocketServletFactory factory) {
factory.register(ToUpperWebSocket.class);
}
}
この1
@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ファイルですが、この1行です。 プロジェクトをmavenで実行すると、Eclipseコンソールにエラーメッセージも表示されません。
このエラーは、localhostでプロジェクトを実行した場合にのみ発生します。サーバー上で動作します。 localhost上で動作させるために、私は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>
私はいくつかの助けを期待しています。 よろしくお願いします。 J.
EDIT: プロジェクトはまだサーバー上にあります。 .classファイルは私によってコンパイルされませんでした。 javacを使ってコマンドラインで他の人がコンパイルしました。 localhostでプロジェクトを実行するときにwebsocketのURI ws://serveraddress.com:1234/toUpperを使用すると、動作するようです。
なぜあなたの戦争の 'WEB-INF/lib'に桟橋の依存関係を置いていますか? –
@JoakimErdfeltどこからそれを得るのですか?私が指でそれを手に入れる前に、コードのほとんどが存在していました。申し訳ありませんが、私は本当にあなたがそのことで何を言いたいのか分かりません。そんなことをするのは間違いでしょうか? – JWo
あなたのWARをどのように実行したいかによって、間違いなく、有効になる可能性があります。 'org.eclipse.jetty 'のための' '宣言。* 'groupIdsはおそらく' '(これは典型的な設定です) –