JabberNet -Jabbber .net客户端框架(XMPP协议)

JabberNet 是一个 .NET 的 Jabber (XMPP)协议的客户端开发包,示例代码:

using System;
using System.Threading;

using jabber.client;

namespace SendMessage
{
    class Program
    {
        // we will wait on this event until we're done sending
        static ManualResetEvent done = new ManualResetEvent(false);
        // if true, output protocol trace to stdout
        const bool VERBOSE = true;
        const string TARGET = "otheruser@example.com";

        static void Main(string[] args)
        {
            JabberClient j = new JabberClient();
            // what user/pass to log in as
            j.User = "someuser";
            j.Server = "example.com";  // use gmail.com for GoogleTalk
            j.Password = "somepassword";

            // don't do extra stuff, please.
            j.AutoPresence = false;
            j.AutoRoster = false;
            j.AutoReconnect = -1;

            // listen for errors.  Always do this!
            j.OnError += new bedrock.ExceptionHandler(j_OnError);

            // what to do when login completes
            j.OnAuthenticate += new bedrock.ObjectHandler(j_OnAuthenticate);

            // listen for XMPP wire protocol
            if (VERBOSE)
            {
                j.OnReadText += new bedrock.TextHandler(j_OnReadText);
                j.OnWriteText += new bedrock.TextHandler(j_OnWriteText);
            }

            // Set everything in motion
            j.Connect();

            // wait until sending a message is complete
            done.WaitOne();

            // logout cleanly
            j.Close();
        }

        static void j_OnWriteText(object sender, string txt)
        {
            if (txt == " ") return;  // ignore keep-alive spaces
            Console.WriteLine("SEND: " + txt);
        }

        static void j_OnReadText(object sender, string txt)
        {
            if (txt == " ") return;  // ignore keep-alive spaces
            Console.WriteLine("RECV: " + txt);
        }

        static void j_OnAuthenticate(object sender)
        {
            // Sender is always the JabberClient.
            JabberClient j = (JabberClient)sender;
            j.Message(TARGET, "test");

            // Finished sending.  Shut down.
            done.Set();
        }

        static void j_OnError(object sender, Exception ex)
        {
            // There was an error!
            Console.WriteLine("Error: " + ex.ToString());

            // Shut down.
            done.Set();
        }
    }
}
posted @   桃花雪  阅读(108)  评论(0编辑  收藏  举报
编辑推荐:
· AI与.NET技术实操系列:向量存储与相似性搜索在 .NET 中的实现
· 基于Microsoft.Extensions.AI核心库实现RAG应用
· Linux系列:如何用heaptrack跟踪.NET程序的非托管内存泄露
· 开发者必知的日志记录最佳实践
· SQL Server 2025 AI相关能力初探
阅读排行:
· 震惊!C++程序真的从main开始吗?99%的程序员都答错了
· 【硬核科普】Trae如何「偷看」你的代码?零基础破解AI编程运行原理
· 单元测试从入门到精通
· 上周热点回顾(3.3-3.9)
· winform 绘制太阳,地球,月球 运作规律
点击右上角即可分享
微信分享提示