在web.xml文件中映射WebSocketEndpoints [英] Mapping WebSocketEndpoints in a web.xml file

查看:1043
本文介绍了在web.xml文件中映射WebSocketEndpoints的处理方法,对大家解决问题具有一定的参考价值,需要的朋友们下面随着小编来一起学习吧!

问题描述

我正在尝试开发一个使用websocket端点的Java EE 7 Web应用程序,并将其部署在Jetty服务器上。

I am trying to develop a Java EE 7 web application that uses a websocket endpoint and deploy it on a Jetty server.

该应用程序具有以下结构: / p>

The application has the following structure:

Game/
  src/
    main/
      java/
        game/
          WebSocketEndpoint.java
      webapp/
        index.html
        scripts/
          variousjavascriptstuff.js
        WEB-INF/
          beans.xml
          web.xml

在beans.xml文件中:

In the beans.xml file:

<beans xmlns="http://xmlns.jcp.org/xml/ns/javaee"
   xmlns:xsi="http://www.w3.org/2001/XMLSchema-instance"
   xsi:schemaLocation="http://xmlns.jcp.org/xml/ns/javaee http://xmlns.jcp.org/xml/ns/javaee/beans_1_1.xsd"
   bean-discovery-mode="annotated">

WebSocketEndpoint已正确注释并可正常工作但是,对于Netbeans / Glassfish4,应用程序必须部署在Jetty服务器上。

WebSocketEndpoint is annotated properly and works fine with Netbeans/Glassfish4, however, the application must be deployed on a Jetty server.

所以,我的问题 - 如何将websocket端点映射到web.xml文件中的URL /游戏?我已经找到了一些映射servlet的例子,但我认为这不适用于服务器端点。

So, my question - How do I map the websocket endpoint to the URL /game in the web.xml file? I have found a number of examples for mapping servlets, but I do not think that this will work for a server endpoint.

或者,有没有办法写一个web Jetty的.xml文件,以便它自动发现ll带注释的类/方法(类似于上面的beans.xml)

Or, is there a way to write a web.xml file for Jetty so that it automatically discovers ll annotated classes/methods (similar to the above beans.xml)

推荐答案

假设你使用JSR-356技术注释 game.WebSocketEndpoint ...

Assuming you have annotated game.WebSocketEndpoint using the JSR-356 techniques ...

示例:

package game;

import javax.websocket.server.ServerEndpoint

@ServerEndpoint("/game")
public class WebSocketEndpoint {

}

然后您必须执行以下操作...

Then you have to do the following...


  1. 使用 Jetty 9.1+

  2. 启用'websocket'模块(添加 - module = websocket 到您的 start.ini 或命令行)

  1. Use Jetty 9.1+
  2. Enable the 'websocket' module (add --module=websocket to your start.ini or command line)

这将启用websocket服务器类+对websocket端点的注释扫描。

That will enable the websocket server classes + annotation scanning for websocket endpoints.

注意:JSR-356并不意味着通过部署描述符进行映射( web.xml )。

Note: that JSR-356 isn't meant to be mapped via the deployment descriptor (web.xml).

但是,您可以使用以下技术之一以编程方式映射端点:

However, you can programmatically map your endpoints using one of the following techniques:


  1. 创建一个手动添加端点的 javax.servlet.ServletContextListener s通过 javax.websocket.server.ServerContainer (参见下面的方法)

  2. 创建 javax。 servlet.ServerContainerInitializer 通过 javax.websocket.server.ServerContainer 手动添加端点(参见下面的方法)

  3. 创建一个 javax.websocket.server.ServerAppliationConfig ,它返回您要添加的端点。

  1. Create a javax.servlet.ServletContextListener that manually adds endpoints via the javax.websocket.server.ServerContainer (see below for how)
  2. Create a javax.servlet.ServerContainerInitializer that manually adds endpoints via the javax.websocket.server.ServerContainer (see below for how)
  3. Create a javax.websocket.server.ServerAppliationConfig that returns the endpoints that you want to add.

注意:技术#2和#3都需要类扫描注释(慢启动)。技术#1是快速启动。

Note: technique #2 and #3 both require class scanning for annotations (slow startup). technique #1 is fast startup.

如何手动添加端点

// Get a reference to the ServerContainer
javax.websocket.server.ServerContainer ServerContainer =
  (javax.websocket.server.ServerContainer)
  servletContext.getAttribute("javax.websocket.server.ServerContainer");
// Add endpoint manually to server container
serverContainer.addEndpoint(game.WebSocketEndpoint.class);

这篇关于在web.xml文件中映射WebSocketEndpoints的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持IT屋!

查看全文
登录 关闭
扫码关注1秒登录
发送“验证码”获取 | 15天全站免登陆