java应用之openfire入门篇
Posted 放在垃圾桶里
tags:
篇首语:本文由小常识网(cha138.com)小编为大家整理,主要介绍了java应用之openfire入门篇相关的知识,希望对你有一定的参考价值。
前言
openfire是一个聊天服务端,好比qq服务端.本质是个socker server.
openfire通讯协议是 xmpp ,什么是xmpp参考百科 https://baike.baidu.com/item/XMPP/3430617?fr=aladdin
openfire项目三部分
1.服务端 ---> 2.桌面版客户端spark ---> 3.smack(api客户端)
1.服务端结构
a.plugins(插件):
1.官方提供的插件
2.自己开发插件
功能概述:比如注册用户。
b.web版的管理端
2.桌面版客户端spark
a.类似QQ客户端,还没用
3.smack(api客户端)
a.api库,程序员都懂.
安装篇
下载 openfire-4.1.5-1.noarch.rpm,jdk环境1.8
#安装
rpm -ivh openfire-4.1.5-1.noarch.rpm
#启动
systemctl restart openfire
#浏览器打开
http://ip:9090
官方rest api插件使用
#安装rest api
REST API插件可以后台添加用户等等,详细查看文档
python调用例子:
#coding=utf-8 import requests import json tou1 = {"Authorization": "pymxG12RQQDj2vFY","Accept": "application/json"} tou2 = {"Authorization": "pymxG12RQQDj2vFY","Content-Type": "application/json"} tou3 = {"Authorization": "pymxG12RQQDj2vFY","Content-Type": "application/xml"} def serach_user(user): url = "http://192.168.0.110:9090/plugins/restapi/v1/users?search={user}".format(user=user) r1 = requests.get(url,headers=tou1) print r1.text def create_user(user): url = "http://192.168.0.110:9090/plugins/restapi/v1/users" r1 = requests.post(url, headers=tou2,data=user) print r1.text print r1.status_code def send_broadcast_message(message): url = "http://192.168.0.110:9090/plugins/restapi/v1/messages/users" r1 = requests.post(url, headers=tou3,data=message) print r1.status_code if __name__ == \'__main__\': # c_user1 ={"username": "new1", "password": "p4ssword"} # c_user1 = json.dumps(c_user1) # create_user(c_user1) message = """<?xml version="1.0" encoding="UTF-8" standalone="yes"?><message><body>Your message hello</body></message>""" send_broadcast_message(message)
smack(api客户端)使用
创建个mavn工程.
pom.xml添加
<dependency>
<groupId>org.igniterealtime.smack</groupId> <artifactId>smack-java7</artifactId> <version>4.1.1</version> <exclusions> <exclusion> <groupId>xpp3</groupId> <artifactId>xpp3</artifactId> </exclusion> </exclusions> </dependency> <dependency> <groupId>xpp3</groupId> <artifactId>xpp3</artifactId> <version>1.1.3.4.O</version> </dependency> <dependency> <groupId>org.igniterealtime.smack</groupId> <artifactId>smack-tcp</artifactId> <version>4.1.1</version> </dependency> <dependency> <groupId>org.igniterealtime.smack</groupId> <artifactId>smack-im</artifactId> <version>4.1.1</version> </dependency> <dependency> <groupId>org.igniterealtime.smack</groupId> <artifactId>smack-extensions</artifactId> <version>4.1.1</version> </dependency>
java客户端发送获取消息例子
package com.openfire.client; import java.io.IOException; import java.util.Scanner; import org.jivesoftware.smack.AbstractXMPPConnection; import org.jivesoftware.smack.StanzaListener; import org.jivesoftware.smack.XMPPException; import org.jivesoftware.smack.chat.Chat; import org.jivesoftware.smack.chat.ChatManager; import org.jivesoftware.smack.chat.ChatManagerListener; import org.jivesoftware.smack.chat.ChatMessageListener; import org.jivesoftware.smack.ConnectionConfiguration.SecurityMode; import org.jivesoftware.smack.SmackException; import org.jivesoftware.smack.SmackException.NotConnectedException; import org.jivesoftware.smack.filter.StanzaFilter; import org.jivesoftware.smack.packet.Message; import org.jivesoftware.smack.packet.Stanza; import org.jivesoftware.smack.tcp.XMPPTCPConnection; import org.jivesoftware.smack.tcp.XMPPTCPConnectionConfiguration; /** * Hello world! * */ public class App extends Thread { void say() { }; public static void main( String[] args ) throws SmackException, IOException, XMPPException, InterruptedException { // 对连接的配置 XMPPTCPConnectionConfiguration.Builder builder = XMPPTCPConnectionConfiguration.builder(); builder.setUsernameAndPassword("admin", "123456"); builder.setServiceName("ww110"); //openfire服务端名字,登录管理后台可查看 builder.setHost("192.168.0.110"); builder.setPort(5222); // 不加这行会报错,因为没有证书 builder.setSecurityMode(SecurityMode.disabled); //SASLAuthentication.supportSASLMechanism("PLAIN",0); builder.setDebuggerEnabled(true); XMPPTCPConnectionConfiguration config = builder.build(); // 建立连接并登陆 AbstractXMPPConnection c = new XMPPTCPConnection(config); c.addPacketSendingListener(new StanzaListener() { @Override public void processPacket(Stanza st) throws NotConnectedException { System.out.println("in StanzaListener:" + st.getFrom()); } }, new StanzaFilter() { @Override public boolean accept(Stanza st) { System.out.println("in StanzaFilter:" + st.getFrom()); return false; } }); c.connect(); c.login(); ChatManager chatManager = ChatManager.getInstanceFor(c); chatManager.addChatListener(new ChatManagerListener() { @Override public void chatCreated(Chat cc, boolean bb) { // 当收到来自对方的消息时触发监听函数 cc.addMessageListener(new ChatMessageListener() { @Override public void processMessage(Chat cc, Message mm) { System.out.println("app2收到消息:" + cc + mm.getBody()); } }); } }); while (true) { sleep(2000);
//每2秒给new1用户发消息 Chat chat = chatManager.createChat("new1@ww110");
chat.getThreadID(); Message msg = new Message();
msg.setBody("kkkkkkkkkkkkkkkkkkkkk33333333333333333333333333333333333");
chat.sendMessage(msg); } } }
总结:
openfire只研究到这,暂时写到这。
以上是关于java应用之openfire入门篇的主要内容,如果未能解决你的问题,请参考以下文章
Spring课程 Spring入门篇 4-6 Spring bean装配之基于java的容器注解说明--@ImportResource和@Value java与properties文件交互