tigase 安装及基本开发
参考:http://docs.tigase.org/tigase-server/snapshot/Administration_Guide/html/#manualinstall
1.下载server的包(http://www.tigase.net/downloads)
2.解压tar -xzvf tigase-server-x.y.z-bv.tar.gz
3.chmod u+x ./scripts/tigase.sh
4.修改两个配置文件:tigase.conf和init.properties:
修改tigase.conf中的
JAVA_HOME="${JDKPath}"
为
JAVA_HOME="${JAVA_HOME}"
修改init.properties内容(下面是最基本的,后面根据需要添加):
config-type=--gen-config-def
--admins=admin@192.168.43.146
--virt-hosts = 192.168.43.146
--debug=server
--user-db=mysql
--user-db-uri=jdbc:mysql://localhost:3306/tigasedb?user=root&password=root&useUnicode=true&characterEncoding=UTF-8&autoCreateUser=true
5.初始化数据库(root/root为数据库账号密码):
./scripts/db-create-mysql.sh root root
6.启动tigase server:
./scripts/tigase.sh start etc/tigase.conf
7.通过查看日志来确认启动是否正常:
tail -f ./logs/tigase.log.0
之后就可以通过各种client(http://xmpp.org/software/clients.html),例如spark和psi等连接server
一些常见的问题:
1.如何进行扩展
只需要添加下面的一些maven依赖:
<dependencies>
<dependency>
<groupId>tigase</groupId>
<artifactId>tigase-server</artifactId>
<version>7.0.4</version>
</dependency>
<dependency>
<groupId>tigase</groupId>
<artifactId>tigase-message-archiving</artifactId>
<version>1.1.0</version>
</dependency>
<dependency>
<groupId>tigase</groupId>
<artifactId>tigase-muc</artifactId>
<version>2.3.0</version>
</dependency>
<dependency>
<groupId>tigase</groupId>
<artifactId>tigase-pubsub</artifactId>
<version>3.1.0</version>
</dependency>
<dependency>
<groupId>tigase</groupId>
<artifactId>tigase-xmltools</artifactId>
<version>3.4.5</version>
</dependency>
<dependency>
<groupId>tigase</groupId>
<artifactId>tigase-http-api</artifactId>
<version>1.1.1</version>
</dependency>
<dependency>
<groupId>tigase</groupId>
<artifactId>tigase-http-api-jetty</artifactId>
<version>1.0.0</version>
</dependency>
<dependency>
<groupId>tigase</groupId>
<artifactId>tigase-utils</artifactId>
<version>3.4.4</version>
</dependency>
</dependencies>
<repositories>
<repository>
<id>tigase</id>
<name>Tigase repository</name>
<url>http://maven.tigase.org</url>
</repository>
<repository>
<id>tigase-snapshot</id>
<name>Tigase repository</name>
<url>http://build.xmpp-test.net/maven/</url>
<snapshots>
<enabled>true</enabled>
</snapshots>
</repository>
</repositories>
然后我们就可以使用tigase相关的api,之后打成jar包放在tigase安装目录的jars目录下即可。
2.自定义的component如何发送packet:
通过tigase.server.AbstractMessageReceiver类的 boolean addOutPacket(Packet packet)方法即可
3.tigase好友关系在哪个表中维护?
在tig_pairs表
4.如何开启日志:
--debug=xmpp.impl.roster
开启tigase.xmpp.impl.roster包下面的类的日志
--debug-packages = com.tch.test.tigase.mytigase
开启我们自己的com.tch.test.tigase.mytigase包下面的类的日志
5.添加好友以及查看用户状态的时候,自动认证,无需对方同意,只需要添加下面的一个配置即可(参考:https://projects.tigase.org/issues/1696):
sess-man/plugins-conf/auto-authorize=true
6.如何在程序中添加用户:
调用存储过程 TigAddUserPlainPw,
java.sql.Connection.prepareStatement("{call TigAddUserPlainPw(?, ?)}");
第一个参数是user_id,第二个参数是密码
7.如何持久化聊天室(群聊,参考:https://projects.tigase.org/boards/8/topics/3456?r=5680):
--comp-name-2 = muc
--comp-class-2 = tigase.muc.MUCComponent
muc/default_room_config/muc#roomconfig_persistentroom=true
tigase init.properties一些配置的含义:
#支持群聊,并且默认就会保存群聊记录到muc_history表,但是默认是临时保存,当所有人离开之后,群就会不存在,群聊天记录也会删除,可以通过muc/default_room_config/muc#roomconfig_persistentroom=true来支持持久化(不配置这个component的话,不支持群聊)
--comp-name-2 = muc
--comp-class-2 = tigase.muc.MUCComponent
#支持消息存储(tig_ma_msgs表,不配置这个component的话,不会存储私聊消息)
--comp-name-3=message-archive
--comp-class-3=tigase.archive.MessageArchiveComponent
#消息存储的数据库配置,不配置这个参数的话,会使用--user-db-uri的数据库地址
message-archive/archive-repo-uri=jdbc:mysql://localhost:3306/messagearchivedb?user=root&password=root&useUnicode=true&characterEncoding=UTF-8
#指定消息存储的component
sess-man/plugins-conf/message-archive-xep-0136/component-jid=message-archive@192.168.43.146
#存储消息的方式(不配置这个参数的话,不会保存消息)
sess-man/plugins-conf/message-archive-xep-0136/required-store-method=message
#自动授权,配置这个参数之后,加好友的时候,不需要对方同意,自动添加成功
sess-man/plugins-conf/auto-authorize=true
#持久化群聊房间,不配置这个参数的话,所有人离开房间的时候,这个群就不存在了。
muc/default_room_config/muc#roomconfig_persistentroom=true
jaxmpp客户端:
添加仓库地址:
<repositories>
<repository>
<id>tigase</id>
<name>Tigase repository</name>
<url>http://maven.tigase.org</url>
</repository>
<repository>
<id>tigase-snapshot</id>
<name>Tigase repository</name>
<url>http://build.xmpp-test.net/maven/</url>
<snapshots>
<enabled>true</enabled>
</snapshots>
</repository>
</repositories>
添加依赖:
<dependency>
<groupId>tigase</groupId>
<artifactId>jaxmpp-core</artifactId>
<version>2.1.0</version>
</dependency>
<dependency>
<groupId>tigase</groupId>
<artifactId>jaxmpp-j2se</artifactId>
<version>2.1.0</version>
</dependency>
客户端代码:
import tigase.jaxmpp.core.client.BareJID;
import tigase.jaxmpp.core.client.JID;
import tigase.jaxmpp.core.client.SessionObject;
import tigase.jaxmpp.core.client.exceptions.JaxmppException;
import tigase.jaxmpp.core.client.observer.Listener;
import tigase.jaxmpp.core.client.xmpp.modules.presence.PresenceModule;
import tigase.jaxmpp.core.client.xmpp.modules.presence.PresenceModule.PresenceEvent;
import tigase.jaxmpp.core.client.xmpp.modules.roster.RosterItem;
import tigase.jaxmpp.core.client.xmpp.modules.roster.RosterStore;
import tigase.jaxmpp.core.client.xmpp.stanzas.Message;
import tigase.jaxmpp.core.client.xmpp.stanzas.Stanza;
import tigase.jaxmpp.core.client.xmpp.stanzas.StanzaType;
import tigase.jaxmpp.j2se.Jaxmpp;
public class JaxmppExample {
public static void main( String[] args ) throws JaxmppException, InterruptedException {
final Jaxmpp jaxmpp = new Jaxmpp();
try {
jaxmpp.getModulesManager().getModule( PresenceModule.class ).addListener( PresenceModule.ContactChangedPresence, new Listener<PresenceModule.PresenceEvent>() {
@Override
public void handleEvent( PresenceEvent be ) throws JaxmppException {
System.out.println( String.format( "Presence received:\t %1$s is now %2$s (%3$s)", be.getJid(), be.getShow(), be.getStatus() != null ? be.getStatus() : "none" ) );
}
} );
jaxmpp.getSessionObject().setProperty(tigase.jaxmpp.j2se.connectors.socket.SocketConnector.HOSTNAME_VERIFIER_DISABLED_KEY, Boolean.TRUE);
jaxmpp.getProperties().setUserProperty( SessionObject.USER_BARE_JID, BareJID.bareJIDInstance( "richmj@192.168.43.146" ) );
jaxmpp.getProperties().setUserProperty( SessionObject.PASSWORD, "richmj" );
System.out.println("开始登陆。。。");
jaxmpp.login();
System.out.println("登陆成功。。。");
RosterStore rosterStore = jaxmpp.getRoster();
for(RosterItem rosterItem : rosterStore.getAll()){
System.out.println("rosterItem:" + rosterItem);
}
jaxmpp.send(createMessage("admin,你好啊"));
//contact.sendMessage(JID.jidInstance("admin@192.168.43.146"), "Test", "This is a test");
} catch (Exception e) {
e.printStackTrace();
}finally {
if(jaxmpp != null){
jaxmpp.disconnect();
}
}
}
private static Stanza createMessage(String msg) {
try {
/*
<message id="pCp2F-58" to="richmj@192.168.43.146" from="admin@192.168.43.146/Spark" type="chat">
<body>1</body>
</message>
*/
Message message = Message.create();
message.setType(StanzaType.chat);
message.setAttribute("from", JID.jidInstance("richmj@192.168.43.146").toString());
message.setAttribute("to", JID.jidInstance("admin@192.168.43.146").toString());
message.setBody(msg);
System.out.println("message:" + message.getAsString());
return message;
} catch (JaxmppException e) {
e.printStackTrace();
}
return null;
}
}
下一篇: python中的插入排序的简单用法