IM-smack 4.3.3 demo

1.java - maven项目  -->pom.xml

<project xmlns="http://maven.apache.org/POM/4.0.0" xmlns:xsi="http://www.w3.org/2001/XMLSchema-instance"
  xsi:schemaLocation="http://maven.apache.org/POM/4.0.0 http://maven.apache.org/xsd/maven-4.0.0.xsd">
  <modelVersion>4.0.0</modelVersion>

  <groupId>com.tt</groupId>
  <artifactId>test</artifactId>
  <version>0.0.1-SNAPSHOT</version>
  <packaging>jar</packaging>

  <name>test</name>
  <url>http://maven.apache.org</url>

  <properties>
    <project.build.sourceEncoding>UTF-8</project.build.sourceEncoding>
  </properties>

  <dependencies>
    <dependency>
      <groupId>junit</groupId>
      <artifactId>junit</artifactId>
      <version>3.8.1</version>
      <scope>test</scope>
    </dependency>
    
    <dependency>
	    <groupId>org.igniterealtime.smack</groupId>
	    <artifactId>smack-core</artifactId>
	    <version>4.3.3</version>
	</dependency>
	<dependency>
    <groupId>org.igniterealtime.smack</groupId>
    <artifactId>smack-tcp</artifactId>
    <version>4.3.3</version>
</dependency>
	
	<dependency>
    <groupId>log4j</groupId>
    <artifactId>log4j</artifactId>
    <version>1.2.17</version>
</dependency>
	
	<dependency>
    <groupId>org.igniterealtime.smack</groupId>
    <artifactId>smack-im</artifactId>
    <version>4.3.3</version>
</dependency>
	
	
    <dependency>
        <groupId>org.igniterealtime.smack</groupId>
        <artifactId>smack-java7</artifactId>
        <version>4.3.3</version>
    </dependency>
    <dependency>
        <groupId>org.igniterealtime.smack</groupId>
        <artifactId>smack-extensions</artifactId>
        <version>4.3.3</version>
    </dependency>
    
  </dependencies>
</project>


 

2.demo

package com.tt.test;

import java.io.BufferedReader;
import java.io.IOException;
import java.io.InputStreamReader;
import java.util.Collection;

import org.jivesoftware.smack.AbstractXMPPConnection; 
import org.jivesoftware.smack.ConnectionConfiguration.SecurityMode;
import org.jivesoftware.smack.SmackException.NotConnectedException;
import org.jivesoftware.smack.chat2.Chat;
import org.jivesoftware.smack.chat2.ChatManager;
import org.jivesoftware.smack.chat2.IncomingChatMessageListener;
import org.jivesoftware.smack.packet.Message;
import org.jivesoftware.smack.packet.Presence; 
import org.jivesoftware.smack.roster.Roster; 
import org.jivesoftware.smack.roster.RosterListener;
import org.jivesoftware.smack.tcp.XMPPTCPConnection;
import org.jivesoftware.smack.tcp.XMPPTCPConnectionConfiguration;
import org.jxmpp.jid.EntityBareJid; 
import org.jxmpp.jid.Jid;
import org.jxmpp.jid.impl.JidCreate;
import org.jxmpp.stringprep.XmppStringprepException;

public class XmppClient2 {
	
	AbstractXMPPConnection connection;
	
	public void login(String userName, String password) throws Exception {
		 
	   XMPPTCPConnectionConfiguration config = XMPPTCPConnectionConfiguration.builder()
				  .setUsernameAndPassword(userName, password)
				  .setXmppDomain("localhost.vm")
				  .setHost("localhost.vm")
				  .setPort(5222)
				  .setSecurityMode(SecurityMode.disabled) // Do not disable TLS except for test purposes!
				  .build();

		connection = new XMPPTCPConnection(config);
		connection.connect().login();
		setOnlineStatus();
	}
 
	public void sendStanza(String userJid, String message) throws NotConnectedException, InterruptedException, XmppStringprepException
	{ 
			Message	message1 = new Message(userJid, message);//Message("jsmith@jivesoftware.com", "Howdy! How are you?");

			connection.sendStanza(message1); 
	}
	 
	public void BuildChat(String userJid) throws IOException, NotConnectedException, InterruptedException {
		// Assume we've created an XMPPConnection name "connection"._
		ChatManager chatManager = ChatManager.getInstanceFor(connection);
		chatManager.addIncomingListener(new IncomingChatMessageListener() {
		  @Override
		public  void newIncomingMessage(EntityBareJid from, Message message, Chat chat) {
		    System.out.println("New message from " + from + ": " + message.getBody());
		  }
		});
		EntityBareJid jid = JidCreate.entityBareFrom(userJid);
		Chat chat = chatManager.chatWith(jid);
		String msg = "";
		BufferedReader br = new BufferedReader(new InputStreamReader(System.in));
		while (!(msg = br.readLine()).equals("bye")) {    
			try {
				chat.send(msg);
			} catch (NotConnectedException e) {
				// TODO Auto-generated catch block
				e.printStackTrace();
				sendStanza(userJid, msg);
			}
		} 
	} 
	 	
