风故故,也依依

Stand still in the wind.

导航

基于XMPP协议与Gtalk进行即时通信

/*
* @(#)GtalkTest.java 2010-4-17
*
* Copyright (c) 2010 by gerald. All Rights Reserved.
*/

package org.jsoft.opensource.demos.smack;

import org.apache.commons.logging.Log;
import org.apache.commons.logging.LogFactory;
import org.jivesoftware.smack.Chat;
import org.jivesoftware.smack.ChatManager;
import org.jivesoftware.smack.ConnectionConfiguration;
import org.jivesoftware.smack.MessageListener;
import org.jivesoftware.smack.XMPPConnection;
import org.jivesoftware.smack.packet.Message;

/**
* 利用Smack,通过XMPP协议与Gtalk通信.
*
* @author <a href="
mailto:gerald.chen@qq.com">GeraldChen</a>
* @version $Id: GtalkTest.java 2010-4-17 上午12:30:23$
*/
public class GtalkTest {
/** Logger for this class */
protected static final Log LOG = LogFactory.getLog(GtalkTest.class);

public static void main(String[] args) {
   test();
}

/**
* 与Gtalk进行通讯
*
*/
public static void test() {
   ConnectionConfiguration connectionConfig = new ConnectionConfiguration(
     "talk.google.com", 5222, "gmail.com");
   connectionConfig.setSASLAuthenticationEnabled(false);
   XMPPConnection connection = new XMPPConnection(connectionConfig);
   try {
    connection.connect();
    connection.login("logs.chen", "***********");
    ChatManager chatmanager = connection.getChatManager();

    Chat newChat = chatmanager.createChat("edotxp@gmail.com",
      new MessageListener() {
       public void processMessage(Chat chat, Message message) {
        if(LOG.isInfoEnabled()) {
         LOG.info("Received message: \r\n" + message.toXML());
         LOG.info("收到相应消息");
        }
       }
      });
    newChat.sendMessage("陈先生,您好!");
    Thread.sleep(10000);
   } catch (Exception e) {
    LOG.error(e);
   }
}

}

posted on 2010-04-17 02:11  jadmin  阅读(172)  评论(0编辑  收藏  举报