Jakarta ee 如何在JavaEE7中运行WebSocket服务器
我是JavaEE新手,正在尝试构建WebSocket服务器。到目前为止,我有以下课程:Jakarta ee 如何在JavaEE7中运行WebSocket服务器,jakarta-ee,java-ee-7,java-websocket,Jakarta Ee,Java Ee 7,Java Websocket,我是JavaEE新手,正在尝试构建WebSocket服务器。到目前为止,我有以下课程: import java.io.IOException; import javax.websocket.OnClose; import javax.websocket.OnMessage; import javax.websocket.OnOpen; import javax.websocket.Session; import javax.websocket.server.ServerEndpoint;
import java.io.IOException;
import javax.websocket.OnClose;
import javax.websocket.OnMessage;
import javax.websocket.OnOpen;
import javax.websocket.Session;
import javax.websocket.server.ServerEndpoint;
/**
* @ServerEndpoint gives the relative name for the end point
* This will be accessed via ws://localhost:8080/EchoChamber/echo
* Where "localhost" is the address of the host,
* "EchoChamber" is the name of the package
* and "echo" is the address to access this class from the server
*/
@ServerEndpoint("/echo")
public class EchoServer {
/**
* @OnOpen allows us to intercept the creation of a new session.
* The session class allows us to send data to the user.
* In the method onOpen, we'll let the user know that the handshake was
* successful.
*/
@OnOpen
public void onOpen(Session session){
System.out.println(session.getId() + " has opened a connection");
try {
session.getBasicRemote().sendText("Connection Established");
} catch (IOException ex) {
ex.printStackTrace();
}
}
/**
* When a user sends a message to the server, this method will intercept the message
* and allow us to react to it. For now the message is read as a String.
*/
@OnMessage
public void onMessage(String message, Session session){
System.out.println("Message from " + session.getId() + ": " + message);
try {
session.getBasicRemote().sendText(message);
} catch (IOException ex) {
ex.printStackTrace();
}
}
/**
* The user closes the connection.
*
* Note: you can't send messages to the client from this method
*/
@OnClose
public void onClose(Session session){
System.out.println("Session " +session.getId()+" has ended");
}
public static void main(String[] args) throws Exception {
}
}
我不知道也找不到任何示例来演示如何运行此服务器。任何帮助都将不胜感激。谢谢。你需要一个容器,比如野蝇或玻璃鱼。@CássioMazzochiMolin你有没有关于如何使用这些容器的例子?@kg这是整本书的主题,不是一个简单的例子。JavaEE开发并不是一件简单的事情。从阅读官方资料(google:JavaEE7WebSockets)开始,你会发现你需要从那里进一步研究什么