gpt4 book ai didi

websocket - 嵌入式Undertown同时部署websocket和servlet,不行

转载 作者:行者123 更新时间:2023-12-04 01:54:38 27 4
gpt4 key购买 nike

我是使用 undertow 的新手,我正在开发一个独立的应用程序,它将用作嵌入式服务器。我希望我可以在我的嵌入式 undertow 中部署 web sockets、servlets 和 restfull htmls 服务。

到目前为止,我只使用 web sockets 和 servlet 完成了测试,每个单独的功能正常的问题,但是将它们部署在一起 web sockets 不是我从带有 HTML 和 JavaScript 的测试页面访问,如果我删除 servlet 页面 nigun 测试运行没有错误。

这是我的代码:

    /*
* Create the appWebSocketDeploymentInfo and configure
*/
WebSocketDeploymentInfo appWebSocketDeploymentInfo = new WebSocketDeploymentInfo();
appWebSocketDeploymentInfo.setBuffers(new ByteBufferSlicePool(BufferAllocator.BYTE_BUFFER_ALLOCATOR, 17000, 17000 * 16));
appWebSocketDeploymentInfo.addEndpoint(WebSocketEndpoint1.class);
appWebSocketDeploymentInfo.addEndpoint(WebSocketEndpoint2.class);

/*
* Create the appDeploymentInfo and configure
*/
DeploymentInfo appDeploymentInfo = Servlets.deployment()
.setClassLoader(Server.class.getClassLoader())
.setContextPath("/myapp)
.setDeploymentName("app.war")
.setResourceManager(new FileResourceManager(new File("src/main/webapp"), 1024))
.addServlets(Servlets.servlet("HomeServlet", HomeServlet.class).addMapping("/home"))
.setResourceManager(new ClassPathResourceManager(Server.class.getClassLoader(), Server.class.getPackage()))
.addServletContextAttribute(WebSocketDeploymentInfo.ATTRIBUTE_NAME, appWebSocketDeploymentInfo);

/*
* Create the deploymentManager
*/
deploymentManager = Servlets.defaultContainer().addDeployment(appDeploymentInfo);

/*
* Deploy the app
*/
deploymentManager.deploy();

/*
* Create the path handle
*/
pathHandler = Handlers.path(Handlers.redirect("/myapp/home")).addPrefixPath("/myapp", deploymentManager.start());

/*
* Create the server
*/
undertowServer = Undertow.builder().addHttpListener(DEFAULT_PORT, DEFAULT_IP).setHandler(pathHandler).build();

javascript日志错误是

WebSocket 连接到“ws://localhost:8080/fermat/node-channel”失败:WebSocket 握手期间出错:意外响应代码:404

最佳答案

经过广泛的测试和研究,我得到了正确的方法来进行配置和启动服务器,还将添加对其他技术的支持,例如:

  • WebSocket
  • 安逸
  • 焊接

  • 我的最终代码 :
    UndertowJaxrsServer server = new UndertowJaxrsServer();
    Undertow.Builder serverBuilder = Undertow.builder().addHttpListener(DEFAULT_PORT, DEFAULT_IP);
    ServletContainer servletContainer = Servlets.defaultContainer();

    /*
    * Create the App WebSocketDeploymentInfo and configure
    */
    WebSocketDeploymentInfo appWebSocketDeploymentInfo = new WebSocketDeploymentInfo();
    appWebSocketDeploymentInfo.setBuffers(new ByteBufferSlicePool(BufferAllocator.BYTE_BUFFER_ALLOCATOR, 17000, 17000 * 16));
    appWebSocketDeploymentInfo.addEndpoint(WebSocketNodeChannelServerEndpoint.class);
    appWebSocketDeploymentInfo.addEndpoint(WebSocketClientChannelServerEndpoint.class);

    /*
    * Create the App ResteasyDeployment and configure
    */
    ResteasyDeployment deployment = new ResteasyDeployment();
    deployment.setApplicationClass(JaxRsActivator.class.getName());
    deployment.setInjectorFactoryClass("org.jboss.resteasy.cdi.CdiInjectorFactory");

    /*
    * Create the App DeploymentInfo and configure
    */
    DeploymentInfo appDeploymentInfo = server.undertowDeployment(deployment, APP_NAME);
    appDeploymentInfo.setClassLoader(FermatEmbeddedNodeServer.class.getClassLoader())
    .setContextPath(APP_NAME)
    .setDeploymentName(WAR_APP_NAME)
    .addServletContextAttribute(WebSocketDeploymentInfo.ATTRIBUTE_NAME, appWebSocketDeploymentInfo)
    .addServlets(Servlets.servlet("HomeServlet", HomeServlet.class).addMapping("/home"))
    .addListeners(Servlets.listener(org.jboss.weld.environment.servlet.Listener.class));

    server.deploy(appDeploymentInfo);
    server.start(serverBuilder);

    Gradle 依赖项
    compile 'io.undertow:undertow-core:1.3.6.Final'
    compile 'io.undertow:undertow-servlet:1.3.6.Final'
    compile 'io.undertow:undertow-websockets-jsr:1.3.6.Final'

    compile 'org.jboss.resteasy:resteasy-undertow:3.0.13.Final'
    compile 'org.jboss.resteasy:resteasy-cdi:3.0.13.Final'
    compile 'org.jboss.weld.servlet:weld-servlet:2.3.1.Final'

    compile 'javax:javaee-api:7.0'

    关于websocket - 嵌入式Undertown同时部署websocket和servlet,不行,我们在Stack Overflow上找到一个类似的问题: https://stackoverflow.com/questions/33698332/

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