1、定義XmppClient
- public XmppClient(string pstrIp,int pIntPort,string pstrUser,string pstrPas)
- {
- //構造XmppClient
- xmpp = new XmppClientConnection(pstrIp);
-
- xmpp.AutoAgents = false;
- xmpp.AutoPresence = true;
- xmpp.AutoRoster = true;
- xmpp.AutoResolveConnectServer = true;
- xmpp.Port = pIntPort;
- xmpp.Username = pstrUser;
- xmpp.Password = pstrPas;
- xmpp.ConnectServer = pstrIp;
-
- m_strServerName = Dns.GetHostEntry(IPAddress.Parse(pstrIp)).HostName; //獲取服務器的計算機名
- xmpp.Server = m_strServerName;
-
- //登錄成功過程
- xmpp.OnSaslStart += new agsXMPP.sasl.SaslEventHandler(xmpp_OnSaslStart); //驗證時觸發(fā)
- xmpp.OnLogin += new ObjectHandler(xmppCon_OnLogin); //登錄成功時觸發(fā)
- xmpp.OnRosterItem += new XmppClientConnection.RosterHandler(xmpp_OnRosterItem); //當讀取、添加、刪除聯(lián)系人時觸發(fā)
- xmpp.OnPresence += new PresenceHandler(xmppCon_OnPresence); //收到Presence節(jié)時引發(fā)的事件
-
- //消息處理
- xmpp.OnMessage += new MessageHandler(xmppCon_OnMessage); //消息事件
-
- //錯誤處理
- xmpp.OnSocketError += new ErrorHandler(xmpp_OnSocketError); //找不到服務器時觸發(fā)
- xmpp.OnError += new ErrorHandler(xmpp_OnError); //登錄錯誤時觸發(fā)
-
- //關閉處理
- xmpp.OnClose += new ObjectHandler(xmpp_OnClose); //網(wǎng)絡中斷或關閉事件
-
- }
2、初始化定義的xmppClient,然后打開連接
- xmppClient = new agsXMPPNet.XmppClient(psys.StrIp, psys.IntPort, psys.StrUser, psys.StrPas);
-
- xmppClient.OnPresenceEvent += new agsXMPPNet.XmppClient.OnPresence(xmppClient_OnPresenceEvent);
- xmppClient.OnRosterItemEvent += new agsXMPPNet.XmppClient.OnRosterItem(xmppClient_OnRosterItemEvent);
-
- xmppClient.LoginEvent += new agsXMPPNet.XmppClient.MsgHandler(xmppClient_LoginEvent);
- xmppClient.Login(); //這里其實就是xmpp.open()
3、設置驗證
- void xmpp_OnSaslStart(object sender, agsXMPP.sasl.SaslEventArgs args)
- {
- args.Auto = false;
- args.Mechanism = agsXMPP.protocol.sasl.Mechanism.GetMechanismName
- (agsXMPP.protocol.sasl.MechanismType.PLAIN);
- }
4、登錄成功事件
- void xmppCon_OnLogin(object sender)
- {
- RosterItem rt = new RosterItem
- {
- IsLoginSucc = true
- };
- LoginEvent(rt);
- }
5、讀取聯(lián)系人
- /// <summary>
- /// 當讀取、添加、刪除聯(lián)系人時觸發(fā)
- /// </summary>
- /// <param name="sender"></param>
- /// <param name="item"></param>
- void xmpp_OnRosterItem(object sender, agsXMPP.protocol.iq.roster.RosterItem item)
- {
- if (item.Subscription == agsXMPP.protocol.iq.roster.SubscriptionType.both)
- {
- //對方也添加自己為好友
- OnRosterItemEvent(new RosterItem()
- {
- //StrGroupName = item.GetGroups().Count > 0 ? item.GetGroups().Item(0).InnerXml : "",
- StrUserName = item.Jid.User,
- StrReMarkName = string.IsNullOrWhiteSpace(item.Name) == true ? item.Jid.User : item.Name,
- RosterType=PreType.Both
- });
- m_intCount = 0;
- }
- else if(item.Subscription==agsXMPP.protocol.iq.roster.SubscriptionType.remove)
- {
- //刪除(對方刪除自己)
- OnRosterItemEvent(new RosterItem()
- {
- //StrGroupName = item.GetGroups().Count > 0 ? item.GetGroups().Item(0).InnerXml : "",
- StrUserName = item.Jid.User,
- StrReMarkName = string.IsNullOrWhiteSpace(item.Name) == true ? item.Jid.User : item.Name,
- RosterType = PreType.ReMove
- });
- }
- }
6、收到Presence節(jié),觸發(fā)事件,進行列表展示
- /// <summary>
- /// 收到Presence節(jié)時引發(fā)的事件
- /// </summary>
- /// <param name="sender"></param>
- /// <param name="pres"></param>
- void xmppCon_OnPresence(object sender, Presence pres)
- {
- RosterItem rt = new RosterItem
- {
- StrFrom = pres.From.ToString(),
- StrUserName = pres.From.User,
- Server=pres.From.Server,
- Resource=pres.From.Resource,
- RosterType=(PreType)((int)pres.Type)
- };
-
- //if(pres.To==null)
- OnPresenceEvent(rt);
- }
至此,完成一個成功的登錄過程。
|