xmpp openfire linux 安裝 im 及時通訊伺服器
技術棧: openfire+smack+spark
Smack是一個開源,易于使用的XMPP用戶端類庫。Smack API, 是一個 Java 的XMPP Client Library,也是由Jive Software開發。 優點:程式設計簡單。 缺點:API并非為大量并發使用者設計,每個客戶要1個線程,占用資源大,1台機器隻能模拟有限(數千個)客戶。Smack是一個用 java 寫的XMPP用戶端代碼庫, 是 spark 的核心。
smack demo android :https://github.com/mini188/SmackDemo
spark 非大資料 spark ,類似 fetion 飛信的用戶端
1.安裝 java
jdk-7u79-linux-x64.tar.gz
tar xf jdk-7u79-linux-x64.tar.gz
vim /etc/profile
export JAVA_HOME=/usr/java/jdk1.7.0_79
export JRE_HOME=$JAVA_HOME/jre
export CLASSPATH=.:$JAVA_HOME/lib:$JRE_HOME/lib
source /etc/profile
java -version
2.mysql 安裝
wget http://dev.mysql.com/get/mysql57-community-release-el7-7.noarch.rpm
yum localinstall -y mysql57-community-release-el7-7.noarch.rpm
yum install -y mysql-community-server
systemctl start mysqld.service
grep 'temporary password' /var/log/mysqld.log 看見密碼
【 Your password does not satisfy the current policy requirements】
set global validate_password_policy=0;
【Your password does not satisfy the current policy requirements】
select @@validate_password_length;
set global validate_password_policy=0
SET PASSWORD = PASSWORD('66666666');
use mysql
update user set host='%' where user='root' and host='localhost';
flush privileges;
exit
firewall-cmd --permanent --add-port=3306/tcp
firewall-cmd --reload
firewall-cmd --list-all
mysql -uroot -p66666666
create database openfire;
use openfire ;
source openfire_mysql.sql
update grant all on openfire.* to [email protected]"%" identified by '66666666'
flush privileges;
exit
3.openfire 安裝
tar xf openfire_3_8_2.tar.gz
cp openfire_3_8_2 /home/baoyou/soft/openfire_3_8_2
bin/openfire start
firewall-cmd --permanent --add-port=9090/tcp
firewall-cmd --permanent --add-port=9091/tcp
firewall-cmd --reload
firewall-cmd --list-all
4.通路位址
通路 openfire
http://192.168.206.237:9090/
5.安裝步驟

spark
6. openfire 資料庫結構
7.java 測試
<!-- smack start -->
<dependency>
<groupId>jivesoftware</groupId>
<artifactId>smack</artifactId>
<version>3.1.0</version>
</dependency>
<!-- smack end -->
package com.baoy.cn.smack;
import org.jivesoftware.smack.Chat;
import org.jivesoftware.smack.ChatManager;
import org.jivesoftware.smack.ConnectionConfiguration;
import org.jivesoftware.smack.MessageListener;
import org.jivesoftware.smack.PacketListener;
import org.jivesoftware.smack.XMPPConnection;
import org.jivesoftware.smack.XMPPException;
import org.jivesoftware.smack.filter.PacketFilter;
import org.jivesoftware.smack.filter.PacketTypeFilter;
import org.jivesoftware.smack.packet.Message;
import org.jivesoftware.smack.packet.Packet;
/**
*
* @author baoy
*
*/
public class SmackTest {
public static XMPPConnection con;
public static Chat newChat;
public static ChatManager chatmanager;
public static void main(String[] args) throws InterruptedException {
try {
// 配置域和端口号,域可以換成IP位址
ConnectionConfiguration config = new ConnectionConfiguration(
"www.baoyou.com", 5222);
// 建立一個XMPPConnection對象
con = new XMPPConnection(config);
// 連接配接伺服器
con.connect();
// 使用者登入
con.login("1223716098", "111111");
// 是否已經通過身份驗證
System.out.println("Authenticated = " + con.isAuthenticated());
addListener();
// 擷取一個ChatManager對象
chatmanager = con.getChatManager();
newChat = chatmanager.createChat("[email protected]",
new MessageListener() {
public void processMessage(Chat chat, Message message) {
System.out.println("I'm sending: " + message.getBody());
}
});
newChat.sendMessage("hi");
} catch (XMPPException e) {
e.printStackTrace();
} finally {
// 讓線程休眠 然後再關閉連接配接
Thread.sleep(20000000);
con.disconnect();
}
}
private static void addListener() {
// 包的過濾器
PacketFilter filterMessage = new PacketTypeFilter(Message.class);
// 建立包的監聽器
PacketListener myListener = new PacketListener() {
public void processPacket(Packet packet) {
// 以XML格式輸出接收到的消息
System.out.println(packet.toXML());
System.out.println("From: " + packet.getFrom() + "\n");
System.out.println("Body: " + ((Message) packet).getBody());
try {
// 嘗試發送消息給伺服器
newChat.sendMessage("hi again");
} catch (XMPPException e) {
e.printStackTrace();
}
}
};
// 給連接配接注冊一個包的監聽器
con.addPacketListener(myListener, filterMessage);
}
}
8.openfire web 向所有使用者發送
9.日志顯示
10.注意
1. host 改為域名
linux
vim /etc/hosts
192.168.206.237 www.baoyou.com
windows
C:\WINDOWS\system32\drivers\etc\hosts
192.168.206.237 www.baoyou.com
2. JID 為 使用者名@域名
eg: [email protected]
捐助開發者
在興趣的驅動下,寫一個
免費
的東西,有欣喜,也還有汗水,希望你喜歡我的作品,同時也能支援一下。 當然,有錢捧個錢場(支援支付寶和微信 以及扣扣群),沒錢捧個人場,謝謝各位。
個人首頁:http://knight-black-bob.iteye.com/
謝謝您的贊助,我會做的更好!