【发布时间】:2021-10-28 02:43:34
【问题描述】:
我正在尝试使用 Java EE 创建一个简单的 Web 套接字服务器。我想创建一个简单的聊天应用程序,但我收到了这个错误,说 WebSocket connection to 'ws://localhost:8080/chat/ws' failed: 而没有别的。
这是我的pom.xml:
<?xml version="1.0" encoding="UTF-8"?>
<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 https://maven.apache.org/xsd/maven-4.0.0.xsd">
<modelVersion>4.0.0</modelVersion>
<groupId>com</groupId>
<artifactId>chat-app</artifactId>
<version>1.0-SNAPSHOT</version>
<name>chat-app</name>
<packaging>war</packaging>
<properties>
<project.build.sourceEncoding>UTF-8</project.build.sourceEncoding>
<maven.compiler.target>1.8</maven.compiler.target>
<maven.compiler.source>1.8</maven.compiler.source>
</properties>
<dependencies>
<dependency>
<groupId>javax</groupId>
<artifactId>javaee-web-api</artifactId>
<version>8.0.1</version>
<scope>provided</scope>
</dependency>
</dependencies>
<build>
<finalName>chat</finalName>
<plugins>
<plugin>
<groupId>org.apache.maven.plugins</groupId>
<artifactId>maven-war-plugin</artifactId>
<version>3.3.1</version>
</plugin>
</plugins>
</build>
</project>
这里是ChatServer.java:
@ServerEndpoint("/ws")
public class ChatServer {
@OnOpen
public void open(Session session) throws IOException, EncodeException {
session.getBasicRemote().sendText("Welcome");
}
}
这是我尝试连接到网络套接字的方式:
const ws = new WebSocket("ws://localhost:8080/chat/ws");
我实际上正在关注使用 Glassfish 4 的 this 教程。另外,我不知道这是否与它有关,但我正在使用 IntelliJ。
有什么想法吗?
附:也试过Tomcat,结果一样。问题一定出在 IntelliJ 上。
【问题讨论】:
-
标题说 Java 11,但你的 POM 说 Java 8。
标签: java maven jakarta-ee websocket glassfish