前言
畢業一年多了,很少寫點什么東西。自從進入現在的公司,就主要負責淘寶應用的項目,從開始的沒聽說過,到現在了解一二,一路摸爬滾打,苦逼的生活歷歷在目。
最開始的時候也百度谷歌,在CSDN上問個問題,留了個QQ后,也有好幾個人來問我淘寶主動通知的實現(那時我已經知道怎么實現了),今天又有人來問我,索性寫點東西分享給大家。這里吐槽一下淘寶的開放平台,sdk(老版本)里面都沒看到注釋,完全憑自己理會,去只是中心提問,支持中心的人讓你看文檔,好像別個不看文檔似的。不過新版本有了注釋了。好了,進入正題:
主動通知,就是淘寶主動發消息給你,前提是你實現了它的接口。
先在http://api.taobao.com/myresources/mySdk.htm?appkey=21612266下載好sdk,我下的是標准版的.net的,你也可以根據自己訂購的消息下載對應的sdk。
正題
為方便講解,建個控制台的項目,引用下載的TopSdk.dll,右鍵項目屬性,將應用程序的目標框架改為.NET Framework 4
新建個TaoSession類
1 public class TaoSession : IConnectionLifeCycleListener, ITopCometMessageListener 2 { 3 #region 連接接口實現 4 public void OnBeforeConnect() 5 { 6 Console.WriteLine("Before Connect..."); 7 } 8 9 public void OnConnect() 10 { 11 Console.WriteLine("Connect..."); 12 } 13 14 public void OnConnectError(Exception e) 15 { 16 throw new NotImplementedException(); 17 } 18 19 public void OnException(Exception throwable) 20 { 21 throw new NotImplementedException(); 22 } 23 24 public void OnMaxReadTimeoutException() 25 { 26 throw new NotImplementedException(); 27 } 28 29 public void OnReadTimeout() 30 { 31 Console.WriteLine("Time Out!"); 32 } 33 34 public void OnSysErrorException(Exception e) 35 { 36 throw new NotImplementedException(); 37 } 38 #endregion 39 40 #region 消息接口實現 41 public void OnClientKickOff() 42 { 43 throw new NotImplementedException(); 44 } 45 46 public void OnConnectMsg(string message) 47 { 48 Console.WriteLine(DateTime.Now.ToString() + ":Connected..."); 49 } 50 51 public void OnConnectReachMaxTime() 52 { 53 throw new NotImplementedException(); 54 } 55 56 public void OnDiscardMsg(string message) 57 { 58 // 丟棄了的消息 59 Console.WriteLine(DateTime.Now.ToString() + ":DiscardMsg..."); 60 } 61 62 public void OnHeartBeat() 63 { 64 // 接受心跳包 65 Console.WriteLine(DateTime.Now.ToString() + ":HeartBeat..."); 66 } 67 68 public void OnOtherMsg(string message) 69 { 70 // 其他消息 71 Console.WriteLine(DateTime.Now.ToString() + ":Other message..."); 72 } 73 74 public void OnReceiveMsg(string message) 75 { 76 try 77 { 78 // 接受到的消息 79 Console.WriteLine("Receives a message..."); 80 81 // 輸出消息 82 Console.WriteLine("接受到的消息:" + message); 83 84 // 解析消息 85 //object obj = MessageDecode.DecodeMsg(message); 86 } 87 catch (Exception e) 88 { 89 Console.WriteLine("處理消息時出錯了···"); 90 } 91 } 92 93 public void OnServerKickOff() 94 { 95 throw new NotImplementedException(); 96 } 97 98 public void OnServerRehash() 99 { 100 throw new NotImplementedException(); 101 } 102 103 public void OnServerUpgrade(string message) 104 { 105 // 淘寶服務器在升級 106 Console.WriteLine("Taobao Server is Upgrade!"); 107 } 108 #endregion 109 }
然后Program.cs開始啟動監聽
1 class Program 2 { 3 static void Main(string[] args) 4 { 5 // 淘寶的appkey和appsecret,申請就有了 6 string appkey = "", appsecret = ""; 7 // 沙箱環境的監聽地址 8 string taotaoListenerUrl = "http://stream.api.tbsandbox.com/stream"; 9 10 Configuration conf = new Configuration(appkey, appsecret, null); 11 conf.SetConnectUrl(taotaoListenerUrl); 12 13 ITopCometStream stream = new TopCometStreamImpl(conf); 14 15 stream.SetConnectionListener(new TaoSession()); 16 stream.SetMessageListener(new TaoSession()); 17 18 stream.Start(); 19 20 // 停止 21 //stream.Stop(); 22 } 23 }
這樣就可以接受到淘寶的消息了,看起來很簡單,具體的消息怎么處理,還是根據不同的業務來定,還要將接收到的消息解析(json格式),淘寶開放平台有提供解析的類
上圖沙箱環境測試的,HeartBeat為心跳,接受到的是沙箱發貨的消息,忘了補充:要接受消息,先要在應用設置的主動通知管理訂閱消息