Jason
Jason

Reputation: 13986

Tomcat7 Websocket Chat example: How to load Websocket Server?

I'm trying to make my own chat client using websockets and thought I would start out with the Tomcat 7 websocket chat example code.. I have successfully compiled and deployed the ChatAnnotation class with Eclipse, but when I redirect the chat.html file to what I think the endpoint should be, nothing happens.

In the sample class, they have a @ServerEndpoint(value = "/websocket/chat") annotation and I'm not sure what I need to do (i.e. in the web.xml) to get that class to load on server start up and have that endpoint bound.

I have put breakpoints in constructor to see if it's getting created when I deploy and it's not.

Upvotes: 4

Views: 5024

Answers (2)

Destroyica
Destroyica

Reputation: 4267

Here is the "little magic":

You must have an implementation of the interface ServerApplicationConfig in your classpath:

Applications may provide an implementation of this interface to filter the discovered WebSocket endpoints that are deployed. Implementations of this class will be discovered via an ServletContainerInitializer scan.

If you use the Tomcat sample, a class exists in {CATALINA_BASE}\webapps\examples\WEB-INF\classes\websocket\ExamplesConfig.java (comes with compiled .class)


Last but not least: as Tomcat discovers things, you don't have to register any servlet in your WEB-INF/web.xml. This file must exist, but with minimal stuffs.

Upvotes: 4

Kevin
Kevin

Reputation: 11672

You don't need any special config in web.xml. Are you running it on the right version of Tomcat locally? This annotation only works on Tomcat 7.0.47. Also, if you're using Maven, this is the dependency I used - it specified the websocket-api so code would compile, but assumed that the necessary lib would be provided by the runtime environment. You'll find websocket-api.jar & tomcat7-websocket.jar in tomcat-7.0.47/lib

    <dependency>
        <groupId>javax.websocket</groupId>
        <artifactId>javax.websocket-api</artifactId>
        <version>1.0</version>
        <scope>provided</scope>
    </dependency>

Upvotes: 0

Related Questions