How to tell when a Stomp server disconnected from the Stomp.JS client

一笑奈何 提交于 2020-02-27 23:08:17

问题


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) {
  ...
}

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 

But I am not sure how to capture this error in the JS. I am sure I am missing something easy but a little help would be great.


回答1:


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.




回答2:


Oops just figured it out I will delete in a bit if it doesn't help others. I needed to use SockJS client instead of the Stomp one...

var socket = new SockJS('/hello');
...
socket.onclose = function() {
    console.log('close');
    stompClient.disconnect();
    setConnected();
};


来源:https://stackoverflow.com/questions/24513818/how-to-tell-when-a-stomp-server-disconnected-from-the-stomp-js-client

标签
易学教程内所有资源均来自网络或用户发布的内容,如有违反法律规定的内容欢迎反馈
该文章没有解决你所遇到的问题?点击提问,说说你的问题,让更多的人一起探讨吧!