日韩性视频-久久久蜜桃-www中文字幕-在线中文字幕av-亚洲欧美一区二区三区四区-撸久久-香蕉视频一区-久久无码精品丰满人妻-国产高潮av-激情福利社-日韩av网址大全-国产精品久久999-日本五十路在线-性欧美在线-久久99精品波多结衣一区-男女午夜免费视频-黑人极品ⅴideos精品欧美棵-人人妻人人澡人人爽精品欧美一区-日韩一区在线看-欧美a级在线免费观看

歡迎訪問 生活随笔!

生活随笔

當前位置: 首頁 > 编程语言 > C# >内容正文

C#

C#连接基于Java开发IM——Openfire

發布時間:2023/12/10 C# 39 豆豆
生活随笔 收集整理的這篇文章主要介紹了 C#连接基于Java开发IM——Openfire 小編覺得挺不錯的,現在分享給大家,幫大家做個參考.

Openfire簡介

?? Openfire 是開源的、基于可拓展通訊和表示協議(XMPP)、采用Java編程語言開發的實時協作服務器。Openfire的效率很高,單臺服務器可支持上萬并發用戶。
??Server和Client端的通信都用xml文檔的形式進行通信。
但是Openfire是Java語言寫的,對于C#的dll拓展庫相比與java的jar包少的可憐,在網上尋找一番之后找到了一個比較好的dll拓展庫,agsxmpp是一個專門為C#連接xmpp協議下即時通訊已經搭建xmpp協議服務端的的dll,同時他有商業版MatriX,博主窮學生一個,沒有啥錢去購買商業版,還是采用了普通的agsxmpp。

AgsXmpp簡介

??agsxmpp是AG—Software進行開發的一個開源項目,可以在它的官網進行下載源碼。
??agsxmpp是在2003年開始研發,2008年發布它的最后一個版本,因此它在兼容性上顯然是不很好的。
??同時在C#連接Openfire上,agsxmpp中有一個巨坑,加上網上關于agsxmpp的開發文檔奇少,而且博主沒有在官網上找到相關的開發文檔(就算有也是全英文看不懂系列),故記下開發全過程。
??因為agsxmpp并不是專門為Openfire制作的,而是對任何以xmpp協議的即時通訊進行連接等服務。如果不對源碼進行一定的重寫,在某些情況下會出現一些問題。
??如果你直接使用 agsxmpp.dllXmppClientConnection 類進行連接,就算你代碼毫無錯誤,也無法正常連接Openfire,因為
博主只是對源碼改了一句話,即可正常連接。
修改 protocolsasl 下的 Mechanism.cs 中源碼,將

case "DIGEST-MD5":return MechanismType.DIGEST_MD5;

注釋,因為 openfire 發送數據流 是通過 PLAIN 的 , 而 agsxmpp 是默認是 通過DIGEST-MD5 發送。
??同時,在agsxmpp中,還有一個地方表現了對openfire的不兼容,openfire 發送iq節 不接收 to屬性,因此還需要修改一個地方
源代碼如下

