关闭

XMPPCLIENT

24人阅读 评论(0) 收藏 举报
using System;using System.Collections.Generic;using System.Linq;using System.Text;using ZYG.Alatin.Common;using ZYG.Alatin.Net.Xmpp;using ZYG.Alatin.Net.Xmpp.net;using ZYG.Alatin.Net.Xmpp.protocol.client;namespace ZYG.Alatin{ public class XmppClinet { private XmppClinet() { _xmppConn = new XmppClientConnection(); _xmppConn.SocketConnectionType = SocketConnectionType.Direct; _xmppConn.OnLogin += new ObjectHandler(_clientConn_OnLogin); _xmppConn.OnReadXml += new XmlHandler(_clientConn_OnReadXml); _xmppConn.OnWriteXml += new XmlHandler(_clientConn_OnWriteXml); _xmppConn.OnError += new ErrorHandler(_clientConn_OnError); _xmppConn.OnSocketError += new ErrorHandler(_clientConn_OnSocketError); //获取群消息 _xmppConn.OnRosterMsgItem += new MessageHandler(_clientConn_OnRosterMsgItem); _xmppConn.OnIq += new Net.Xmpp.protocol.client.IqHandler(_clientConn_OnIq); //_clientConn.OnMessage += new Net.Xmpp.protocol.client.MessageHandler(_clientConn_OnMessage); //获取个人消息 _xmppConn.OnMessageRec += new Net.Xmpp.protocol.client.MessageHandler(_clientConn_OnMessage); _xmppConn.OnStreamError += new XmppElementHandler(_clientConn_OnStreamError); //_clientConn.OnGroupMsgItem += new XmppClientConnection.GroupMsgHandler(_clientConn_OnGroupMsgItem); _xmppConn.OnOffLineRosterMsgItem += new XmppClientConnection.OffLineRosterMsgHandler(_clientConn_OnOffLineRosterMsgItem); _xmppConn.OnOffLineSingleMsgItem += new XmppClientConnection.OffLineSingleMsgHandler(_clientConn_OnOffLineSingleMsgItem); } private static XmppClinet _instance; private XmppClientConnection _xmppConn; private Jid jid = null;//xmpp连接相关信息对象 public static XmppClinet Instance { get { if (_instance == null) { _instance = new XmppClinet(); } return _instance; } } public void Initialize() { jid = new Jid(string.Format("{0}@{1}", "test4", "192.168.1.200")); jid.Password = "test4"; _xmppConn.Server = jid.Server; _xmppConn.Resource = jid.Resource; _xmppConn.Username = jid.User; _xmppConn.Password = jid.Password; _xmppConn.Resource = "MiniClent"; _xmppConn.Priority = 10; _xmppConn.Port = int.Parse("5222"); _xmppConn.UseSSL = false; _xmppConn.AutoResolveConnectServer = true; _xmppConn.UseCompression = false; _xmppConn.RegisterAccount = false; _xmppConn.UseStartTLS = false; // Caps _xmppConn.EnableCapabilities = true; _xmppConn.ClientVersion = "1.0"; _xmppConn.Capabilities.Node = "http://www.ag-software.de/miniclient/caps"; _xmppConn.KeepAlive = true; _xmppConn.AutoRoster = false; _xmppConn.AutoPresence = false; _xmppConn.Open(); } public void Dispose() { if (_xmppConn != null) { _xmppConn.Close(); } } //xmpp 连接成功时触发 private void _clientConn_OnLogin(object sender) { //whXmppServer.Set();//连接xmpp服务器成功. LogHelper.Debug("xmppClient连接成功"); } private void _clientConn_OnStreamError(object sender, Exception ex) { LogHelper.Error(ex); } //xmpp socket错误时触发 private void _clientConn_OnSocketError(object sender, Exception ex) { LogHelper.Error(ex); } //xmpp 连接错误时触发 private void _clientConn_OnError(object sender, Exception ex) { LogHelper.Error(ex); } //接收xml信令时触发 private void _clientConn_OnReadXml(object sender, string xml) { LogHelper.Debug(">>>>>Received:" + xml); } //发送xml信令时触发 private void _clientConn_OnWriteXml(object sender, string xml) { LogHelper.Debug(">>>>>Send:" + xml); } // 接收到讨论组消息时触发 在没有打开群聊、讨论组窗口时接收 private void _clientConn_OnRosterMsgItem(object sender, Message msg) { var type = msg.Subject; if (type != null && type.Contains("sending")) //正在接收发送过来的图片,这个不需要解析 return; } // 接收到单聊消息触发 在没有打开单聊窗口时接收 private void _clientConn_OnMessage(object sender, Message msg) { var type = msg.Subject; if (type != null && type.Contains("sending")) //正在接收发送过来的图片,这个不需要解析 return; } //当网络断开时回调的这个方法 private void _clientConn_OnClose(object sender) { LogHelper.Debug("OffLine"); } //xmpp IO读写错误产生触发 private void _clientConn_OnStreamError(object sender, Net.Xmpp.Xml.Dom.Element ex) { if (ex.InnerXml.Contains("conflict")) { //_pushOffine = true; //MessageBox.Show("您的账号在其他地方登陆,本机已下线。"); } LogHelper.Error(ex.ToString()); } //创建讨论组成功后激活讨论组 private void _clientConn_OnActiveGroupMember(object sender, IQ iq) { } //收到离线单聊消息时触发 private void _clientConn_OnOffLineSingleMsgItem(object sender, Message msg) { //主窗口未打开时调用写数据库方法,打开时调用主窗体方法,这样能保证信息不会丢失 var type = msg.Subject; if (type != null && type.Contains("sending")) //正在接收发送过来的图片,这个不需要解析 return; } //收到离线群消息时触发 private void _clientConn_OnOffLineRosterMsgItem(object sender, Message msg) { var type = msg.Subject; if (type != null && type.Contains("sending")) //正在接收发送过来的图片,这个不需要解析 return; //主窗口未打开时调用写数据库方法,打开时调用主窗体方法,这样能保证信息不会丢失 } //接收到IQ信息时触发 private void _clientConn_OnIq(object sender, Net.Xmpp.protocol.client.IQ iq) { if (iq.Error != null) { LogHelper.Error(iq.Error.ToString()); return; } if (iq.Media != null) { string jsonStr = iq.Media.GetTag("content"); } } }}
0
0

查看评论
* 以上用户言论只代表其个人观点,不代表CSDN网站的观点或立场
    个人资料
    • 访问:82次
    • 积分:20
    • 等级:
    • 排名:千里之外
    • 原创:2篇
    • 转载:0篇
    • 译文:0篇
    • 评论:0条
    文章存档
    阅读排行
    评论排行