rabbitmq-stomp - java客户端发布者,javascript(web)监听器

时间:2014-06-02 06:26:54

标签: java javascript rabbitmq stomp

我试图通过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>

1 个答案:

答案 0 :(得分:0)

改变这个:

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

我尝试了你的代码,并正确使用它。 我希望它有所帮助

修改**

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