淘宝开放平台主动通知的实现

淘宝开放平台主动通知的实现

 

 前言

  毕业一年多了,很少写点什么东西。自从进入现在的公司,就主要负责淘宝应用的项目,从开始的没听说过,到现在了解一二,一路摸爬滚打,苦逼的生活历历在目。

  最开始的时候也百度谷歌,在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为心跳,接受到的是沙箱发货的消息,忘了补充:要接受消息,先要在应用设置的主动通知管理订阅消息

  

 
 
 
原文地址:https://www.cnblogs.com/Leo_wl/p/3498630.html