实例介绍
【实例截图】
【核心代码】
using System;
using System.Collections.Generic;
using System.ComponentModel;
using System.Data;
using System.Drawing;
using System.Text;
using System.Windows.Forms;
using System.Threading;
using agsXMPP;
using agsXMPP.protocol.client;
using agsXMPP.protocol.iq.browse;
using agsXMPP.Xml.Dom;
using agsXMPP.protocol.iq.rpc;
using agsXMPP.protocol.iq.oob;
using agsXMPP.protocol.iq.last;
using System.IO;
namespace xmpp_client
{
public partial class FormMain : Form
{
private XmppClientConnection con = new XmppClientConnection();
private Form1 login;
delegate void OnPresenceDelegate(object sender, Presence pres);
private string name = string.Empty;//选中的联系人
delegate void dosomething();
delegate void OnMessageDelegate(object sender, agsXMPP.protocol.client.Message msg);
public FormMain()
{
InitializeComponent();
//登录事件
con.OnLogin = new ObjectHandler(con_OnLogin);
//验证事件
con.OnAuthError = new OnXmppErrorHandler(con_OnAuthError);
//消息处理
con.OnMessage = new XmppClientConnection.MessageHandler(con_OnMessage);
//出息消息
con.OnPresence = new XmppClientConnection.PresenceHandler(con_OnPresence);
//出错
con.OnError = new ErrorHandler(con_OnError);
//请求事件
con.OnIq = new agsXMPP.Xml.StreamHandler(con_OnIq);
}
//连接服务器失败的事件
void con_OnError(object sender, Exception ex)
{
YLSystemBase.BRMessage.ShowMessage(2, "系统提示", "连接服务器失败!");
Thread.Sleep(2000);
System.Diagnostics.Process.Start("xmpp_client.exe");
Application.Exit();
}
#region(废物)
//打开连接
#endregion
//调用登录窗体的构造函数
private void main_form()
{
login = new Form1(con);
if (login.ShowDialog() == DialogResult.OK)
{
Thread mythread = new Thread(connect);
mythread.Start();
mythread.IsBackground = true;
}
else
{
this.Close();
Application.Exit();
}
}
void connect()
{
con.Open();
}
//登录事件
void con_OnLogin(object sender)
{
con.Show = ShowType.chat;
con.SendMyPresence();
Control.CheckForIllegalCrossThreadCalls = false;
this.Text = con.Username;
YLSystemBase.BRMessage.ShowMessage(4, "系统提示", "登录成功");
}
//在席事件(用来处理用户上线下线)
void con_OnPresence(object sender, Presence pres)
{
if (InvokeRequired)
{
BeginInvoke(new OnPresenceDelegate(con_OnPresence), new object[] { sender, pres });
return;
}
if (pres.Show == ShowType.chat)
{
if(pres.From.User != con.MyJID.User)
listBox1.Items.Add(pres.From.User);
}
else if (pres.Type == PresenceType.unavailable)
{
listBox1.Items.Remove(pres.To.User);
//处理下线
//YLSystemBase.BRMessage.ShowMessage(3,"系统提示","服务器已关闭,程序将在3秒内退出");
//this.Invoke(new dosomething(delegate()
// {
// Thread.Sleep(3000);
// }));
//Application.Exit();
}
}
//消息处理事件
void con_OnMessage(object sender, agsXMPP.protocol.client.Message msg)
{
if (InvokeRequired)
{
BeginInvoke(new OnMessageDelegate(con_OnMessage), new object[] { sender, msg });
return;
}
//处理用户点对点聊天信息
if (msg.Type == MessageType.chat)
{
Control.CheckForIllegalCrossThreadCalls = false;
richTextBox1.SelectionColor = Color.Red;
richTextBox1.AppendText(msg.From.User "" DateTime.Now.ToLongTimeString() "\r\n");
richTextBox1.SelectionColor = Color.Black;
richTextBox1.AppendText(msg.Body "\r\n");
}
else if (msg.Type == MessageType.groupchat)
{
if (msg.Body == "[SerClose]")
{
Presence p = new Presence();
p.Type = PresenceType.unavailable;
con.Send(p);
YLSystemBase.BRMessage.ShowMessage(5,"系统提示","服务器已经关闭,程序将在3秒后自动退出");
Thread.Sleep(3000);
Application.Exit();
}
else
{
Control.CheckForIllegalCrossThreadCalls = false;
richTextBox1.SelectionColor = Color.Red;
richTextBox1.AppendText(msg.From.User " " DateTime.Now.ToLongTimeString() "\r\n");
richTextBox1.SelectionColor = Color.Black;
richTextBox1.AppendText(msg.Body "\r\n");
}
}
//文件传输
//else if (msg.Type == MessageType.normal)
//{
// Control.CheckForIllegalCrossThreadCalls = false;
// string s = msg.Body;
// string fn = s.Substring(0, s.IndexOf("[filename]"));
// string shiti = s.Substring(s.LastIndexOf(']') 1);
// string hz = s.Substring(s.IndexOf("[filename]") 10, s.Length - fn.Length - shiti.Length - 18);
// FileStream fs = new FileStream("../../" (fn hz), FileMode.Create);
// StreamWriter sw = new StreamWriter(fs);
// sw.Write(shiti);
// sw.Close();
// fs.Close();
// YLSystemBase.BRMessage.ShowMessage(5, "文件传输", fn "接收完毕");
//}
else if (msg.Type == MessageType.headline)
{
YLSystemBase.BRMessage.ShowMessage(5, "系统提示", "服务器以关闭,请退出程序");
Application.Exit();
}
else
{
YLSystemBase.BRMessage.ShowMessage(3, "系统消息", msg.Body);
}
}
//请求事件
void con_OnIq(object sender, Node e)
{
}
//验证出错
void con_OnAuthError(object sender, Element e)
{
YLSystemBase.BRMessage.ShowMessage(3, "系统提示", "系统错误");
}
//窗体加载事件
private void FormMain_Load(object sender, EventArgs e)
{
richTextBox1.ReadOnly = true;
//textBox1.ReadOnly = true;
}
//点击登录按钮
private void button1_Click(object sender, EventArgs e)
{
main_form();
}
//发送消息
private void button2_Click(object sender, EventArgs e)
{
agsXMPP.protocol.client.Message msg = new agsXMPP.protocol.client.Message();
if (listBox1.SelectedIndex > 0)
{
msg.Type = MessageType.chat;
msg.Body = richTextBox2.Text;
msg.From = new Jid(con.Username, "localhost", "resource");
msg.To = new Jid(name, "localhost", con.Username);
con.Send(msg);
richTextBox2.Text = "";
}
else
{
msg.Type = MessageType.groupchat;
msg.Body = richTextBox2.Text;
msg.From = new Jid(con.Username, "localhost", "resource");
msg.To = new Jid(con.Server, "localhost", con.Username);
con.Send(msg);
richTextBox2.Text = "";
}
}
//滚动条定位
private void richTextBox1_TextChanged(object sender, EventArgs e)
{
this.richTextBox1.ScrollToCaret();
}
//选中联系人
private void listBox1_Click(object sender, EventArgs e)
{
if (listBox1.SelectedIndex > -1)
{
name = listBox1.SelectedItem.ToString();
YLSystemBase.BRMessage.ShowMessage(2, "选中联系人", name);
}
}
//取消选中
private void 群聊ToolStripMenuItem_Click(object sender, EventArgs e)
{
listBox1.SelectedIndex = -1;
}
private void FormMain_FormClosing(object sender, FormClosingEventArgs e)
{
if (login != null)
changestate(login.jid, 1);
}
private void changestate(string jid, int visible)
{
Presence p = new Presence();
if (jid != null)
p.To = new Jid(jid);
if (visible == 0)//完全上线
{
p.Type = PresenceType.available;
p.Show = ShowType.chat;
}
else if (visible == 1)
{
p.Type = PresenceType.unavailable;//下线
}
con.Send(p);
}
//发送文件
private void button3_Click(object sender, EventArgs e)
{
if (listBox1.SelectedIndex < 0) return;
OpenFileDialog ofd = new OpenFileDialog();
if (ofd.ShowDialog() != DialogResult.OK) return;
string hz = Path.GetExtension(ofd.FileName);
string filename = Path.GetFileNameWithoutExtension(ofd.FileName);
agsXMPP.protocol.client.Message msg = new agsXMPP.protocol.client.Message();
if (listBox1.SelectedIndex >= 0)
{
msg.Type = MessageType.normal;
FileStream fs = new FileStream(ofd.FileName, FileMode.Open);
byte[] buffer = new byte[fs.Length];
fs.Read(buffer, 0, (int)fs.Length);
msg.Body = filename "[filename]" hz "[filehz]" Encoding.Default.GetString(buffer, 0, buffer.Length);
msg.From = new Jid(con.Username, "localhost", "resource");//发送方
msg.To = new Jid(name, "localhost", con.Username);//接收方
con.Send(msg);
fs.Close();
}
}
}
}
标签: XMPP
小贴士
感谢您为本站写下的评论,您的评论对其它用户来说具有重要的参考价值,所以请认真填写。
- 类似“顶”、“沙发”之类没有营养的文字,对勤劳贡献的楼主来说是令人沮丧的反馈信息。
- 相信您也不想看到一排文字/表情墙,所以请不要反馈意义不大的重复字符,也请尽量不要纯表情的回复。
- 提问之前请再仔细看一遍楼主的说明,或许是您遗漏了。
- 请勿到处挖坑绊人、招贴广告。既占空间让人厌烦,又没人会搭理,于人于己都无利。
关于好例子网
本站旨在为广大IT学习爱好者提供一个非营利性互相学习交流分享平台。本站所有资源都可以被免费获取学习研究。本站资源来自网友分享,对搜索内容的合法性不具有预见性、识别性、控制性,仅供学习研究,请务必在下载后24小时内给予删除,不得用于其他任何用途,否则后果自负。基于互联网的特殊性,平台无法对用户传输的作品、信息、内容的权属或合法性、安全性、合规性、真实性、科学性、完整权、有效性等进行实质审查;无论平台是否已进行审查,用户均应自行承担因其传输的作品、信息、内容而可能或已经产生的侵权或权属纠纷等法律责任。本站所有资源不代表本站的观点或立场,基于网友分享,根据中国法律《信息网络传播权保护条例》第二十二与二十三条之规定,若资源存在侵权或相关问题请联系本站客服人员,点此联系我们。关于更多版权及免责申明参见 版权及免责申明


网友评论
我要评论