android - 带有 Android 和 iOS 的 Azure 服务总线消息传递
<p><p>目前,我的团队对 Azure 服务总线消息传递以及在 Android 和 iOS 上找到正确的客户端/协议(protocol)感到非常沮丧。 </p>
<p>Service Bus 支持 amqp 1.0 协议(protocol)。有 Android 和 iOS 的客户端来处理 amqp 1.0 吗?</p>
<p>监听队列消息的其他选项是什么?</p>
<p>是否有任何示例应用可以在 Android 和/或 iOS 上监听来自 Azure 服务总线的消息? (不是从 2013 年开始,实际上正在工作/编译)</p>
<hr/>
<p>附加信息(与问题无关):</p>
<p>我们遇到的问题是:</p>
<ul>
<li>确实如此:azure 团队主要关注微软技术,主要是 Windows Phone 8 和适用于 Android 和 iOS 的 Xamarin。 </li>
<li>文档:分散主题的仙境,有时有 android 的示例,有时有 xamarin android,有时有 ios 或 xamarin。</li>
</ul></p>
<br><hr><h1><strong>Best Answer-推荐答案</ strong></h1><br>
<p><p>经过近 80 小时的调查、谷歌和反复试验,我找到了从 Xamarin 发送 <code>BrokeredMessages</code> 的解决方案。以下代码有效:</p>
<pre><code> 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;
}
</code></pre>
<p>提一下使用 async await 做起来很重要</p>
<pre><code>new System.Net.Http.StreamContent(new System.IO.MemoryStream(stream.ToArray()))
</code></pre>
<p>否则 <code>PostAsync</code> 将阻塞,直到发生超时。我不确定为什么。我猜 <code>MemoryStream</code> 以某种方式关闭或处置,等待的同步 <code>Context</code> 阻塞了 <code>PostAsync</code> 调用。</p></p>
<p style="font-size: 20px;">关于android - 带有 Android 和 iOS 的 Azure 服务总线消息传递,我们在Stack Overflow上找到一个类似的问题:
<a href="https://stackoverflow.com/questions/31165311/" rel="noreferrer noopener nofollow" style="color: red;">
https://stackoverflow.com/questions/31165311/
</a>
</p>
页:
[1]