I'm trying to adopt old code that uses (now deprecated) WebSocketServlet. The old code looks like this:
@Singleton
ExampleServlet extends WebSocketServlet {
@Override
protected StreamInbound createWebSocketInbound(String subProtocol, HttpServletRequest request) {
// Do something
// ...
return // StreamInbound impl;
}
}
As I said in the newer version of the tomcat 7 there is WebSocket implementation backported from tomcat 8 (WebSocket 1.0, Tyrus) and class WebSocketServlet is deprecated.
What I should use instead to deploy my servlet with newer API?
With regards to KIC's answer - it is not all what is needed.
Since JSR356 should be used with Tomcat 8.*, you need to modify your class from:
class MyWebSocket extends WebSocketServlet
to:
import javax.websocket.server.ServerEndpoint;
@ServerEndpoint(value="/your-websocket-endpoint")
class MyWebSocket
Then, since StreamInbound is also deprecated in Tomcat 8.*, you need to write own methods to handle open, close, error and message events:
import javax.websocket.OnClose;
import javax.websocket.OnError;
import javax.websocket.OnMessage;
import javax.websocket.OnOpen;
import javax.websocket.Session;
@OnOpen
void onOpen(Session session) { }
@OnClose
void onClose(Session session) { }
@OnMessage
void onMessage(Session session, String message) { }
@OnError
void onError(Session session, Throwable throwable) { }
Finally, Tomcat provides javax.websocket.* classes already and it should not be included with your application. For Maven dependency is:
<dependency>
<groupId>javax.websocket</groupId>
<artifactId>javax.websocket-api</artifactId>
<version>1.0</version>
<scope>provided</scope>
</dependency>
and for Gradle is:
providedCompile 'javax.websocket:javax.websocket-api:1.0'
Please see related post Tomcat 8 and Websocket.
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