gpt4 book ai didi

java - 编程式 ServerEndpoint 不能在嵌入式 Tomcat 中工作

转载 作者:行者123 更新时间:2023-11-28 23:13:43 26 4
gpt4 key购买 nike

我有一个嵌入式 tomcat 正在运行,我正在尝试以编程方式设置 websockets,不能使用注释,因为我动态获取上下文路径列表。使用 Java 8 和 Tomcat 7。

下面是我使用的代码,

嵌入式 Tomcat,

public class EmbeddedTomcat {
/**
* @param args
* @throws LifecycleException
*/
public static void main(String[] args) throws LifecycleException {
Tomcat tomcat = new Tomcat();
tomcat.setPort(5555);

Context ctx = tomcat.addContext("", new File(".").getAbsolutePath());

Tomcat.addServlet(ctx, "hello", new HttpServlet() {
@Override
protected void service(HttpServletRequest req, HttpServletResponse resp)
throws ServletException, IOException {
Writer w = resp.getWriter();
w.write("Hello World !!");
w.flush();
w.close();
}
});

ctx.addServletMapping("/hello", "hello");

ctx.addApplicationListener(WSContextListener.class.getName());

tomcat.start();
tomcat.getServer().await();
}

用于动态添加端点的 WebSocket 监听器,

public class WSContextListener extends WsContextListener {

@Override
public void contextInitialized(ServletContextEvent sce) {
super.contextInitialized(sce);

ServerContainer sc =
(ServerContainer) sce.getServletContext().getAttribute(
Constants.SERVER_CONTAINER_SERVLET_CONTEXT_ATTRIBUTE);
try {
ServerEndpointConfig endPointConfig = ServerEndpointConfig.Builder
.create(WSEndpoint.class, "/wshello")
.build();
sc.addEndpoint(endPointConfig);
} catch(DeploymentException de) {
de.printStackTrace();
}
}

实际的服务器端点类,

public class WSEndpoint extends Endpoint {

/* (non-Javadoc)
* @see javax.websocket.Endpoint#onOpen(javax.websocket.Session, javax.websocket.EndpointConfig)
*/
@Override
public void onOpen(Session session, EndpointConfig endpointConfig) {
System.out.println(String.format("Opened a new session with Id[%s] associated to endpoint[%s]", session.getId(), session.getRequestURI().getPath()));

session.addMessageHandler(new MessageHandler.Whole<String>() {
@Override
public void onMessage(String data) {
System.out.println("Data received - " + data);
session.getAsyncRemote().sendText(data);
}
});
}

@Override
public void onClose(Session session, CloseReason closeReason) {
System.out.println(String.format("Closing the connection to endpoint[%s] for session Id[%s] ", session.getRequestURI().getPath(), session.getId()));
super.onClose(session, closeReason);
}

@Override
public void onError(Session session, Throwable throwable) {
System.out.println(String.format("Error [%s] occurred on session Id[%s] associated to endpoint[%s]", throwable.getMessage(), session.getId(), session.getRequestURI().getPath()));
super.onError(session, throwable);
}

最后,连接到 Websocket 的 javascript 位,

var webSocket = new WebSocket("ws://localhost:5555/wshello");

我可以访问 servlet ( http://localhost:5555/hello ) 并且该位有效。当我尝试通过上面的 javascript 代码访问 websocket 时,它失败了,

websocket_test.html:18 WebSocket connection to 'ws://localhost:5555/wshello' failed: Error during WebSocket handshake: Unexpected response code: 404

最佳答案

试试这个

var webSocket = new WebSocket("ws://localhost:5555/hello", "protocol");

在客户端添加用于处理 websocket 的协议(protocol)(可选)。删除 wshello "ws"

关于java - 编程式 ServerEndpoint 不能在嵌入式 Tomcat 中工作,我们在Stack Overflow上找到一个类似的问题: https://stackoverflow.com/questions/53475084/

26 4 0
Copyright 2021 - 2024 cfsdn All Rights Reserved 蜀ICP备2022000587号
广告合作:1813099741@qq.com 6ren.com