Mapping WebSocketEndpoints in a web.xml file

怎甘沉沦 提交于 2019-12-10 14:38:58

问题


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

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

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 is annotated properly and works fine with Netbeans/Glassfish4, however, the application must be deployed on a Jetty server.

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.

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)


回答1:


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

Example:

package game;

import javax.websocket.server.ServerEndpoint

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

}

Then you have to do the following...

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

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

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. 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.

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

How to Manually Add Endpoints

// 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);


来源:https://stackoverflow.com/questions/20127800/mapping-websocketendpoints-in-a-web-xml-file

易学教程内所有资源均来自网络或用户发布的内容,如有违反法律规定的内容欢迎反馈
该文章没有解决你所遇到的问题?点击提问,说说你的问题,让更多的人一起探讨吧!