今天碰到一個很討厭的問題,使用nodejs 接收Azure service bus隊列消息的時候,出現了:@strin3http//schemas.microsoft.com/2003/10/Serialization/�html
如何產生的:node
使用 C# 往隊列中插入消息,使用 nodejs 讀取消息。url
出現亂碼的緣由:spa
寫入消息使用AMQP對象進行序列化,讀取的時候使用http進行傳輸。code
解決辦法:orm
寫入的時候改用 stream 的方式寫入而不是直接寫入字符串。htm
var obj = new { key = i, url = string.Format("http://item.m.jd.com/product/{0}.html", i) };
var bytes = Encoding.ASCII.GetBytes(Newtonsoft.Json.JsonConvert.SerializeObject(obj)); var stream = new MemoryStream(bytes);
//加入消息 messages.Add(new BrokeredMessage(stream));
Nodejs 接收部分:對象
//接收隊列消息。 var serviceBusService = azure.createServiceBusService("..."); serviceBusService.receiveQueueMessage('1001', function (error, m) { console.log(m); }
stackoverflow 的個回答:blog