views:

1827

answers:

3

I was taking a look at WebSockets last week and made a few thoughts on how to implement the server side with the Java Servlet API. I didn't spend too much time, but ran into the following problems during a few tests with Tomcat, which seem impossible to solve without patching the container or at least making container specific modifications to the HttpServletResponse implementation:

  • The WebSocket specification mandate a defined message in the 101 HTTP response. HttpServletResponse.setStatus(int code, String message) is deprecated without mentioning a usable replacement. After changing the default Tomcat configuration, I made Tomcat honor my message string, but since the method is deprecated, I'm not sure if this will work with other servlet containers.

  • The WebSocket specification require a specified order of the first few headers in the HTTP response to the connection upgrade request. The servlet API does not offer a method to specify the order of the response headers and Tomcat adds its own headers to the response, placing a few of them before any headers, which are added by the servlet implementation.

  • Since the content length of the response is not known when committing the header, Tomcat automatically switches to chunked transfer encoding for the response, which is incompatible with the WebSocket specification.

Am I missing something obvious, or is it really not possible to integrate WebSocket server endpoints in a servlet based web app?

+1  A: 

The javadoc for void setStatus(int sc, String sm) states why it is deprecated, and what the replacement is:

As of version 2.1, due to ambiguous meaning of the message parameter. To set a status code use setStatus(int), to send an error with a description use sendError(int, String). Sets the status code and message for this response.

skaffman
The problem is, that I can't send an error, since it will close the connection and not allow me to use the response's OutputStream to send content. setStatus would allow me to set the status code and message string for the response and still keep the response open for content.
jarnbjo
A: 

Yes there is a very good one (open source and completely free): http://www.jWebSocket.org

DrCoPe
No, jWebSocket is not servlet based.
jarnbjo
It IS Java based and there is, among many different cases, a WebApp approach ready for download! What is the difference? It works, it's Java, there you go. How do you imagine a servlet based Socket connection anyway?And by the way, as I have heard, the jWebSocket team has been contacted by the Tomcat team about the possibility of including their code in Tomcat... (consider this as just a rumor)
DrCoPe
+1  A: 

There is an implementation in Jetty. We can hope that tomcat and jetty find a compatible API.

Horcrux7
I know, but I was more or less hoping that WebSocket support should be possible using the standard Servlet API and not by using any container specific extensions.
jarnbjo
I understand. Then only the coming Servlet 3.0 spec is a solution. Currently the spec is a beta (I think). But there are already some implementations of the spec. Of course in beta.
Horcrux7