public IQ SendIq(agsXMPP.protocol.client.IQ iq, int timeout) {synchronousResponse = null;AutoResetEvent are = new AutoResetEvent(false);SendIq(iq, new IqCB(SynchronousIqResult), are);if (!are.WaitOne(timeout, true)){// Timed outlock (m_grabbing){if (m_grabbing.ContainsKey(iq.Id))m_grabbing.Remove(iq.Id);}return null;}return synchronousResponse; }修改后如下 public void SendIq(IQ iq, IqCB cb, object cbArg) {// check if the callback is null, in case of wrong usage of this classif (cb != null){TrackerData td = new TrackerData();td.cb = cb;td.data = cbArg;m_grabbing[iq.Id] = td;//iq在agsxmpp中發送Iq節的時候先iq.RemoveAttribute("to")iq.RemoveAttribute("to");}m_connection.Send(iq); }public void SendIq2(IQ iq, IqCB cb, object cbArg) {// check if the callback is null, in case of wrong usage of this classif (cb != null){TrackerData td = new TrackerData();td.cb = cb;td.data = cbArg;m_grabbing[iq.Id] = td;//iq在agsxmpp中發送Iq節的時候先iq.RemoveAttribute("to")//iq.RemoveAttribute("to");}m_connection.Send(iq); }

??登錄操作:發送xml消息用 SendIq() 方法
??其他操作:發送xml消息用 SendIq2() 方法

連接上Openfire

官方提供了一個只有三行代碼的小型Demo

XmppClientConnection xmpp = new XmppClientConnection(server); xmpp.Open(username,secret); xmpp.OnLogin+=delegate(object o){xmpp.Send(new Message(JID,MessageType.chat,msg));};

我的代碼

public class XmppLogin{private XmppClientConnection xmppCon;private bool isSSL;/// <summary>/// 是否使用加密連接/// </summary>public bool IsSSL { get { return isSSL; } set { isSSL = value; } }private string userName;private string server;public string Server { get { return server; } set { server = value; } }/// <summary>/// 用戶名/// </summary>public string UserName { get { return userName; } set { userName = value; } }private string passWord;/// <summary>/// 密碼/// </summary>public string PassWord { get { return passWord; } set { passWord = value; } }private string clientVersion;/// <summary>/// 客戶端版本/// </summary>public string ClientVersion { get { return clientVersion; }set { clientVersion = value; } }/// <summary>/// 登錄狀態/// </summary>public string LoginState { get { return xmppCon.XmppConnectionState.ToString(); } }private int port;/// <summary>/// 登錄端口,通常是5222,加密時是5223/// </summary>public int Port { get { return port; }set{ port = value;} }public XmppLogin(){xmppCon = new XmppClientConnection();}#region 傳遞一個XmppClient對象/// <summary>/// 傳遞一個XmppClient對象/// </summary>/// <param name="con">需要操作的具體實例</param>public XmppLogin(XmppClientConnection con){xmppCon = new XmppClientConnection();xmppCon = con;}#endregion#region 登錄/// <summary>/// 登錄openfire的方法/// </summary>/// <returns>返回值為是否登錄</returns>public void Login(){xmppCon.Server = server;xmppCon.UseSSL = false;xmppCon.Port = 5222;xmppCon.AutoResolveConnectServer = true;xmppCon.UseCompression = false;xmppCon.EnableCapabilities = true;xmppCon.ClientVersion = "1.0";xmppCon.Capabilities.Node = "http://www.ag-software.de/miniclient/caps";xmppCon.DiscoInfo.AddIdentity(new DiscoIdentity("pc", "MyClient", "client"));xmppCon.DiscoInfo.AddFeature(new DiscoFeature(agsXMPP.Uri.DISCO_INFO));xmppCon.DiscoInfo.AddFeature(new DiscoFeature(agsXMPP.Uri.DISCO_ITEMS));xmppCon.DiscoInfo.AddFeature(new DiscoFeature(agsXMPP.Uri.MUC));xmppCon.Open(userName,passWord);//xmppCon.OnLogin += delegate (object o) { xmppCon.Send(new agsXMPP.protocol.client.Message("testa@118.89.48.159", MessageType.chat, "sdgo")); };}#endregion#region 測試連接/// <summary>/// 測試指定的OpenFire服務器和端口是否能連通/// </summary>/// <returns>返回是否能連通</returns>public bool TestPing(){string ipAddress = Server;int portNum = port;bool CanConnect = false;IPAddress ip = IPAddress.Parse(ipAddress);try{IPEndPoint point = new IPEndPoint(ip, portNum);using (Socket sock = new Socket(AddressFamily.InterNetwork, SocketType.Stream, ProtocolType.Tcp)){sock.Connect(point);CanConnect = sock.Connected;sock.Close();return CanConnect;}}catch (SocketException e){//LOG TODOreturn false;}}#endregionpublic static implicit operator XmppClientConnection(XmppLogin v){return v.xmppCon;}}

??至此,Openfire連接成功。
??最近忙而且也剛開始弄這個,過幾天更新一下XmppConnection下各種屬性、事件、函數的具體用法。

我的掘金:WarrenRyan

我的簡書:WarrenRyan

歡迎關注我的博客獲得第一時間更新 https://blog.tity.online

我的Github:StevenEco

我的博客園:WarrenRyan

轉載于:https://www.cnblogs.com/WarrenRyan/p/10406535.html

總結

以上是生活随笔為你收集整理的C#连接基于Java开发IM——Openfire的全部內容,希望文章能夠幫你解決所遇到的問題。

如果覺得生活随笔網站內容還不錯,歡迎將生活随笔推薦給好友。