如何将WebSocketContainer实现到StandardWebSocketClient类中

时间:2015-07-09 11:10:39

标签: java websocket spring-websocket

我正在尝试通过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中的例子,但没有任何运气。我只需要了解容器究竟是什么以及如何将其实现到我的项目中

1 个答案:

答案 0 :(得分:6)

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

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

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