Logo Questions Linux Laravel Mysql Ubuntu Git Menu
 

Mapping WebSocketEndpoints in a web.xml file

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)

like image 625
Kyle Avatar asked Nov 21 '13 17:11

Kyle


People also ask

What are WebSocket endpoints?

In simple terms, a WebSocket server endpoint is a web address (URL) at which clients of a specific service can gain access to it. By referencing that URL, WebSocket clients can get to operations provided by that service. The WebSocket endpoint contains callback services for both server and client.

How do you implement WebSockets?

How Websockets are implemented? webSockets are implemented as follows: Client makes HTTP request to server with "upgrade" header on the request. If server agrees to the upgrade, then client and server exchange some security credentials and the protocol on the existing TCP socket is switched from HTTP to webSocket.

How read data from WebSocket?

First, you need to copy your web browser's header to here and use json. dumps to convert it into the string format. After that, create the connection to the server by using create_connection . Then, perform the handshake by sending the message, and you will be able to see the data on your side.


1 Answers

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);
like image 125
Joakim Erdfelt Avatar answered Sep 29 '22 08:09

Joakim Erdfelt