简体   繁体   English

如何从rabbitmq提供的webstomp websocket发送和接收数据?

[英]How to send and receive data from webstomp websocket provided by rabbitmq?

I am writing a chat application without explicit server side web socket. 我正在编写没有显式服务器端Web套接字的聊天应用程序。 I am using RabbitMQ webstomp as the web socket container and plain Javascript as the cleint to both send and receive data. 我正在使用RabbitMQ webstomp作为Web套接字容器,并使用普通Javascript作为发送和接收数据的约束。

Below is the flow : Browser -> native websocket/sockjs -> rabbitmq /sockjs websocket ( ws://127.0.0.1:15674/ws or http://localhost:15674/stomp ) -> put messages to queue. 下面是流程: 浏览器 -> 本机websocket / sockjs- > Rabbitmq / sockjs websocketws://127.0.0.1:15674 / wshttp:// localhost:15674 / stomp )->将消息放入队列。

However while testing the application, I am not able to send the data directly to ws://127.0.0.1:15674/ws . 但是,在测试应用程序时,我无法将数据直接发送到ws://127.0.0.1:15674 / ws I am just able to connect to it. 我可以连接到它。

I use the below template to send and receive data on client Javascript. 我使用以下模板在客户端Javascript上发送和接收数据。

  1. ws = new WebSocket('ws://127.0.0.1:15674/ws'); ws = new WebSocket('ws://127.0.0.1:15674 / ws');
  2. client = Stomp.over(ws); 客户端= Stomp.over(ws);
  3. client.connect('guest','guest',on_connection,on_connect_error,'/'); client.connect('guest','guest',on_connection,on_connect_error,'/');
  4. client.send(queue, {'reply-to':'/temp-queue/logs',priority: 9}, "msg" ); client.send(queue,{'reply-to':'/ temp-queue / logs',priority:9},“ msg”);
  5. client.onreceive = func() client.onreceive = func()

The problem,most likely,in your code is: 您的代码中最有可能出现的问题是:

client.send(queue, {'reply-to':'/temp-queue/logs',priority: 9}, "msg" );

you have to send the message to a topic 您必须将消息发送到主题

I suggest to see the example here: https://github.com/rabbitmq/rabbitmq-web-stomp-examples 我建议在这里查看示例: https : //github.com/rabbitmq/rabbitmq-web-stomp-examples

Here is a rapid example i made starting for the original example: 这是我从原始示例开始的一个快速示例:

var client = Stomp.client('ws://localhost:15674/ws');
      client.debug = pipe('#second');

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

在此处输入图片说明

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

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