.Net开源IM项目:agsXMPP(C#)

先传个实例:

using System;
using System.Text;
using System.Threading;

using agsXMPP;
using agsXMPP.protocol;
using agsXMPP.protocol.client;

namespace ConsoleClient
{
    class Program
    {
        static bool _bWait;
        
        static void Main(string[] args)
        {
            XmppClientConnection xmppCon = new XmppClientConnection();

            Console.Title = "Console Client";

            // read the jid from the console
            PrintHelp("Enter you Jid ( user@server.com ): ");
            Jid jid = new Jid(Console.ReadLine());

            PrintHelp(String.Format("Enter password for '{0}': ", jid.ToString()));

            xmppCon.Password = Console.ReadLine();
            xmppCon.Username = jid.User;
            xmppCon.Server = jid.Server;
            xmppCon.AutoAgents = false;
            xmppCon.AutoPresence = true;
            xmppCon.AutoRoster = true;
            xmppCon.AutoResolveConnectServer = true;

            // Connect to the server now 
            // !!! this is asynchronous !!!
            try
            {
                xmppCon.OnRosterStart += new ObjectHandler(xmppCon_OnRosterStart);
                xmppCon.OnRosterItem += new XmppClientConnection.RosterHandler(xmppCon_OnRosterItem);
                xmppCon.OnRosterEnd += new ObjectHandler(xmppCon_OnRosterEnd);
                xmppCon.OnPresence += new PresenceHandler(xmppCon_OnPresence);
                xmppCon.OnMessage += new MessageHandler(xmppCon_OnMessage);
                xmppCon.OnLogin += new ObjectHandler(xmppCon_OnLogin);

                xmppCon.Open();

            }
            catch (Exception e)
            {
                Console.WriteLine(e.Message);
            }

            Wait("Login to server, please wait");
            
            PrintCommands();

            bool bQuit = false;

            while (!bQuit)
            {
                string command = Console.ReadLine();
                string[] commands = command.Split(' ');

                switch (commands[0].ToLower())
                {
                    case "help":
                        PrintCommands();
                        break;
                    case "quit":
                        bQuit = true;
                        break;
                    case "msg":
                        string msg = command.Substring(command.IndexOf(commands[2]));
                        xmppCon.Send(new Message(new Jid(commands[1]), MessageType.chat, msg));
                        break;
                    case "status":
                        switch (commands[1])
                        {
                            case "online":
                                xmppCon.Show = ShowType.NONE;
                                break;
                            case "away":
                                xmppCon.Show = ShowType.away;
                                break;
                            case "xa":
                                xmppCon.Show = ShowType.xa;
                                break;
                            case "chat":
                                xmppCon.Show = ShowType.chat;
                                break;
                        }
                        string status = command.Substring(command.IndexOf(commands[2]));
                        xmppCon.Status = status;
                        xmppCon.SendMyPresence();
                        break;
                }
            }

            // close connection
            xmppCon.Close();
        }

        private static void PrintCommands()
        {
            PrintHelp("You are logged in to the server now.");
            PrintHelp("");
            PrintHelp("Available commands are:");
            PrintHelp("msg toJid text");
            PrintHelp("status show{online, away, xa, chat} status");
            PrintHelp("help");
            PrintHelp("quit");
            PrintHelp("");
            PrintHelp("Examples:");
            PrintHelp("msg  test@server.com  Hello World");
            PrintHelp("msg  test@server.com /Office Hello World");
            PrintHelp("status chat free for chat");
            PrintHelp("");
        }

        private static void Wait(string statusMessage)
        {
            int i = 0;
            _bWait = true;

            while (_bWait)
            {
                i++;
                if (i == 60)
                    _bWait = false;

                Thread.Sleep(500);
            }
        }

        static void xmppCon_OnLogin(object sender)
        {
            Console.WriteLine();
            PrintEvent("Logged in to server");
        }

        static void xmppCon_OnRosterEnd(object sender)
        {
            _bWait = false;
            Console.WriteLine();
            PrintInfo("All contacts received");
        }

        static void xmppCon_OnRosterItem(object sender, agsXMPP.protocol.iq.roster.RosterItem item)
        {
            PrintInfo(String.Format("Got contact: {0}", item.Jid));
        }

        static void xmppCon_OnRosterStart(object sender)
        {
            PrintEvent("Getting contacts now");
        }

        static void xmppCon_OnPresence(object sender, Presence pres)
        {
            PrintInfo(String.Format("Got presence from: {0}", pres.From.ToString()));
            PrintInfo(String.Format("type: {0}", pres.Type.ToString()));
            PrintInfo(String.Format("status: {0}", pres.Status));
            PrintInfo("");
        }

        static void xmppCon_OnMessage(object sender, Message msg)
        {

            if (msg.Body != null)
            {
                PrintEvent(String.Format("Got message from: {0}", msg.From.ToString()));
                PrintEvent("message: " + msg.Body);
                PrintInfo("");
            }
        }      

        static void PrintEvent(string msg)
        {
            ConsoleColor current = Console.BackgroundColor;

            Console.BackgroundColor = ConsoleColor.DarkGreen;
            Console.WriteLine(msg);

            Console.BackgroundColor = current;
        }

        static void PrintInfo(string msg)
        {
            ConsoleColor current = Console.BackgroundColor;

            Console.BackgroundColor = ConsoleColor.Red;
            Console.WriteLine(msg);

            Console.BackgroundColor = current;
        }

        static void PrintHelp(string msg)
        {
            ConsoleColor current = Console.ForegroundColor;

            Console.ForegroundColor = ConsoleColor.Yellow;
            Console.WriteLine(msg);

            Console.ForegroundColor = current;
        }
    }
}

Jabber Software:Jabber-NET、agsXMPP与Wilefire 
本篇介绍两个使用.NET技术,确切的说是使用C#写的Jabber Code Libraries – Jabber.NET、agsXMPP,以及一个Java写的跨平台Jabber Server – Wilefire。

前言:

即将完成Jabber Protocal(XMPP) : Core的翻译,在接下来的学习中将结合使用一个Jabber代码库和一个Jabber服务器,做些XMPP实现的分析与Practice。 

Jabber-NET

Jabber-NET是一个使用.NET技术连接到Jabber服务器的类库集。它目前没有任何实现服务器端的计划,如果你兴趣于领导一个分项目来实现服务器端,那么就可以将其从你的选择中排除。它是用C#写的,但是也可以用.NET的其它语言(如VB.NET)进行引用,可以用于组件也可以用于客户端。同时当你探究时你会发现深藏其中的好东西,如Trees、命令行处理等,而且要比也是使用.NET技术的JabberCOM来的简单。

事实上,Jabber-NET似乎已经被搁置,最近一次跟新也是在半年以前,跟新了一些新Xep的支持,以及对vs2005的支持。另外它的文档极少,而且除了邮件列表里有些声响,没有支持该项目的开发Forum或是Blog。也因此官方没有整理好的源文件及编译好的类库可下载。不过仅从中做一些参考,在它的CVS上还是可以进行的。

在Jabber Code Libraries中的介绍:

Code Libraries中罗列了用于 Jabber/XMPP 开发的一些知名类库。

Library   
Language   
License   
Client   
Component   
Server   

Jabber-Net
C#
JOSL
Yes
Yes
--


有兴趣研究Jabber-Net的朋友可以下载我从CVS中整理编译后的类库:Jabber-NET Setup

agsXMPP

agsXMPP 是用C#写的,用于XMPP协议开发的SDK, 该SDK以基于"AG-Software shared source licence"的开源软件形式发布。目前版本0.1。

不同于Jabber-NET,它作为一个SDK同时支持Client、Component和Server的开发;有着比较良好的文档、邮件列表及开发社区支持。官方页面:agsXMPP SDK,Forum: agsXMPP SDK。

下面是一个简单示例,展示了采用agsXMPP登录XMPP服务器,发送一条信息给另一个用户的方便性。仅用三行代码: XmppClientConnection xmpp = new XmppClientConnection("jabber.org");
xmpp.Open("myUsername", "mySecret");
// Wait for the OnLogin event and send your message
xmpp.Send( new Message("test@jabber.org", MessageType.chat, "Hello, how are you?"));
在Jabber Code Libraries中的介绍:

Library   
Language   
License   
Client   
Component   
Server   

agsXMPP
C#
Shared Source
Yes
Yes
Yes



Wildfire

Wildfire就非常知名了,它不是类库,而是一个Java写的跨平台的Jabber服务器实现。它的安装、管理、定制、与其它应用的整合以及联合其它网络都非常简单,甚至做为你的自己的应用程序的平台也是如此方便。它有着强大的Jive软件社区支持 - Jive Software:instant messaging。

Wildfire支持中文,目前版本为3.1,我们可以基于GPL许可证来使用。

在Jabber Servers中的介绍:

Servers中罗列了我们可以利用的,来自许多开源项目和商业用途的 Jabber/XMPP 服务器实现。

Server   
Feature Score   
License   
Platforms   

Wildfire
98%
GPL or Proprietary
AIX, HP-UX, Linux, MacOS X, Solaris, Windows




更多的Jabber服务器介绍可以参见iso1600的:常用开源Jabber(XMPP) IM服务器介绍
评论 2
添加红包

请填写红包祝福语或标题

红包个数最小为10个

红包金额最低5元

当前余额3.43前往充值 >
需支付:10.00
成就一亿技术人!
领取后你会自动成为博主和红包主的粉丝 规则
hope_wisdom
发出的红包
实付
使用余额支付
点击重新获取
扫码支付
钱包余额 0

抵扣说明:

1.余额是钱包充值的虚拟货币,按照1:1的比例进行支付金额的抵扣。
2.余额无法直接购买下载,可以购买VIP、付费专栏及课程。

余额充值