参考文章
http://jmesnil.net/stomp-websocket/doc/
https://www.toptal.com/java/stomp-spring-boot-websocket
http://www.mydlq.club/article/86/
https://docs.spring.io/spring-framework/docs/4.3.x/spring-framework-reference/html/websocket.html
SpringBoot集成websocket, 并使用rabbitmq的stomp插件作为中继服务
- 如果不设置客户端默认使用guest,guest账号登陆
// 设置客户端登陆账号
.setClientLogin(stompBrokerProperties.getUsername())
.setClientPasscode(stompBrokerProperties.getPassword())
指定客户端使用的账号.png
- 配置中继服务
@Override
public void configureMessageBroker(MessageBrokerRegistry registry) {
if (log.isTraceEnabled()) {
log.trace("host:{} port:{} username:{} password:{}", stompBrokerProperties.getHost(), stompBrokerProperties.getPort(), stompBrokerProperties.getUsername(), stompBrokerProperties.getPassword());
}
registry.setApplicationDestinationPrefixes("/my_app");
// Enables a simple in-memory broker
// registry.enableSimpleBroker("/topic", "/queue");
// Use this for enabling a Full featured broker like RabbitMQ
registry.enableStompBrokerRelay("/topic", "/queue")
.setRelayHost(stompBrokerProperties.getHost())
.setRelayPort(stompBrokerProperties.getPort())
// Set client login account, client will send `ping` to relay interval
.setClientLogin(stompBrokerProperties.getUsername())
.setClientPasscode(stompBrokerProperties.getPassword())
.setSystemLogin(stompBrokerProperties.getUsername())
.setSystemPasscode(stompBrokerProperties.getPassword())
// .setSystemHeartbeatSendInterval(100000)
// .setSystemHeartbeatReceiveInterval(100000)
.setUserRegistryBroadcast("/topic/simp-user-registry")
.setUserDestinationBroadcast("/topic/unresolved-user-destination");
}
问题:websocket客户端大概在10几分钟左右会和中继断连
- websocket客户端和rabbitmq stomp中继服务会定期发送和响应ping,pong指令保活(红色向下箭头代表服务器响应的消息,绿色向上箭头代表客户端发送的消息) ,经观察大概10几分钟,客户端指令到达不了中继服务,然后中继服务会主动关闭客户端连接。
websocket客户端保活.png
中继主动关闭了客户端连接.png
- SockJS协议要求服务器发送心跳消息,以防止代理断定连接挂起,Spring SockJS配置有一个名为heartbeatTime的属性,你可以使用它来定制频率。默认情况下,在25秒后发送心跳,假设在该连接上没有发送其他消息,这个25秒的值符合以下对公共互联网应用程序的IETF建议。SockJS添加了最少的消息框架,例如,服务器最初发送字母o(“open” frame),消息以["message1","message2"](json编码数组)的形式发送,如果在25秒内(默认情况下)没有消息流,则发送字母h("heartbeat" frame)和字母c("close" frame)来关闭会话。
使用简单内存broker.png
# 后台设置
// Enables a simple in-memory broker
registry.enableSimpleBroker("/topic", "/queue");
解决使用rabbit的stomp中继导致的websocket客户端断连问题
经观察,客户端断连的时间不定,所以初步怀疑是由于公司网络不稳定,导致网络抖动造成短时间之内客户端的ping到达不了rabbit的stomp中继。stomp.js默认的保活时间间隔是10s,经观察如果超过10s*2出现网络故障,中继就会认为客户端连接断开,主动断开客户端连接。后把保活时间间隔设置成100s,观察了一晚上,客户端连接比较正常。
var stompClient = null;
function connect(username) {
var socket = new SockJS('/my_ws');
stompClient = Stomp.over(socket);
stompClient.heartbeat.outgoing = 100000; // 100s,默认10s
stompClient.heartbeat.incoming = 100000; // 100s
var headers = {
'username': username
};
stompClient.connect(headers, function (frame) {
console.log('Connected: ' + frame);
// 点对点消息
stompClient.subscribe('/user/queue/msg', function (msg) {
console.log("queue:", msg.body);
});
// setTimeout(function () {
// // stompClient.send("/my_app/hello", {}, "hello");
// stompClient.send("/my_app/hello/clp", {}, "hello");
// }, 2000);
}, function (errorCallback) {
console.log("断开连接:", errorCallback);
});
}
初始观察时间.png
终了观察时间.png