How to implement WebSocketContainer into StandardWebSocketClient class

a.hrdie picture a.hrdie · Jul 9, 2015 · Viewed 7k times · Source

I am attempting to connect to a SockJs web sockets server via Spring 4.2 WebsocketClient support. This is my client so far:

public static void main(String[] args) throws Exception {
    WebSocketClient transport = new StandardWebSocketClient();
    WebSocketStompClient stompClient = new WebSocketStompClient(transport);
    stompClient.setMessageConverter(new StringMessageConverter());

    String url = "ws://localhost:8080/priceticker/ws";
    StompSessionHandler handler = new WSClient() ;
    stompClient.connect(url, handler);
}

this will provide the connection I need to subscribe to my channel. When I run the code, I get the following exception:

Exception in thread "main" java.lang.RuntimeException: Could not find an implementation class.
at javax.websocket.ContainerProvider.getWebSocketContainer(ContainerProvider.java:73)
at org.springframework.web.socket.client.standard.StandardWebSocketClient.<init>(StandardWebSocketClient.java:76)
at Main.main(Main.java:10)

I understand I need to provide a WebSocketContainer to the project in the META-INF folder, as instructed by the comments on the failing method:

* Obtain a new instance of a WebSocketContainer. The method looks for the
* ContainerProvider implementation class in the order listed in the META-INF/services/javax.websocket.ContainerProvider 
* file, returning the WebSocketContainer implementation from the ContainerProvider implementation

But I do not understand how to wire a file into the constructor without any arguments. I have attempted to follow the examples in this test case but without any luck. I just need to understand what exactly the container is and how to implement it into my project

Answer

Karthik picture Karthik · Jul 9, 2015

Javax websocket only provides specification. It does not provide the implementation. To get full implementation you can use tyrus-standalone client.

If you are using Maven, add this dependency :

<dependency>
     <groupId>org.glassfish.tyrus.bundles</groupId>
     <artifactId>tyrus-standalone-client</artifactId>
     <version>1.9</version>
</dependency>