基本信息
源码名称:xmpp在线聊天示例源码(IM,完整可运行,含服务端)
源码大小:0.48M
文件格式:.rar
开发语言:C#
更新时间:2019-04-26
友情提示:(无需注册或充值,赞助后即可获取资源下载链接)
嘿,亲!知识可是无价之宝呢,但咱这精心整理的资料也耗费了不少心血呀。小小地破费一下,绝对物超所值哦!如有下载和支付问题,请联系我们QQ(微信同号):78630559
本次赞助数额为: 5 元×
微信扫码支付:5 元
×
请留下您的邮箱,我们将在2小时内将文件发到您的邮箱
源码介绍
可以发送在线消息,发送文件,显示在线人员列表等等
可以发送在线消息,发送文件,显示在线人员列表等等
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();
}
}
}
}