📄 presencemanager.cs
字号:
using System;
using System.IO;
using System.Xml;
using System.Threading;
using System.Collections;
using gowk.core.packets;
using gowk.net.Sockets;
using gowk.net;
namespace gowk.core.managers
{
/// <summary>
/// unavailable -- Signals that the entity is no longer available for communication.
/// subscribe -- The sender wishes to subscribe to the recipient's presence.
/// subscribed -- The sender has allowed the recipient to receive their presence.
/// unsubscribe -- The sender is unsubscribing from another entity's presence.
/// unsubscribed -- The subscription request has been denied or a previously-granted subscription has been cancelled.
/// probe -- A request for an entity's current presence; SHOULD be generated only by a server on behalf of a user.
/// error -- An error has occurred regarding processing or delivery of a previously-sent presence stanza.
/// </summary>
// public enum SubscribeType{unavailable,subscribe,subscribed,unsubscribe,unsubscribed,probe,error}
/// <summary>
/// away -- The entity or resource is temporarily away.
///chat -- The entity or resource is actively interested in chatting.
///dnd -- The entity or resource is busy (dnd = "Do Not Disturb").
///xa -- The entity or resource is away for an extended period (xa = "eXtended Away").
/// </summary>
/// <summary>
/// MessageManager 的摘要说明。
/// </summary>
public class PresenceManager:ManagerBase
{
#region field & event
public event JabberEventHandler
StatusChanaged,
Subcribe,
Subscribed,Unsubscribe,Unsubscribed;
Presence _myPresence;
#endregion
#region property
public Presence MyPresence
{
get{return this._myPresence;}
set{this._myPresence=value;}
}
#endregion
private System.Collections.Hashtable onlineUsers;
public PresenceManager():base()
{
this.onlineUsers=new System.Collections.Hashtable();
InitializeMyPresence();
}
public PresenceManager(JabberClient jc):base(jc)
{
this.onlineUsers=new System.Collections.Hashtable();
InitializeMyPresence();
// jc.StateChange+=new EventHandler(jc_StateChange);
}
private void InitializeMyPresence()
{
this._myPresence=new Presence();
this._myPresence.Show=Show.chat;
}
protected internal override bool OnMessage(Packet pack)
{
if(!(pack is Presence))return false;
if(pack.Type==PacketType.subscribe)
{
if(this.Subcribe!=null)
this.Invoke(this.Subcribe,new object[]{new JabberEventArgs(pack)});
}
else if(pack.Type==PacketType.subscribed)
{
if(this.Subscribed!=null)
this.Invoke(this.Subscribed,new object[]{new JabberEventArgs(pack)});
}
else if(pack.Type==PacketType.unsubscribe)
{
if(this.Unsubscribe!=null)
this.Invoke(this.Unsubscribe,new object[]{new JabberEventArgs(pack)});
}
else if(pack.Type==PacketType.unsubscribed)
{
if(this.Unsubscribed!=null)
this.Invoke(this.Unsubscribed,new object[]{new JabberEventArgs(pack)});
}
else
{
string jid=Jid.GetBareJid(pack.From);
if(pack.Type==PacketType.available ||pack.Type==null)
{
if(this.onlineUsers.Contains(jid))
{
this.onlineUsers[jid]=pack;
}
else
{
this.onlineUsers.Add(jid,pack);
}
}
else if(pack.Type==PacketType.unavailable)
{
if(this.onlineUsers.Contains(jid))this.onlineUsers.Remove(jid);
}
if(this.StatusChanaged!=null)
this.Invoke(this.StatusChanaged,new object[]{new JabberEventArgs(pack)});
}
return true;
}
public System.Collections.Hashtable OnlineUsers
{
get{return this.onlineUsers;}
}
public void DoGoOnline()
{
this.MyPresence.Type=null;
this.UpdatePresence();
}
public void DoGoOffline()
{
this.MyPresence.Type=PacketType.unavailable;
this.UpdatePresence();
}
public void DoInvisible()
{
this.MyPresence.Type=PacketType.invisible;
this.UpdatePresence();
}
public void DoChangeStatus(string status,string show)
{
this.MyPresence.Status=status;
this.MyPresence.Show=show;
this.UpdatePresence();
}
public void UpdatePresence()
{
this.DoSendPresence(this.MyPresence);
}
public void Presence(JabberCallBack dlgt,int timeout)
{
this.DoSendPresence(this.MyPresence,dlgt,timeout);
}
public void DoSendPresence(Presence p)
{
this.Send(p);
}
public void DoSendPresence(Presence p,JabberCallBack dlgt,int timeout)
{
this.Send(p,dlgt,timeout);
}
/// <summary>
/// <presence to='contact@example.org' type='subscribe'/>
/// </summary>
/// <param name="jid"></param>
public void DoSubcribe(string jid,string ps,JabberCallBack dlgt,int timeout)
{
Presence p=new Presence();
p.Type=PacketType.subscribe;
p.To=jid;
p.Status=ps;
this.DoSendPresence(p,dlgt,timeout);
}
public void DoSubcribed(string jid,JabberCallBack dlgt,int timeout)
{
Presence p=new Presence();
p.Type=PacketType.subscribed;
p.To=jid;
this.DoSendPresence(p,dlgt,timeout);
}
public void DoUnsubcribe(string jid,JabberCallBack dlgt,int timeout)
{
Presence p=new Presence();
p.Type=PacketType.unsubscribed;
p.To=jid;
this.DoSendPresence(p,dlgt,timeout);
}
public void DoUnsubcribed(string jid,JabberCallBack dlgt,int timeout)
{
Presence p=new Presence();
p.Type=PacketType.unsubscribed;
p.To=jid;
this.DoSendPresence(p,dlgt,timeout);
}
/* private void jc_StateChange(object sender, EventArgs e)
{
if(this.JabberClient.State==State.Logined)
{
this.UpdatePresence();
}
}*/
}
}
⌨️ 快捷键说明
复制代码
Ctrl + C
搜索代码
Ctrl + F
全屏模式
F11
切换主题
Ctrl + Shift + D
显示快捷键
?
增大字号
Ctrl + =
减小字号
Ctrl + -