【问题标题】:rabbitmq-stomp - java client publisher, javascript(web) listenerrabbitmq-stomp - java 客户端发布者,javascript(web) 监听器
【发布时间】:2014-06-02 06:26:53
【问题描述】:

我正在尝试通过 java 客户端简单地发送消息并使用 stomp 在服务器端获取它。 我启用了 stomp 插件,它确实有效,但似乎没有通信,我做错了什么?....

java 客户端

 public class ServerToStomp {

private static final String EXCHANGE_NAME = "test1";

public static void main(String[] argv) throws Exception {

    ConnectionFactory factory = new ConnectionFactory();
    factory.setHost("localhost");
    Connection connection = factory.newConnection();
    Channel channel = connection.createChannel();

    channel.exchangeDeclare(EXCHANGE_NAME, "topic");

    String routingKey = "test1";
    String message = "hello";
    channel.basicPublish(EXCHANGE_NAME, routingKey, null, message.getBytes());
    System.out.println(" [x] Sent '" + routingKey + "':'" + message + "'" + channel.getConnection().getPort());

    connection.close();
}
// ...
}

还有 stomp 客户端:

 <!DOCTYPE html>
<html><head>
  <script src="http://ajax.googleapis.com/ajax/libs/jquery/1.6.2/jquery.min.js"></script>
  <script src="http://cdn.sockjs.org/sockjs-0.3.min.js"></script>
 <script src="stomp.js"></script>

</head><body lang="en">


<div id="first" class="box">
  <h2>Received</h2>
  <div></div>
  <form><input autocomplete="off" value="Type here..."></input></form>
</div>

<div id="second" class="box">
  <h2>Logs</h2>
  <div></div>
</div>

<script>
    var has_had_focus = false;
    var pipe = function(el_name, send) {
        var div  = $(el_name + ' div');
        var inp  = $(el_name + ' input');
        var form = $(el_name + ' form');

        var print = function(m, p) {
            p = (p === undefined) ? '' : JSON.stringify(p);
            div.append($("<code>").text(m + ' ' + p));
            div.scrollTop(div.scrollTop() + 10000);
        };

        if (send) {
            form.submit(function() {
                send(inp.val());
                inp.val('');
                return false;
            });
        }
        return print;
    };

  // Stomp.js boilerplate
  var ws = new SockJS('http://' + window.location.hostname + ':15674/stomp');
  var client = Stomp.over(ws);

  // SockJS does not support heart-beat: disable heart-beats
  client.heartbeat.outgoing = 0;
  client.heartbeat.incoming = 0;
  client.debug = pipe('#second');

  var print_first = pipe('#first', function(data) {
      client.send('/topic/test1', {"content-type":"text/plain"}, data);
  });
  var on_connect = function(x) {
      id = client.subscribe("/topic/test1", function(d) {
          debugger;
          print_first(d.body);
      });
  };
  var on_error =  function() {
    console.log('error');
  };
  client.connect('guest', 'guest', on_connect, on_error, '/');

  $('#first input').focus(function() {
      if (!has_had_focus) {
          has_had_focus = true;
          $(this).val("");
      }
  });
</script>

【问题讨论】:

    标签: java javascript rabbitmq stomp


    【解决方案1】:

    改变这个:

    var on_connect = function(x) {
          id = client.subscribe("/exchange/test1/test1", function(d) {
              debugger;
              print_first(d.body);
          });
      };
    

    我尝试了您的代码并且可以正常使用。 希望对你有帮助

    编辑**

    您还必须启用web-stomp plug-in

    【讨论】:

      猜你喜欢
      • 2016-10-21
      • 1970-01-01
      • 1970-01-01
      • 2014-11-05
      • 2015-09-14
      • 1970-01-01
      • 2021-12-14
      • 2020-12-01
      • 2016-06-25
      相关资源
      最近更新 更多