【问题标题】:Send JSON message to Azure Queue storage将 JSON 消息发送到 Azure 队列存储
【发布时间】:2019-11-08 03:47:14
【问题描述】:

我正在使用azure-storage 节点模块。 我想在我的队列上发送 JSON 并在 azure 函数上将其放入我的队列中。

我在队列中发送消息。我将我的消息串起来放入队列中。

// This is my service who send message to queue via node lib azure-storage
       const queueMsg = {
              userId,
              token: tokenNotif
            };
            queueSvc.createMessage(Config.REGISTRATION_FCM_PUSH_NOTIFICATION_QUEUE, JSON.stringify(queueMsg), (err) => {
              if (!error) {
                this._logger.info(`User ${userId} has register this push notification token`);
                resolve(true);

              } else {
                reject(false);
              }
            });

在队列函数中我有一个错误,因为函数认为不是字符串并将消息文本推送到 xx-queue-poison {"userId":"a6c8a103-dacc-4b15-bffd-60693105f131","token":"xxxx"}

不知道为什么引号被队列中的ASCII码代替了?

我已经测试了其他东西! 从我的服务中,我调用了一个 Http Azure 函数,这个函数调用了队列存储,它是通过这种方式工作的:s ..

HttpTrigger函数调用队列 context.bindings.notificationQueue = [{ userId: name, token }];

并排队接收到的数据 context.log(`Received userId ${queueItem.userId} :: ${queueItem.token}`);

为什么通过使用 HttpTrigger 函数到 QueueTrigger 函数它正在工作,但是当我使用 lib“azure-storage”时不起作用?

谢谢

【问题讨论】:

  • 我找到了!我没有将对象字符串化,而是将其转换为 b64。 Buffer.from(JSON.stringify(queueMsg)).toString('base64') 它正在工作

标签: node.js azure azure-functions


【解决方案1】:

不知道为什么引号被队列中的ASCII码代替了?

基本上,SDK 正在转换字符串消息以使其成为 XML 安全的。如果您查看 SDK 中的代码,默认情况下它使用 TextXmlQueueMessageEncoder 作为消息编码器。 encode 函数将 " 替换为 " 以使其成为 XML 安全的。

来自SDK代码(部分代码sn-ps):

QueueService.prototype.createMessage = function (queue, messageText, optionsOrCallback, callback) {
  var userOptions;
  azureutil.normalizeArgs(optionsOrCallback, callback, function (o, c) { userOptions = o; callback = c; });

  validate.validateArgs('createMessage', function (v) {
    v.string(queue, 'queue');
    v.queueNameIsValid(queue);
    v.callback(callback);
  });

  var xmlMessageDescriptor = QueueMessageResult.serialize(messageText, this.messageEncoder);

function TextXmlQueueMessageEncoder(){
}
util.inherits(TextXmlQueueMessageEncoder, QueueMessageEncoder);

/**
 * Encode utf-8 string by escaping the xml markup characters.
 * @this TextXmlQueueMessageEncoder
 * 
 * @param   {string}    [input]               The target to be encoded.
 * 
 * @return {string}
 */
TextXmlQueueMessageEncoder.prototype.encode = function(input){
  return input.replace(/&/gm, '&')
    .replace(/</gm, '&lt;')
    .replace(/>/gm, '&gt;')
    .replace(/"/gm, '&quot;')
    .replace(/'/gm, '&apos;');
};

一种可能的解决方案是按照您的建议将字符串转换为 base64 编码的字符串。但是,如果您使用 SDK 检索消息,则不应在消息正文中看到这些 &amp;quot;,因为 SDK 负责对消息进行解码。

【讨论】:

    猜你喜欢
    • 2014-01-28
    • 2021-02-22
    • 2015-12-13
    • 1970-01-01
    • 1970-01-01
    • 1970-01-01
    • 2020-09-28
    • 2014-08-30
    • 1970-01-01
    相关资源
    最近更新 更多