📄 xmppserverconnection.cs
字号:
using System;
using System.IO;
using System.Text;
using System.Threading;
using System.Net;
using System.Net.Sockets;
using server;
using agsXMPP.protocol;
using agsXMPP.protocol.iq;
using agsXMPP.protocol.iq.auth;
using agsXMPP.protocol.iq.roster;
using agsXMPP.protocol.iq.browse;
using agsXMPP.protocol.client;
using agsXMPP.protocol.x;
using agsXMPP.Xml;
using agsXMPP.Xml.Dom;
using System.Windows.Forms;
using System.Data;
namespace agsXMPP
{
public class XmppSeverConnection
{
#region << Constructors >>
public XmppSeverConnection()
{
streamParser = new StreamParser();
streamParser.OnStreamStart += new StreamHandler(streamParser_OnStreamStart);
streamParser.OnStreamEnd += new StreamHandler(streamParser_OnStreamEnd);
streamParser.OnStreamElement += new StreamHandler(streamParser_OnStreamElement);
}
public XmppSeverConnection(serverfrm a, Socket sock)
: this()
{
m_Sock = sock;
frm = a;
m_Sock.BeginReceive(buffer, 0, BUFFERSIZE, 0, new AsyncCallback(ReadCallback), null);
}
#endregion
private StreamParser streamParser;
public Socket m_Sock;
private const int BUFFERSIZE = 1024;
private byte[] buffer = new byte[BUFFERSIZE];
private serverfrm frm;
public Jid jid;
public delegate void mydelegate(string str);
public void ReadCallback(IAsyncResult ar)
{
try
{
int bytesRead = m_Sock.EndReceive(ar);
if (bytesRead > 0)
{
streamParser.Push(buffer, 0, bytesRead);
m_Sock.BeginReceive(buffer, 0, BUFFERSIZE, 0, new AsyncCallback(ReadCallback), null);
}
else
{
m_Sock.Shutdown(SocketShutdown.Both);
m_Sock.Close();
}
}
catch
{
deluser();
}
}
public void Send(string data)
{
try
{
byte[] byteData = Encoding.UTF8.GetBytes(data);
//发送数据给客户端
frm.BeginInvoke(new mydelegate(frm.ShowSendMessage), new Object[] { data });
m_Sock.BeginSend(byteData, 0, byteData.Length, 0, new AsyncCallback(SendCallback), null);
}
catch
{ }
}
public void SendCallback(IAsyncResult ar)
{
try
{
int bytesSent = m_Sock.EndSend(ar);
Console.WriteLine("Sent {0} bytes to client.", bytesSent);
}
catch (Exception e)
{
Console.WriteLine(e.ToString());
}
}
public void Stop()
{
Send("</stream:stream>");
m_Sock.Shutdown(SocketShutdown.Both);
m_Sock.Close();
}
#region << Properties and Member Variables >>
// private int m_Port = 5222;
private string m_SessionId = null;
public string SessionId
{
get
{
return m_SessionId;
}
set
{
m_SessionId = value;
}
}
#endregion
private void streamParser_OnStreamStart(object sender, Node e)
{
SendOpenStream();
}
private void streamParser_OnStreamEnd(object sender, Node e)
{
deluser();
}
private void deluser()
{
frm.listViewOnlineUser.Items.Remove(frm.listViewOnlineUser.FindItemWithText(jid.User));
foreach (XmppSeverConnection con in Online.onlineuser)
{
if (con.jid.User == jid.User)
{
Online.onlineuser.Remove(con);
break;
}
}
Presence pre;
pre = new Presence();
pre.Type = PresenceType.unavailable;
pre.From = jid;
try
{
foreach (XmppSeverConnection con in Online.onlineuser)
{
con.Send(pre);
}
}
catch
{ }
}
private void streamParser_OnStreamElement(object sender, Node e)
{
frm.BeginInvoke(new mydelegate(frm.ShowRecvMessage), new Object[] { e.ToString() });
Console.WriteLine("OnStreamElement: " + e.ToString());
if (e.GetType() == typeof(Presence))
{
Presence pres=e as Presence;
//处理会议室被取消消息
if(pres.Type==PresenceType.invisible)
{
foreach(Meeting meeting in Online.onlinemeeting)
{
if (meeting.jid.Equals(pres.To))
{
if (this.jid.User == pres.To.Resource)
{
Online.onlinemeeting.Remove(meeting);
pres.From = pres.To;
foreach (XmppSeverConnection con in Online.onlineuser)
{
pres.To = con.jid;
con.Send(pres);
}
break;
}
}
}
}
//处理用户上线消息
if (pres.Show == ShowType.chat)
{
pres.From = this.jid;
foreach (XmppSeverConnection con in Online.onlineuser)
{
if (con.jid.User != this.jid.User)
{
pres.To = con.jid;
con.Send(pres);
}
}
}
//处理用户退出会议消息
if (pres.Type == PresenceType.unsubscribe)
{
foreach (Meeting meeting in Online.onlinemeeting)
{
if (pres.To.Equals(meeting.jid))
{
meeting.userinmeeting.Remove(this.jid);
pres.From = this.jid;
foreach (Jid jd2 in meeting.userinmeeting)
{
foreach (XmppSeverConnection con in Online.onlineuser)
{
if (jd2.Equals(con.jid))
{
con.Send(pres);
break;
}
}
}
break;
}
}
}
}
else if (e.GetType() == typeof(agsXMPP.protocol.client.Message))
{
agsXMPP.protocol.client.Message msg = e as agsXMPP.protocol.client.Message;
//路由点对点聊天消息
if (msg.Type == MessageType.chat)
{
foreach (XmppSeverConnection con in Online.onlineuser)
{
if (con.jid.User == msg.To.User)
{
msg.From = jid;
con.Send(msg);
}
}
}
//路由群聊消息
if (msg.Type == MessageType.groupchat)
{
foreach (Meeting meeting in Online.onlinemeeting)
{
if (msg.To.Equals(meeting.jid))
{
msg.From = this.jid;
foreach (Jid jd2 in meeting.userinmeeting)
{
foreach (XmppSeverConnection con in Online.onlineuser)
{
if (jd2.Equals(con.jid))
{
con.Send(msg);
break;
}
}
}
break;
}
}
}
}
else if (e.GetType() == typeof(IQ))
{
ProcessIQ(e as IQ);
}
}
⌨️ 快捷键说明
复制代码
Ctrl + C
搜索代码
Ctrl + F
全屏模式
F11
切换主题
Ctrl + Shift + D
显示快捷键
?
增大字号
Ctrl + =
减小字号
Ctrl + -