maven project structure
google talk receive message from java program
google talk send message to java program
gmail receive message from java program
<dependencies> ... ... ... <!-- SMACK --> <dependency> <groupId>org.igniterealtime.smack</groupId> <artifactId>smack</artifactId> <version>3.2.1</version> </dependency> <dependency> <groupId>org.igniterealtime.smack</groupId> <artifactId>smackx</artifactId> <version>3.2.1</version> </dependency> <!-- SMACK --> ... ... ... </dependencies>at class path (Other Sources) /account.properties
... ... ... email=YOUR_GOOGLE_EMAIL password=YOUR_GOOGLE_PASSWORD chatWith=CHAT_BUDDY_EMAIL ... ... ...PropertiesFileUtils.java
package com.blogspot.na5cent.smacklearning; import java.io.IOException; import java.util.Properties; /** * * @author redcrow * @link http://na5cent.blogspot.com/2013/07/connect-to-google-talk-by-xmpp-smack.html */ public class PropertiesFileUtils { private static Properties properties; public static synchronized Properties load(String path) throws IOException { if (properties == null) { properties = new Properties(); } properties.load(PropertiesFileUtils.class.getResourceAsStream(path)); return properties; } }Connect.java
package com.blogspot.na5cent.smacklearning; import java.io.IOException; import java.util.Properties; import org.jivesoftware.smack.ConnectionConfiguration; import org.jivesoftware.smack.PacketListener; import org.jivesoftware.smack.XMPPConnection; import org.jivesoftware.smack.XMPPException; import org.jivesoftware.smack.filter.PacketTypeFilter; import org.jivesoftware.smack.packet.Message; import org.jivesoftware.smack.packet.Packet; import org.slf4j.Logger; import org.slf4j.LoggerFactory; /** * * @author redcrow * @link http://na5cent.blogspot.com/2013/07/connect-to-google-talk-by-xmpp-smack.html */ public class Connect { private static final Logger LOG = LoggerFactory.getLogger(Connect.class); private static final String GOOGLE_TALK_URL = "talk.google.com"; private static final int GOOGLE_TALK_PORT = 5222; private static final String GOOGLE_TALK_SERVICE = "gmail.com"; static { XMPPConnection.DEBUG_ENABLED = true; } public static void main(String[] args) throws XMPPException, IOException { Properties properties = PropertiesFileUtils.load("/account.properties"); String email = properties.getProperty("email"); String password = properties.getProperty("password"); String chatWith = properties.getProperty("chatWith"); ConnectionConfiguration config = new ConnectionConfiguration(GOOGLE_TALK_URL, GOOGLE_TALK_PORT, GOOGLE_TALK_SERVICE); XMPPConnection xmppConnection = new XMPPConnection(config); xmppConnection.connect(); xmppConnection.login(email, password); xmppConnection.addPacketListener(new PacketListener() { public void processPacket(Packet packet) { LOG.debug("packet => {}", packet.toXML()); } }, new PacketTypeFilter(Message.class)); ChatManager chatManager = xmppConnection.getChatManager(); Chat chat = chatManager.createChat(chatWith, new MessageListener() { public void processMessage(Chat chat, Message message) { LOG.debug("message body => {}", message.getBody()); } }); chat.sendMessage("hello google talk"); } }Have fun for Learning!
reference : http://www.igniterealtime.org/projects/smack/index.jsp
ไม่มีความคิดเห็น:
แสดงความคิดเห็น