OStack程序员社区-中国程序员成长平台

标题: android - 带有 Android 和 iOS 的 Azure 服务总线消息传递 [打印本页]

作者: 菜鸟教程小白    时间: 2022-12-12 15:41
标题: android - 带有 Android 和 iOS 的 Azure 服务总线消息传递

目前,我的团队对 Azure 服务总线消息传递以及在 Android 和 iOS 上找到正确的客户端/协议(protocol)感到非常沮丧。

Service Bus 支持 amqp 1.0 协议(protocol)。有 Android 和 iOS 的客户端来处理 amqp 1.0 吗?

监听队列消息的其他选项是什么?

是否有任何示例应用可以在 Android 和/或 iOS 上监听来自 Azure 服务总线的消息? (不是从 2013 年开始,实际上正在工作/编译)


附加信息(与问题无关):

我们遇到的问题是:



Best Answer-推荐答案


经过近 80 小时的调查、谷歌和反复试验,我找到了从 Xamarin 发送 BrokeredMessages 的解决方案。以下代码有效:

    private const String topic = "mytopic";
    private const String keyName = "RootManageSharedAccessKey";
    private const String sharedAccessKey = "myAccessKey";
    private const String baseAddress = "myaddress.servicebus.windows.net";

    private async static void SendMessage(String baseAddress, string queueName, string body)
    {
        string fullAddress = $"{baseAddress}{queueName}/messages";
        HttpClient client = Program.CreateHttpClient();
        client.DefaultRequestHeaders.TryAddWithoutValidation("BrokerProperties", @"{ ""MessageId"": """ + Guid.NewGuid().ToString() + @"""}");

        MemoryStream stream = new MemoryStream();
        DataContractSerializer s = new DataContractSerializer(typeof(string));
        XmlDictionaryWriter writer = XmlDictionaryWriter.CreateBinaryWriter(stream);
        writer.WriteStartDocument();
        s.WriteStartObject(writer, body);
        s.WriteObjectContent(writer, body);
        s.WriteEndObject(writer);
        writer.Flush();
        stream.Position = 0;

        var response = await client.PostAsync(fullAddress, new System.Net.Http.StreamContent(new System.IO.MemoryStream(stream.ToArray())));
    }

    private static HttpClient CreateHttpClient()
    {
        HttpClient client = new HttpClient();

        client.DefaultRequestHeaders.TryAddWithoutValidation("Content-Type", "application/atom+xml;type=entry;charset=utf-8");
        string token = GetSASToken(baseAddress, Program.keyName, Program.sharedAccessKey);
        client.DefaultRequestHeaders.TryAddWithoutValidation("Authorization", token);

        return client;
    }

    private static string GetSASToken(string baseAddress, string SASKeyName, string SASKeyValue)
    {
        TimeSpan fromEpochStart = DateTime.UtcNow - new DateTime(1970, 1, 1);
        string expiry = Convert.ToString((int)fromEpochStart.TotalSeconds + 3600);
        string stringToSign = WebUtility.UrlEncode(baseAddress) + "\n" + expiry;

        HMACSHA256 hmac = new HMACSHA256(Encoding.UTF8.GetBytes(SASKeyValue));
        string signature = Convert.ToBase64String(hmac.ComputeHash(Encoding.UTF8.GetBytes(stringToSign)));

        string sasToken = String.Format(CultureInfo.InvariantCulture, "SharedAccessSignature sr={0}&sig={1}&se={2}&skn={3}",
              WebUtility.UrlEncode(baseAddress), WebUtility.UrlEncode(signature), expiry, SASKeyName);
        return sasToken;
    }

提一下使用 async await 做起来很重要

new System.Net.Http.StreamContent(new System.IO.MemoryStream(stream.ToArray()))

否则 PostAsync 将阻塞,直到发生超时。我不确定为什么。我猜 MemoryStream 以某种方式关闭或处置,等待的同步 Context 阻塞了 PostAsync 调用。

关于android - 带有 Android 和 iOS 的 Azure 服务总线消息传递,我们在Stack Overflow上找到一个类似的问题: https://stackoverflow.com/questions/31165311/






欢迎光临 OStack程序员社区-中国程序员成长平台 (https://ostack.cn/) Powered by Discuz! X3.4