良好的 C# XMPP 库,支持群聊和 TLS 或 SSL 加密

发布于 2024-11-15 00:44:51 字数 1536 浏览 3 评论 0原文

如果你对这篇内容有疑问,欢迎到本站社区发帖提问 参与讨论,获取更多帮助,或者扫码二维码加入 Web 技术交流群。

扫码二维码加入Web技术交流群

发布评论

需要 登录 才能够评论, 你可以免费 注册 一个本站的账号。

评论(2

哭泣的笑容 2024-11-22 00:44:51

大多数 XmppClient 都是异步工作的。当您调用像 client.Open() 这样的函数时,它会立即返回,但您需要等待它引发 OnBind() 事件,然后才能对 mucManager 执行任何操作。例如:

    public class ChatRoom
    {
        private readonly ILogger _logger;
        private XmppClient _client;
        private MucManager _mucManager;

        public ChatRoom(ILogger logger)
        {
            _logger = logger;           
        }

        public string UserName { get; set; }
        public string Password { get; set; }
        public string XmppDomain { get; set; }
        public System.Uri BoshUri { get; set; }
        public string RoomJid { get; set; }
        public string RoomNick { get; set; }

        public void Start()
        {
            _client = new XmppClient(UserName, XmppDomain, Password);               
            _client.OnBind += (o, e) => _CreateChatRoom(_client, RoomJid, RoomNick);
            _client.OnSendXml += (o, e) => Trace(ConsoleColor.DarkGreen, "Sending:\n {0}", e.Text);
            _client.OnReceiveXml += (o, e) => Trace(ConsoleColor.DarkMagenta, "Receiving:\n {0}", e.Text);
            _client.OnError += (o, e) => Trace(ConsoleColor.Red, "Error: {0}", e.Exception);
            _client.Open();
            _client.Close(); 
        }
        private void Trace(ConsoleColor color, string msg, params object[] args)
        {
            var oldColor = _logger.Color;
            _logger.Color = color;
            _logger.Log(msg, args);
            _logger.Color = oldColor;
            Debug.WriteLine(msg, args);
        }

        private void _CreateChatRoom(XmppClient client, string chatRoomName, string roomNick)
        {
            _mucManager = new MucManager(client);
            _mucManager.EnterRoom(chatRoomName, roomNick);            
        }

        public void SendMessage(string text)
        {
            _client.Send(new Message("[email protected]", MessageType.groupchat, text));
        }

        public void End()
        {
            _client.Close();
        }

        public void Invite(Jid[] user)
        {
            _mucManager.Invite(user, RoomJid, "Come chat");
        }      
    }

Most of XmppClient works asynchronously. When you call someting like client.Open(), it returns immediately but you need to wait for it to raise the OnBind() event before doing anything with the mucManager. For example:

    public class ChatRoom
    {
        private readonly ILogger _logger;
        private XmppClient _client;
        private MucManager _mucManager;

        public ChatRoom(ILogger logger)
        {
            _logger = logger;           
        }

        public string UserName { get; set; }
        public string Password { get; set; }
        public string XmppDomain { get; set; }
        public System.Uri BoshUri { get; set; }
        public string RoomJid { get; set; }
        public string RoomNick { get; set; }

        public void Start()
        {
            _client = new XmppClient(UserName, XmppDomain, Password);               
            _client.OnBind += (o, e) => _CreateChatRoom(_client, RoomJid, RoomNick);
            _client.OnSendXml += (o, e) => Trace(ConsoleColor.DarkGreen, "Sending:\n {0}", e.Text);
            _client.OnReceiveXml += (o, e) => Trace(ConsoleColor.DarkMagenta, "Receiving:\n {0}", e.Text);
            _client.OnError += (o, e) => Trace(ConsoleColor.Red, "Error: {0}", e.Exception);
            _client.Open();
            _client.Close(); 
        }
        private void Trace(ConsoleColor color, string msg, params object[] args)
        {
            var oldColor = _logger.Color;
            _logger.Color = color;
            _logger.Log(msg, args);
            _logger.Color = oldColor;
            Debug.WriteLine(msg, args);
        }

        private void _CreateChatRoom(XmppClient client, string chatRoomName, string roomNick)
        {
            _mucManager = new MucManager(client);
            _mucManager.EnterRoom(chatRoomName, roomNick);            
        }

        public void SendMessage(string text)
        {
            _client.Send(new Message("[email protected]", MessageType.groupchat, text));
        }

        public void End()
        {
            _client.Close();
        }

        public void Invite(Jid[] user)
        {
            _mucManager.Invite(user, RoomJid, "Come chat");
        }      
    }
终陌 2024-11-22 00:44:51

所以看起来这个问题的答案一直是我原来的首选库(Soapbox Studio SDK)实际上支持 TLS 加密,因此一直是最佳选择。看起来 TLS 似乎没有很好的记录和/或我错过了这个功能。

根据您的服务器配置(我使用的是 Openfire 服务器),您应该能够将服务器配置为仅接受加密连接。不是使用 SSL(Openfire 是其服务器中已弃用的进程)并尝试通过 SSL 端口 5223 进行连接,而是简单地通过默认端口 (5222) 连接并要求加密连接将强制客户端使用以下方式发送数据: TLS 并自动协商握手。服务器还可以配置为阻止客户端创建其他用户。通过控制哪些客户端能够在您的服务器上创建 XMPP 用户而不是颁发 SSL 证书,您可以从根本上确保整个通信的安全。

So it looks as though all along that the answer to this question was that my original preferred library, (Soapbox Studio SDK) does in fact support TLS encryption and therefore was by best choice all along. It looks as though TLS is just not well documented and/or I missed this feature.

Depending upon your server configuration ( I am using an Openfire Server ), you should be able to configure your server to only accept encrypted connections. Rather than using the SSL (which Openfire is the process of deprecated from its server ) and attempting to connect over the SSL port 5223, Simply connected over the default port (5222) and requiring an encrypted connection will force the client to send the data using TLS and automatically negotiate the handshake. Also the server can be configured to prevent clients from creating additional users. By controlling which clients are able to create XMPP users on your server rather than issuing SSL certificates you therefore essentially ensure secure communication throughout.

~没有更多了~
我们使用 Cookies 和其他技术来定制您的体验包括您的登录状态等。通过阅读我们的 隐私政策 了解更多相关信息。 单击 接受 或继续使用网站,即表示您同意使用 Cookies 和您的相关数据。
原文