简体   繁体   English

将 JSON 消息发送到 Azure 队列存储

[英]Send JSON message to Azure Queue storage

I am using azure-storage node module.我正在使用azure-storage节点模块。 I would like to send JSON on my queue and get it into my queue on azure function.我想在我的队列中发送 JSON 并通过 azure 函数将其放入我的队列中。

I send message in queue.我在队列中发送消息。 I stringify my message to put in queue.我将我的消息字符串化以放入队列。

// 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);
              }
            });

And in the queue function i have an error because the function think isn't a string and push the message text on xx-queue-poison {"userId":"a6c8a103-dacc-4b15-bffd-60693105f131","token":"xxxx"}在队列函数中,我有一个错误,因为函数认为不是字符串并将消息文本推{"userId":"a6c8a103-dacc-4b15-bffd-60693105f131","token":"xxxx"} xx-queue-poison {"userId":"a6c8a103-dacc-4b15-bffd-60693105f131","token":"xxxx"}

I don't know why the quote is replaced by ASCII code on queue ?我不知道为什么引号被队列上的 ASCII 代码替换?

I have tested something else!我测试了别的东西! From my service i call a Http Azure function, and this function call the Queue Storage and it's work by this way :s ..在我的服务中,我调用了一个 Http Azure 函数,这个函数调用了队列存储,它是通过这种方式工作的 :s ..

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

And queue received data context.log(`Received userId ${queueItem.userId} :: ${queueItem.token}`);和队列接收数据context.log(`Received userId ${queueItem.userId} :: ${queueItem.token}`);

Why by using HttpTrigger function to QueueTrigger function it's working, but when i am using the lib "azure-storage" is not working ?为什么通过使用 HttpTrigger 函数到 QueueTrigger 函数它可以工作,但是当我使用库“azure-storage”时却不起作用?

Thx谢谢

I don't know why the quote is replaced by ASCII code on queue ?我不知道为什么引号被队列上的 ASCII 代码替换?

Basically the SDK is converting the string message to make it XML safe.基本上,SDK 正在转换字符串消息以使其 XML 安全。 If you look at the code in the SDK, by default it uses TextXmlQueueMessageEncoder as the message encoder.如果您查看 SDK 中的代码,默认情况下它使用TextXmlQueueMessageEncoder作为消息编码器。 The encode function replaces " with " to make it XML safe.encode函数替换""使其XML安全。

From the SDK code (partial code snippets):来自 SDK 代码(部分代码片段):

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;');
};

One possible solution would be to convert the string to a base64 encoded string as you suggested.一种可能的解决方案是按照您的建议将字符串转换为 base64 编码的字符串。 However if you're using the SDK to retrieve the messages, you should not see these &quot;但是,如果您使用 SDK 来检索消息,则不应看到这些&quot; in your message body as the SDK takes care of decoding the message.在您的消息正文中,因为 SDK 负责对消息进行解码。

暂无
暂无

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

相关问题 使用 Azure 存储节点 SDK 创建队列消息时,队列触发 Azure 函数抛出异常 - Queue triggered Azure function throws exception when Queue message is created with the Azure Storage Node SDK 在Azure函数绑定中将引用队列触发消息作为JSON - Reference Queue Trigger Message as JSON in Azure Functions Bindings 更新后无法删除Azure存储队列消息(找不到消息) - Can't delete azure storage queue message after it has been updated (Message not found) Azure存储队列-空触发 - Azure Storage Queue - Trigger on empty 如何从Azure移动服务脚本向Azure服务总线队列发送消息 - How to send a message to Azure service bus queue from a Azure mobile services script 如何使用 Azure 函数绑定发送预定的服务总线队列消息? - How to use an Azure Function Binding to send a scheduled Service Bus Queue message? Azure Function v2 在 NodeJS (index.js) 中使用 ServiceBusQueueTrigger - 如果出现异常,如何将消息发送到死信队列 - Azure Function v2 with ServiceBusQueueTrigger in NodeJS (index.js) - How to send message to Deadletter Queue if there is exception 将数千条消息添加到Azure存储队列 - add thousands of messages to an Azure Storage Queue 如何在节点js中向azure服务总线队列发送消息时将内容类型指定为application/json? - How to specify content type as application/json while sending message to azure service bus queue in node js? Azure队列不断通知未决消息 - Azure Queue to inform about pending message constantly
 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM