java socket spring,如何正确实现spring-websocket java客户端

I am working on a Spring WebSocket Stomp Client for my WebSocket Server and I am getting conflicting information.

I have found 2 ways to get it to work and without going into too much detail I was wondering which way is considered the "correct" way of implementing the client.

Could somebody help me understand what the WebSocketConnectionManager is for?

Also, one more question, how do I keep the websocket connection open and the program running to accept new messages without having to write the line System.in.read().

1'st way: Using the SockJsClient directly

URI uri = new URI("ws://localhost:8080/stomp");

StandardWebSocketClient simpleWebSocketClient = new StandardWebSocketClient();

List transports = new ArrayList<>(1);

transports.add(new WebSocketTransport(simpleWebSocketClient));

SockJsClient sockJsClient = new SockJsClient(transports);

sockJsClient.setMessageCodec(new Jackson2SockJsMessageCodec());

StompMessageReceiver messageHandler = new StompMessageReceiver();

StompWebSocketHandler websocketHandler = new StompWebSocketHandler(messageHandler, new StringMessageConverter());

try {

this.webSocketClient.doHandshake(websocketHandler, null, uri).get();

} catch (InterruptedException | ExecutionException e) {

throw new IllegalStateException(e);

}

System.in.read();

2'nd way: Using the WebSocketConnectionManager

StandardWebSocketClient simpleWebSocketClient = new StandardWebSocketClient();

List transports = new ArrayList<>(1);

transports.add(new WebSocketTransport(simpleWebSocketClient));

SockJsClient sockJsClient = new SockJsClient(transports);

sockJsClient.setMessageCodec(new Jackson2SockJsMessageCodec());

StompMessageHandler messageHandler = new StompMessageHandler();

StompWebSocketHandler websocketHandler = new StompWebSocketHandler(messageHandler, new StringMessageConverter());

WebSocketConnectionManager manager = new WebSocketConnectionManager(sockJsClient, websocketHandler, "ws://localhost:8080/stomp");

manager.start();

System.in.read();

I know that I could make this a lot simpler by using the Annotations for @Configuration and @Bean but I am trying to do a "raw" implementation so I can understand how everything works together.

A little more information:

org.springframework

spring-websocket

4.1.4.RELEASE

org.springframework

spring-messaging

4.1.4.RELEASE

javax.websocket

javax.websocket-client-api

1.1

org.apache.tomcat

tomcat-websocket

8.0.0-RC10

com.fasterxml.jackson.core

jackson-databind

2.5.0

解决方案

If it is interesting Spring Integration provides an implementation for the WebSocketClient.

And yes, internally it uses ConnectionManagerSupport.

Here is a test-case which demonstrate how to configure it from @Configuration.

But I think you should try with out-of-the-box WebSocketHandler implementation - SubProtocolWebSocketHandler, and StompSubProtocolHandler.

  • 0
    点赞
  • 0
    收藏
    觉得还不错? 一键收藏
  • 0
    评论

“相关推荐”对你有帮助么?

  • 非常没帮助
  • 没帮助
  • 一般
  • 有帮助
  • 非常有帮助
提交
评论
添加红包

请填写红包祝福语或标题

红包个数最小为10个

红包金额最低5元

当前余额3.43前往充值 >
需支付:10.00
成就一亿技术人!
领取后你会自动成为博主和红包主的粉丝 规则
hope_wisdom
发出的红包
实付
使用余额支付
点击重新获取
扫码支付
钱包余额 0

抵扣说明:

1.余额是钱包充值的虚拟货币,按照1:1的比例进行支付金额的抵扣。
2.余额无法直接购买下载,可以购买VIP、付费专栏及课程。

余额充值