简体   繁体   English

STOMP Web套接字回调不起作用

[英]STOMP web socket callback doesn't work

I am trying to connect to a mqtt brocker using STOMP javascript web socket. 我正在尝试使用STOMP javascript Web套接字连接到mqtt brocker。 The connection is made. 建立连接。 But the callback function in my code is not called? 但是我的代码中的回调函数没有被调用吗? But the ping messages are send. 但是ping消息已发送。

I am using a url as the host address. 我使用网址作为主机地址。 Here is my code. 这是我的代码。

<!DOCTYPE html>

<html>
<head>


  <script src="http://cdn.jsdelivr.net/sockjs/0.3.4/sockjs.min.js"></script>
  <script src="stomp.js"></script>

  <script>

var ws = new SockJS('http://108.567.234.143:9876/stomp');


var client = Stomp.over(ws);




client.connect('username', 'pw', connect_callback, on_error);


client.heartbeat.outgoing = 20000; // client will send heartbeats every 20000ms
client.heartbeat.incoming = 0;     // client does not want to receive heartbeats
                                   // from the server

var connect_callback = function() {

   alert("Connected to rabbitMQ");
var subscription = client.subscribe("CRICKET", subs_callback);
console.log('subscribe to CRICKET'); 
};

var on_error =  function(error) {
    console.log('error');
};

var    subs_callback = function(message) {
// called when the client receives a STOMP message from the server
if (message.body) {
  alert("got message with body " + message.body)
//console.log('got message with body' + message.body);
} else {
  alert("got empty message");
}
};


console.log('message.body');


</script>


</head>

<body> 


hello world



</body>
</html>  

I cant subscribe to a topic. 我不能订阅一个话题。 Please tell me what is wrong? 请告诉我怎么了? Here is the console log 这是控制台日志

Thanks 谢谢

The callbacks are being defined after they're used. 回调在使用后进行定义。

It's likely the .connect() function is silently ignoring the undefined functions, so you didn't see any errors. .connect()函数可能会默默地忽略未定义的函数,因此您看不到任何错误。

Moving the .connect() to after the callbacks are defined, should fix the issue. 在定义回调之后.connect()移至,应该可以解决此问题。

var ws = new SockJS('http://108.567.234.143:9876/stomp');


var client = Stomp.over(ws);


client.heartbeat.outgoing = 20000; // client will send heartbeats every 20000ms
client.heartbeat.incoming = 0;     // client does not want to receive heartbeats
                                   // from the server

var connect_callback = function() {

   alert("Connected to rabbitMQ");
var subscription = client.subscribe("CRICKET", subs_callback);
console.log('subscribe to CRICKET'); 
};

var on_error =  function(error) {
    console.log('error');
};

var    subs_callback = function(message) {
// called when the client receives a STOMP message from the server
if (message.body) {
  alert("got message with body " + message.body)
//console.log('got message with body' + message.body);
} else {
  alert("got empty message");
}
};


client.connect('username', 'pw', connect_callback, on_error);


console.log('message.body');

声明:本站的技术帖子网页,遵循CC BY-SA 4.0协议,如果您需要转载,请注明本站网址或者原文地址。任何问题请咨询:yoyou2525@163.com.

 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM