3

我在客户端使用 protobuf.js 为rabbitmq stomp 创建了一个示例。protobuf 示例链接:https ://github.com/dcodeIO/ProtoBuf.js

发送消息文件内容:-

var Game = builder.build("Game");
var Car = Game.Cars.Car;
var car = new Car("Rusty", "mayur");
var buffer = car.encode();      

var mq_username = "guest",
mq_password = "guest",
mq_vhost    = "/",
mq_url      = 'http://192.168.0.14:15674/stomp',
mq_queue1 = '/queue/A3';
var client = Stomp.client(mq_url);
function on_connect() 
{

client.send(mq_queue1, { priority: 9}, buffer); 

}
window.onload = function () 
{
client.connect(
        mq_username,
        mq_password,
        on_connect,
        on_connect_error,
        mq_vhost
      );    

}

接收文件内容:-

  var Game = builder.build("Game");
  var Car = Game.Cars.Car;
  var car = new Car("Rusty", "mayur");
  var buffer = car.encode();        

  var mq_username = "guest",
  mq_password = "guest",
  mq_vhost    = "/",
  mq_url      = 'http://192.168.0.14:15674/stomp',
  mq_queue1 = '/queue/A3';
  var client = Stomp.client(mq_url);
  function on_connect() 
  {
        un = client.subscribe(mq_queue1, on_message);

  }
function on_message(m) 
{
  console.log('message received'); 
  console.log(m);
  var buffer = m.body;
  alert(buffer);                     
  var carDec =Car.decode(buffer);                         
  alert(carDec.model);

}
  window.onload = function () 
  {
        client.connect(
            mq_username,
            mq_password,
            on_connect,
            on_connect_error,
            mq_vhost
          );    

  }

我无法解码 stomp(protobuf 二进制数据)发送的数据。如果问题通过"content-length"解决,请指导我将"content-length"设置为stomp header

请帮我。没有人知道这件事吗?

4

1 回答 1

5

我知道我会在 2 年后回答!但我想我应该分享:)

我正在开发一个使用rabbitmq stomp 发送ProtoBuf 编码数据的项目。

我遇到了同样的问题:我可以发送但无法在接收端解码。

我只需使用另一种编码即可使其工作:

  • 改变:

    var buffer = car.encode();
    

    至:

    var buffer = car.encode64();
    
  • 改变:

    var carDec =Car.decode64(buffer);
    

    至:

    var carDec =Car.decode64(buffer);
    

这解决了我的问题,我希望它也解决了你的问题。

于 2015-06-09T12:38:52.107 回答