6

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

a.hrdie
  • 716
  • 2
  • 14
  • 35

1 Answers1

8

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>
Karthik
  • 4,950
  • 6
  • 35
  • 65
  • thanks. This is what I have been trying to achieve. I have added the dependancy to my classpath, but it is still throwing the exception. Do i need to extend a class and override or pass the Container class into the method somehow? – a.hrdie Jul 09 '15 at 12:04
  • @a.hrdie you don't need to, are you getting the same exception? – Karthik Jul 09 '15 at 12:19
  • yes, the same exception. I think I have misunderstood the configuration of the client. Should this project be contained in a web servlet ? – a.hrdie Jul 09 '15 at 12:30
  • @a.hrdie can you check if your downloaded jar has full class name in `META-INF/services/javax.websocket.ContainerProvider`? – Karthik Jul 09 '15 at 12:30
  • i added the maven dependancy and was receiving the same error. i have now downloaded org.glassfish.tyrus.bundles » websocket-ri-bundle and added to the classpath and it is working! Thank you – a.hrdie Jul 09 '15 at 12:34
  • @a.hrdie glad that It helped :) – Karthik Jul 09 '15 at 12:39
  • 1
    I have the same issue, added the dependency and still have the same error. Could you detail a bit more how you fixed it @a.hrdie? I don't really understand what you did there. I added the library called websocket-ri-bundle and it's not fixed: https://i.imgur.com/NEzozxi.png – afarre Jan 15 '21 at 13:09