Last edited: 01 March 2018
The war file can be downloaded here, ready for dropping into your servlet container's web apps directory. It contains the Java source code and an Ant build file. Once deployed, you can access the client page at http://localhost:8080/websocket
The Java source code consists of 3 files:
- Intercepts an incoming request and ensures that an HTTP session is created
- Stores a reference to the HTTP session in the user properties
- The actual WS endpoint, contained in the open, message, close and error methods. The message method reads the client message and generates the response from that.
Of these, only SocketEndPoint is really required for a basic setup. The others are scaffolding to allow access to the user session from which the servlet context can be obtained, used here for logging purposes. Generally, an HTTP session is not required, and should not be created unless necessary.
Server-Sent Events (SSE)
It's been tested with Tomcat 8.5 on Java 8 (which is the minimum Java version supported for JAX-RS 2.1). Note that the example in the documentation makes it seem as if the name of the message ("message-to-client") can contain hyphens - that does not work with current browsers. But that example code has in any case not been updated to the released version, so you're better off just using the source code that comes with this web app.
The war file can be downloaded here, ready for dropping into your servlet container's web apps directory. It contains the Java source code and an Ant build file. Once deployed, you can access the client page at http://localhost:8080/sse
The Java source code consists of 2 files:
- A source of messages intended for sending to the client. In this case a thread creates a series of 10 messages over 10 seconds.
- This class creates the actual events from the messages creates by SSE and sends them to the web page.