Smack Overview

翻译这个文档的主要原因是为了同Gtalk进行通信,利用Smack来实现自己的机器人等等服务。主要是没有接触其他的Jabber的Java类库,所以没有比较这些类库之间的差别。不过就这一段时间的使用,感觉Smack确实是简单易用! 
现在开始Smack的Tut。如果你也对Smack感兴趣,或对与Gtalk通信感兴趣,你可以留言进行交流,或到我的Google论坛进行交流。 

1:Smack是一个简单的,功能强大的类库。给用户发送信息只需三行代码便可完成 
XMPPConnection connection = new XMPPConnection(”jabber.org“); 
connection.login(”mtucker”, “password”); 
connection.createChat(”[email protected]“).sendMessage(”Howdy!”); 
2:不会强迫你向其他类库那样,在信息包层面进行编码。它提供了更加智能化的类比如Chat和Groups,能使你的工作更富效率。 
3:不需要你熟悉XMPP XML格式,甚至是XML格式。 
4:易于实现机-机对话。 
5:Apace License下的开源软件。你可以把它用于你的商业或非商业应用程序。 

关于XMPP 

怎样使用这个文档 
这个文档假定你熟悉XMPP及时消息的主要特征。并热切的希望你能在阅读本文档的时候打开Java Doc API作为参考。 

  • Getting stared with Smack 
    续上 Smack Overview 
    这个文档向你介绍一下Smack API以及它里面的重要类库和观念。 

    建立连接 
    XMPPConection用来同XMPP SERVER建立连接。创建SSL连接的话,需要使用SSLXMPPConection类。下面的代码说明了怎样建立连接 

    // Create a connection to the jabber.org server. 
    XMPPConnection conn1 = new XMPPConnection(”jabber.org”); 

    // Create a connection to the jabber.org server on a specific port. 
    XMPPConnection conn2 = new XMPPConnection(”jabber.org”, 5222); 

    // Create an SSL connection to jabber.org. 
    XMPPConnection connection = new SSLXMPPConnection(”jabber.org”); 

    一旦你建立了连接,你就可以通过XMPPConnection.login(String username, String password)方法,使用你的用户名和密码进行登录。登录成功后,可以创建Chat或GroupChat对象来和其他用户进行交流了。 

    好友列表 
    Roster 类可以用来跟踪每一个用户的状态。你可以把用户归入不同的组比如”Friends”或”Co-worker”,也可以判定用户是否online或 offline。通过XMPPConnection.getRoster()能够获得好友列表,Roster类允许你遍历好友列表,弄清他们是什么名字, 属于那个组,是否在线等等。 

    读写消息包(Reading Or Writing Packet) 
    从客户端发向XMPP SERVER的消息成为包(Packet),通过XML格式发送。org.jivesoftware.smack.packet包包含了这些类,他封装了 XMPP允许的三个不同类型的基本包(message, presence, IQ)。类Chat和GroupChat提供了更高级别的结构,能够自动管理包(Packet)的创建和发送,当然你也能直接创建和发送包 (Packet)。下面的例子说明了如何改变自己的状态,让别人知道你出去钓鱼不在电脑前了。 

    // Create a new presence. Pass in false to indicate we’re unavailable. 
    Presence presence = new Presence(Presence.Type.UNAVAILABLE); 
    presence.setStatus(”Gone fishing”); 
    // Send the packet (assume we have a XMPPConnection instance called “con”). 
    con.sendPacket(presence); 

    Smack提供两种方式来读将要到来的包(Packet): PacketListener和PacketCollector。 两者都使用PacketFilter来决定该处理那种类型的包(Packet).PacketListener常常用在事件驱动的程序中;而 PacketCollector维护一个包(Packet)的结果队列,你可以在其上进行Polling和Blocking操作。当包(Packet)到 来时引发某些事件的时候,使用PacketListener最好不过;而PacketCollector常常用来等待某个特定的包(Packet)。这两 种对象都可以通过XMPPConnection的实例来创建。 

    Messaging using Chat and GroupChat 

    接上 Getting stared with Smack 
    及时通讯的核心是收发信息。Smack提供两个类Chat、GroupChat来完成这项工作。 

    # org.jivesoftware.smack.Chat — 两人交谈 
    # org.jivesoftware.smack.GroupChat — 加入聊天室同多个人进行交谈 
    这两个类都使用org.jivesoftware.smack.packet.Message包类(Packet Class)来收发消息。在某些时候,你可能想不通过这两个类,而是直接收发消息。 

    Chat 
    一个Chat在两个用户之间创造一个新的消息线程。下面的代码段说明了如何进行创建Chat,并发送消息 

    // Assume we’ve created an XMPPConnection name “connection”. 
    Chat newChat = connection.createChat(”[email protected]“); 
    newChat.sendMessage(”Howdy!”); 

    Chat.sendMessage(String)方法是一个快捷方法:它首先创建一个Message对象,然后设置消息体,最后发送消息。如果你想在发送消息前增加其他熟悉,你可以使用如下的代码段: 

    // Assume we’ve created an XMPPConnection name “connection”. 
    Chat newChat = connection.createChat(”[email protected]“); 
    Message newMessage = newChat.createMessage(); 
    newMessage.setBody(”Howdy!”); 
    message.setProperty(”favoriteColor”, “red”); 
    newChat.sendMessage(newMessage); 

    Chat允许你很容易的侦听对方的回复。下面的代码段是一个应声虫,它回显对方输入的信息 

    // Assume we’ve created an XMPPConnection name “connection”. 
    Chat newChat = connection.createChat(”[email protected]“); 
    newMessage.setBody(”Hi, I’m an annoying parrot-bot! Type something back to me.”); 
    while (true) { 
    // Wait for the next message the user types to us. 
    Message message = newChat.nextMessage(); 
    // Send back the same text the other user sent us. 

    newChat.sendMessage(message.getBody()); 

    使用Chat.nextMessage()方法来获取下一个Message,它会无限期的等,直到有下一个消息。还有别的方法可以为新消息指定一段特定的事件,也可以使用事件驱动模式,当消息来时通知你。 
    GroupChat 
    群组聊天允许你连接到聊天室,你可以和组内的人进行通信。在发送和接受消息前,你必须使用一个组名(昵称)来加入某个组。下面的代码段说明了如何加入组并发送消息: 

    // Assume we’ve created an XMPPConnection name “connection”. 
    GroupChat newGroupChat = connection.createGroupChat(”[email protected]“); 
    // Join the group chat using the nickname “jsmith”. 
    newGroupChat.join(”jsmith”); 
    // Send a message to all the other people in the chat room. 
    newGroupChat.sendMessage(”Howdy!”); 
    一般来说,在组内收发消息和两人对话类似。并且提供了列出组内其他人的方法。 

  • 使用Java链接Gtalk服务 

    既然想和 Gtalk通信,那么首先怎样才能用Java连上Gtalk呢?而Gtalk采用了基于Jabber的 XMPP协议, 一个开源的即时通信平台,那么他肯定兼容任何一个使用XMPP协议的库。很幸运,Jive SoftWare的Smack就是这样一个简单的而且好用的Jabber 客户端类库。 从Smack2.0后,增加了GoogleTalkConnection,这样你就可以非常简单的连接Gtalk服务器了,不用再设定Host、Port了 。使用下面几行代码就可以连接到Gtalk了: 

    GoogleTalkConnection con = new GoogleTalkConnection(); 
    con.login(”joesmith”, “password”); 
    con.createChat(”[email protected]”).sendMessage(”Howdy!”); 
    con.close(); 

    在测试过程�发现应该注意下面的一些问题: 

    login�的用户名是不需要@gmail.com这个后缀的 
    接受方一定要是对方Gtalk的全称。 
    接受方应该在你的好友列表里面,负责对方会收不到任何信息的。 
    很简单,但还是比较有趣的。比如你可以把某个文本�的内容一行行的读出来,然后发送给对方,就像看小说一样。呵呵!

你可能感兴趣的:(Smack Overview)