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)
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 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.
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.
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...
--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:
javax.servlet.ServletContextListener
that manually adds endpoints via the javax.websocket.server.ServerContainer
(see below for how)javax.servlet.ServerContainerInitializer
that manually adds endpoints via the javax.websocket.server.ServerContainer
(see below for how)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);
If you love us? You can donate to us via Paypal or buy me a coffee so we can maintain and grow! Thank you!
Donate Us With