本文介绍了如何判断 Stomp 服务器何时与 Stomp.JS 客户端断开连接的处理方法,对大家解决问题具有一定的参考价值,需要的朋友们下面随着小编来一起学习吧!

问题描述

我正在通过 Sock.JS 客户端发送 stomp 消息.当我断开服务器连接时,我希望在客户端上显示一条警告消息.为此,我实现了服务器端心跳

I am sending a stomp message over a Sock.JS client. When I disconnect the server I would like a warning message to show up on the client. To do this I have implemented a server side heartbeat

stompClient = Stomp.over(socket);
stompClient.heartbeat.outgoing = 20000;
stompClient.heartbeat.incoming = 20000;
stompClient.connect({}, function(frame) {
  ...
}

在 Chrome 开发者控制台中,我看到消息

In the Chrome developer console I see the message

POST http://localhost:8080/hello/800/8n_btbxb/xhr_streaming net::ERR_CONNECTION_RESET sockjs-0.3.min.js:27
Whoops! Lost connection to undefined

如何捕获此错误消息?

推荐答案

正如来自 muttonUp stomp.js 所指出的https://github.com/jmesnil/stomp-websocket/ 将覆盖 onclose 处理程序.另一方面,它提供了在连接时传递错误回调的选项:

As pointed out by muttonUp stomp.js from https://github.com/jmesnil/stomp-websocket/ will overwrite the onclose handler. On the other hand it provides the option to pass an error-callback on connect:

stompClient.connect({}, function(frame) {
    ...
}, function(message) {
    // check message for disconnect
});

由于您的回调会收到多种错误,因此您必须检查消息,它确实是哎呀![...]",表示连接丢失.

Since you will get several kinds of errors delivered to your callback, you have to check the message it it was indeed the "Whoops! [...]" which indicates a connection loss.

这篇关于如何判断 Stomp 服务器何时与 Stomp.JS 客户端断开连接的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持!

08-11 04:23