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)
            {
            }
        }

}