如何将 WebSocketContainer 实现为 StandardWebSocketClient class

How to implement WebSocketContainer into StandardWebSocketClient class

我正在尝试通过 Spring 4.2 WebsocketClient support 连接到 SockJs 网络套接字服务器。到目前为止,这是我的客户:

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);
}

这将提供我订阅频道所需的连接。 当我 运行 代码时,出现以下异常:

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)

我知道我需要为 META-INF 文件夹中的项目提供一个 WebSocketContainer,正如失败方法的评论所指示的那样:

* 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

但我不明白如何在不带任何参数的情况下将文件连接到构造函数中。我试图按照 test case 中的示例进行操作,但没有任何运气。我只需要了解容器到底是什么以及如何将其实现到我的项目中

Javax websocket 仅提供规范。它不提供实现。要获得完整实施,您可以使用 tyrus-standalone client.

如果您使用的是 Maven,请添加此依赖项:

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