xmpp開始使用xmpp


備註

可擴展消息傳遞和存在協議(XMPP)是一種網絡協議,它使用XML以近實時方式在兩個或多個網絡連接實體之間交換結構化數據。創建XMPP是為了滿足IETF的即時消息和狀態協議( RFC 2779 )指南,但其目的遠遠超出了IM。它還用作面向消息的中間件,用於機器對機器(M2M)通信和物聯網(IoT)。

輕量級XMPP核心協議為用戶提供

  • 強認證
  • 全球地址
  • 用於數據交換的結構化和可擴展格式

可擴展的方法使得在XMPP核心之上構建自定義協議成為可能。

核心XMPP協議在RFC 6120中定義,由Internet工程任務組(XMPP)管理。即時消息傳遞擴展在RFC 6121中定義,第三文檔( RFC 7622 )定義XMPP地址的格式,也稱為“Jabber標識符”(JID)。其他功能以XMPP擴展協議 (XEP)的形式指定, XMPP擴展協議由社區創建並由XMPP標準基金會 (XSF)維護。

版本

筆記發布日期
1.0 核心: RFC 6120 ,IM: RFC 6121 ,地址: RFC 7622 2011-03-01
0.9 核心: RFC 3920 ,IM: RFC 3921 ,地址: RFC 6122 2004-10-01

連接和發送消息

SleekXMPP(Python)

import sleekxmpp

client = sleekxmpp.Client("address@example.net", "password")
client.connect()
client.process(blocking=False)
client.send_message(mto="remote@example.net", mbody=self.msg)
 

Smack(Java / Android)

XMPPTCPConnection connection = new XMPPTCPConnection("user", "password", "example.org")
connection.connect().login();
Message message = new Message("otheruser@example.net", "Hi, how are you?");
connection.sendStanza(message);
connection.disconnect();
 

使用agsxmpp庫創建Xmpp客戶端連接

public void OpenXmppConnection(int port, bool useSsl, string serverJid, string userName, string password)
        {
            try
            {
                _xmppClientConnection.AutoResolveConnectServer = true;
                _xmppClientConnection.Port = port;
                _xmppClientConnection.UseSSL = useSsl;
                _xmppClientConnection.Server = serverJid;
                _xmppClientConnection.Username = userName;
                _xmppClientConnection.Password = password;
                _xmppClientConnection.Resource = "web";


                //authenticate and open connection with server
                _xmppClientConnection.Open();
            }
            catch (Exception ex)
            {
            }
        }
 

創建聊天會話並發送消息

Smack(Java)

  • 使用Smack 4.1
  • 建議在項目中包含Smack作為Maven依賴項(例如,使用gradle或Maven)。
  • 另外,必須手動將以下Smack工件/ jar添加到類路徑中:smack-core,smack-extensions,smack-experimental,smack-im,smnack-tcp,smack-java7
import org.jivesoftware.smack.ConnectionConfiguration.SecurityMode;
import org.jivesoftware.smack.SmackException;
import org.jivesoftware.smack.XMPPException;
import org.jivesoftware.smack.chat.Chat;
import org.jivesoftware.smack.chat.ChatManager;
import org.jivesoftware.smack.chat.ChatMessageListener;
import org.jivesoftware.smack.packet.Message;
import org.jivesoftware.smack.packet.Presence;
import org.jivesoftware.smack.tcp.XMPPTCPConnection;
import org.jivesoftware.smack.tcp.XMPPTCPConnectionConfiguration;

public void sendMessage() {

XMPPTCPConnectionConfiguration config = 
  XMPPTCPConnectionConfiguration.builder()
            .setServiceName("mydomain.local")
            .setHost("127.0.0.1")
            .setPort(5222)
            .build();

XMPPTCPConnection connection = new XMPPTCPConnection(config);

connection.connect();
connection.login("test1", "test1pwd"); 

ChatManager chatManager = ChatManager.getInstanceFor(connection);
String test2JID = "test2@domain.example";
Chat chat = chatManager.createChat(test2JID);
chat.sendMessage("Hello, how are you?");

connection.disconnect();
}
 

使用agsxmpp庫發送消息

        public class ConversationManager
        {
            #region ClassMemeber
    
            private XmppClientConnection _xmppClientConnection = null;
    
    
    
     public ConversationManager(XmppClientConnection con)
            {
                _xmppClientConnection = con;
             }

 public void SendMessage(string message, string to, string guid, string type)
        {
            try
            {
                if (_xmppClientConnection != null)
                {
                    Jid jidTo = new Jid(to);
                    agsXMPP.protocol.client.Message mesg = new agsXMPP.protocol.client.Message(jidTo, _ConnectionWrapper.MyJid,
                        agsXMPP.protocol.client.MessageType.chat,
                                      message);

                    mesg.Id = guid;
                    mesg.AddChild(new agsXMPP.protocol.extensions.msgreceipts.Request());//request delievery
                    _xmppClientConnection.Send(mesg);
                }
            }
            catch (Exception ex)
            {
            }
        }

}