	public void showJid(String rosterType, Collection<Jid> entries)
	{  
		System.out.println("\n\nrosterType:" + entries.size() + " buddy(ies):");
		for (Jid r : entries) {    
			System.out.println(r.toString());
		}
	}
	public void listenRoster() {
		Roster roster = Roster.getInstanceFor(connection);
		roster.addRosterListener(new RosterListener() { 
			public void presenceChanged(Presence presence) {
				System.out.println("Presence changed: " + presence.getFrom() + "\r\n" + presence);
				//type=available
				//type=unavailable
			}
			@Override
			public void entriesAdded(Collection<Jid> addresses) {
				// TODO Auto-generated method stub
				showJid("added", addresses);
			}
			@Override
			public void entriesUpdated(Collection<Jid> addresses) {
				// TODO Auto-generated method stub
				showJid("updated", addresses);
				
			}
			@Override
			public void entriesDeleted(Collection<Jid> addresses) {
				// TODO Auto-generated method stub
				showJid("deleted", addresses);
				
			}
		});
	}
 
	
	public void disconnect() throws NotConnectedException, InterruptedException {
		setOfflineStatus();
		connection.disconnect();
	}

	public void setOnlineStatus() throws NotConnectedException, InterruptedException { 	
		Presence presence = new Presence(Presence.Type.available, "online", 1 , Presence.Mode.available); 
		connection.sendStanza(presence);		
		Presence presence2 = new Presence(Presence.Type.subscribe, "online", 1 , Presence.Mode.available); 
		connection.sendStanza(presence2);		
		Presence presence3 = new Presence(Presence.Type.subscribed, "online", 1 , Presence.Mode.available); 
		connection.sendStanza(presence3);		
	}
	public void setOfflineStatus() throws NotConnectedException, InterruptedException {
		Presence presence = new Presence(Presence.Type.available, "Gone fishing", 2 , Presence.Mode.away); 
		connection.sendStanza(presence);		
		Presence presence2 = new Presence(Presence.Type.subscribe, "Gone fishing", 2 , Presence.Mode.away); 
		connection.sendStanza(presence2);		
		Presence presence3 = new Presence(Presence.Type.subscribed, "Gone fishing", 2 , Presence.Mode.away); 
		connection.sendStanza(presence3);		
	}
	
	
	public void createUser(String domain, String userName, String pwd) throws SmackException, IOException, XMPPException, InterruptedException {

		   XMPPTCPConnectionConfiguration config = XMPPTCPConnectionConfiguration.builder() 
					  .setXmppDomain(domain)
					  .setHost(domain)
					  .setPort(5222)
					  .setSendPresence(false) 
					  .setSecurityMode(SecurityMode.disabled) // Do not disable TLS except for test purposes!
					  .build();

			connection = new XMPPTCPConnection(config);
			connection.connect(); 
			AccountManager actMgr = AccountManager.getInstance(connection);
			actMgr.sensitiveOperationOverInsecureConnection(true);
			if(actMgr.supportsAccountCreation())
			{
				Localpart l_username = Localpart.from(userName);
				actMgr.createAccount(l_username, pwd);
			}
	}

		 
	public static void main(String args[]) throws Exception {

		XmppClient2 c = new XmppClient2(); 
                boolean choice = false;
		if(choice)
		{
			c.createUser("localhost.vm", "testUser", "1"); 
		} else
		{
                c.login("hells", "1");  
		c.listenRoster();
  
		String talkTo = "tzj101@localhost.vm";  
		System.out.println("All messages will be sent to " + talkTo);
		System.out.println("Enter your message in the console:");
		System.out.println("-----\n");
		c.BuildChat(talkTo); 
                }
		c.disconnect();
		System.exit(0);
	}

}

 

3.测试需求:IM服务器+spark|PSI

参考smack开发文档:

https://github.com/igniterealtime/Smack/wiki/Smack-4.3-Readme-and-Upgrade-Guide

 

https://download.igniterealtime.org/smack/docs/latest/javadoc/    |   smack-4.3.3\javadoc   --函数说明文档     
smack-4.3.3\releasedocs  --开发流程说明文档

注:smack 的api每个版本变更不一,对于不同版本是需要阅读相应的文档来进行对应的代码编写

 

 

 

  • 1
    点赞
  • 1
    收藏
    觉得还不错? 一键收藏
  • 0
    评论

“相关推荐”对你有帮助么?

  • 非常没帮助
  • 没帮助
  • 一般
  • 有帮助
  • 非常有帮助
提交
评论
添加红包

请填写红包祝福语或标题

红包个数最小为10个

红包金额最低5元

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

抵扣说明:

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

余额充值