我已经在互联网上搜索过,但到目前为止,Jakarta 存储库的所有文档均不成功。实际上没有任何关于如何部署端点的内容。显然,除非部署,否则端点只是一个无用的文件。下面是 Websocket 服务器端点。
package com.someApp;
import java.io.IOException;
import jakarta.websocket.*;
import jakarta.websocket.server.ServerEndpoint;
@ServerEndpoint("/ws")
public class WebSocketEndpoint {
@OnOpen
public void onOpen(Session session) {
System.out.println("WebSocket opened: " + session.getId());
}
@OnMessage
public void onMessage(String message, Session session) {
System.out.println("Message received: " + message);
try {
session.getBasicRemote().sendText("Echo: " + message);
} catch (IOException e) {
e.printStackTrace();
}
}
@OnClose
public void onClose(Session session, CloseReason closeReason) {
System.out.println("WebSocket closed: " + session.getId());
}
@OnError
public void onError(Session session, Throwable throwable) {
System.out.println("WebSocket error: " + session.getId());
throwable.printStackTrace();
}
}
这是main.java文件
package com.someApp;
import jakarta.websocket.ContainerProvider;
import jakarta.websocket.OnClose;
import jakarta.websocket.OnMessage;
import jakarta.websocket.OnClose;
import jakarta.websocket.OnError;
import jakarta.websocket.Session;
import jakarta.websocket.WebSocketContainer;
import jakarta.websocket.server.ServerEndpoint;
import jakarta.websocket.server.ServerEndpointConfig;
import java.net.URI;
import java.net.http.WebSocket;
@ServerEndpoint("/ws")
public class Main {
public static void main(String[] args) throws Exception {
ServerEndpointConfig serverEndpoint = ServerEndpointConfig.Builder.create(WebsocketEndpoint.class, "/pathToRoot/").build();
}
}
Main.java 似乎成功构建了 websocket,但我如何监听事件?任何见解都会很棒。谢谢。
如果您正在寻找符合 Jakarta EE 的示例代码,请查看以下位置:
https://jakarta.ee/learn/docs/jakartaee-tutorial/current/intro/usingexamples/usingexamples.html
教程示例代码位于 https://github.com/eclipse-ee4j/jakartaee-examples
https://www.wildfly.org/downloads/
34.0.1.Final 或 35.0.0.Final ,找到 快速入门源代码 -> zip https://github.com/wildfly/quickstart/releases/download/35.0.0.Final/wildfly- 35.0.0.Final-quickstarts.zip
有关其他示例,请参阅 GlassFish 示例: https://github.com/eclipse-ee4j/glassfish-samples/tree/master/ws/jakartaee9
此示例项目是从以下项目修改而来的:
https://github.com/eclipse-ee4j/glassfish-samples/tree/master/ws/jakartaee9/websocket/echo
jakartaee10-websocket-echo
├── pom.xml
└── src
└── main
├── java
│ └── org
│ └── glassfish
│ └── samples
│ └── websocket
│ └── echo
│ └── EchoEndpoint.java
└── webapp
├── index.html
└── WEB-INF
└── glassfish-web.xml
<?xml version="1.0" encoding="UTF-8"?>
<project xmlns:xsi="http://www.w3.org/2001/XMLSchema-instance" xmlns="http://maven.apache.org/POM/4.0.0"
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>org.glassfish-samples</groupId>
<version>6.0-SNAPSHOT</version>
<artifactId>websocket-samples-echo</artifactId>
<packaging>war</packaging>
<name>WebSocket Echo Sample Application</name>
<properties>
<maven.compiler.source>17</maven.compiler.source>
<maven.compiler.target>17</maven.compiler.target>
<maven.compiler.encoding>UTF-8</maven.compiler.encoding>
<project.build.sourceEncoding>UTF-8</project.build.sourceEncoding>
<project.reporting.outputEncoding>UTF-8</project.reporting.outputEncoding>
</properties>
<dependencies>
<dependency>
<groupId>jakarta.platform</groupId>
<artifactId>jakarta.jakartaee-api</artifactId>
<version>10.0.0</version>
<scope>provided</scope>
</dependency>
</dependencies>
<build>
<finalName>websocket-echo</finalName>
<plugins>
<plugin>
<groupId>org.apache.maven.plugins</groupId>
<artifactId>maven-war-plugin</artifactId>
<configuration>
<failOnMissingWebXml>false</failOnMissingWebXml>
<warName>websocket-echo</warName>
</configuration>
</plugin>
</plugins>
</build>
</project>
将代码复制到此类中。
package org.glassfish.samples.websocket.echo;
import jakarta.websocket.*;
import jakarta.websocket.server.ServerEndpoint;
import java.io.IOException;
/**
* Echo endpoint.
*/
@ServerEndpoint("/echo")
public class EchoEndpoint {
@OnOpen
public void onOpen(Session session) {
System.out.println("WebSocket opened: " + session.getId());
}
/**
* Incoming message is represented as parameter and return value is going to be send back to peer.
* </p>
* {@link jakarta.websocket.Session} can be put as a parameter and then you can gain better control of whole
* communication, like sending more than one message, process path parameters, {@link jakarta.websocket.Extension Extensions}
* and sub-protocols and much more.
*
* @param message incoming text message.
* @return outgoing message.
* @see OnMessage
* @see jakarta.websocket.Session
*/
/*
@OnMessage
public String echo(String message) {
return message;
}
*/
@OnMessage
public void onMessage(String message, Session session) {
System.out.println("Message received: " + message);
try {
session.getBasicRemote().sendText("Echo: " + message);
} catch (IOException e) {
e.printStackTrace();
}
}
@OnClose
public void onClose(Session session, CloseReason closeReason) {
System.out.println("WebSocket closed: " + session.getId());
}
@OnError
public void onError(Session session, Throwable throwable) {
System.out.println("WebSocket error: " + session.getId());
throwable.printStackTrace();
}
}
mvn clean package
输出文件:
websocket-echo.war
jakartaee10-websocket-echo/target/websocket-echo.war
将
websocket-echo.war
放入 apache-tomcat-10.1.24/webapps
在Tomcat目录下
cd bin
./startup.sh
http://localhost:8080/websocket-echo/
apache-tomcat-10.1.24/logs/catalina.out
您的代码
System.out.println
输出到catalina.out
WebSocket opened: 0
Message received: Hello World - WebSocket!
WebSocket opened: 1
Message received: Hello 1
WebSocket opened: 2
Message received: Hello 2
WebSocket closed: 0
WebSocket closed: 1
WebSocket closed: 2
如何连接到WebSocket
在此示例中,您的 Web 应用程序的网页位于
src/main/webapp/index.html
。
index.html
文件使用 JavaScript 连接到您的 WebSocket。
<script language="javascript" type="text/javascript">
var wsUri = getRootUri() + "/websocket-echo/echo";
...