开源一个Java Class实现Openfire登陆、推出、消息发送,方便其他系统集成IM功能了
2024-10-20 00:40:30
开源一个Java Class实现Openfire登陆、推出、消息发送
N年前写的,希望对Openfire开发新手有帮助哦
import java.util.*;
import java.io.*;
import org.jivesoftware.smack.Chat;
import org.jivesoftware.smack.ConnectionConfiguration;
import org.jivesoftware.smack.MessageListener;
import org.jivesoftware.smack.Roster;
import org.jivesoftware.smack.RosterEntry;
import org.jivesoftware.smack.XMPPConnection;
import org.jivesoftware.smack.XMPPException;
import org.jivesoftware.smack.packet.Message;
public class ImApi {//implements MessageListener{
XMPPConnection connection;
public void login(String userName, String password) throws XMPPException
{
ConnectionConfiguration config = new ConnectionConfiguration("222.186.191.86",5222, "Work");
connection = new XMPPConnection(config);
connection.connect();
connection.login(userName, password);
}
public void sendMessage(String message, String to) throws XMPPException
{
Chat chat = connection.getChatManager().createChat(to, null); //this);
chat.sendMessage(message);
}
public void displayBuddyList()
{
Roster roster = connection.getRoster();
Collection<RosterEntry> entries = roster.getEntries();
//System.out.println("\n\n" + entries.size() + " buddy(ies):");
for(RosterEntry r:entries)
{
System.out.println(r.getUser());
}
}
public void disconnect()
{
connection.disconnect();
}
public void processMessage(Chat chat, Message message)
{
if(message.getType() == Message.Type.chat)
System.out.println(chat.getParticipant() + " says: " + message.getBody());
}
public static void main(String args[]) throws XMPPException, IOException
{
// declare variables
ImApi c = new ImApi();
BufferedReader br = new BufferedReader(new InputStreamReader(System.in));
String msg;
// turn on the enhanced debugger
//XMPPConnection.DEBUG_ENABLED = true;
// Enter your login information here
c.login("a2@dev-pc", "1234");
//c.displayBuddyList();
System.out.println("-----");
System.out.println("Who do you want to talk to? - Type contacts full email address:");
String talkTo = "a4@dev-pc"; //br.readLine();
System.out.println("-----");
System.out.println("All messages will be sent to " + talkTo);
System.out.println("Enter your message in the console:");
System.out.println("-----\n");
while( !(msg=br.readLine()).equals("bye"))
{
c.sendMessage(msg, talkTo);
}
c.disconnect();
System.exit(0);
}
}
import java.io.*;
import org.jivesoftware.smack.Chat;
import org.jivesoftware.smack.ConnectionConfiguration;
import org.jivesoftware.smack.MessageListener;
import org.jivesoftware.smack.Roster;
import org.jivesoftware.smack.RosterEntry;
import org.jivesoftware.smack.XMPPConnection;
import org.jivesoftware.smack.XMPPException;
import org.jivesoftware.smack.packet.Message;
public class ImApi {//implements MessageListener{
XMPPConnection connection;
public void login(String userName, String password) throws XMPPException
{
ConnectionConfiguration config = new ConnectionConfiguration("222.186.191.86",5222, "Work");
connection = new XMPPConnection(config);
connection.connect();
connection.login(userName, password);
}
public void sendMessage(String message, String to) throws XMPPException
{
Chat chat = connection.getChatManager().createChat(to, null); //this);
chat.sendMessage(message);
}
public void displayBuddyList()
{
Roster roster = connection.getRoster();
Collection<RosterEntry> entries = roster.getEntries();
//System.out.println("\n\n" + entries.size() + " buddy(ies):");
for(RosterEntry r:entries)
{
System.out.println(r.getUser());
}
}
public void disconnect()
{
connection.disconnect();
}
public void processMessage(Chat chat, Message message)
{
if(message.getType() == Message.Type.chat)
System.out.println(chat.getParticipant() + " says: " + message.getBody());
}
public static void main(String args[]) throws XMPPException, IOException
{
// declare variables
ImApi c = new ImApi();
BufferedReader br = new BufferedReader(new InputStreamReader(System.in));
String msg;
// turn on the enhanced debugger
//XMPPConnection.DEBUG_ENABLED = true;
// Enter your login information here
c.login("a2@dev-pc", "1234");
//c.displayBuddyList();
System.out.println("-----");
System.out.println("Who do you want to talk to? - Type contacts full email address:");
String talkTo = "a4@dev-pc"; //br.readLine();
System.out.println("-----");
System.out.println("All messages will be sent to " + talkTo);
System.out.println("Enter your message in the console:");
System.out.println("-----\n");
while( !(msg=br.readLine()).equals("bye"))
{
c.sendMessage(msg, talkTo);
}
c.disconnect();
System.exit(0);
}
}
==============================================================================
编译脚本:
java -cp smackx.jar;smack.jar;. ImApi
打包命令:
jar -cvfm IMA.jar MANIFEST.MF ImApi.class
就这么简单,这么做之后其他系统也可以集成IM功能了,三年前写的希望没过时哦:)
最新文章
- GDB调试汇编堆栈过程分析
- LINQ实现递归算法
- 个人博客作业week3——案例分析
- checkbox 设置不可更改
- Java 报表之JFreeChart(第二讲)
- jquery.lazyload用法
- Ubuntu - Dconf 注册表键值修改参考表
- Swift实战-QQ在线音乐(第一版)
- D - 小Y上学记——要迟到了!
- POJ Ikki&#39;s Story IV - Panda&#39;s Trick [2-SAT]
- Type Archive for required library: &#39;C:/Users/EuphemiaShaw/.m2/repository/org/apache/hadoop/hadoop-hdfs/2.6.5/hadoop-hdfs-2.6.5.jar&#39; in project &#39;mapreduce&#39; cannot be read or is not a valid ZIP file
- scrapy爬虫框架和selenium的配合使用
- Unexpected error: java.security.InvalidAlgorithmParameterException: the trustAnchors parameter must be non-empty 报错
- IdentityServer4【QuickStart】之使用asp.net core Identity
- oracle 11g R2(静默安装)
- Web前端基础——jQuery(二)
- 1 翻译系列:什么是Code First(EF 6 Code First 系列)
- LeetCode88.合并两个有序数组
- THEOS的第一个TWeak的成功创建
- 高盛为什么认为中国AI领域将超越美国?
热门文章
- linux 下 WebSphere日志中文乱码
- HDU 1060 Leftmost Digit (数学log)
- UISwitch的常见属性
- Shell编程-04-Shell中变量数值计算
- [label][paypal] Paypal 支付页面的语言显示问题
- 浏览器缓存和Service Worker
- 45 Useful JavaScript Tips, Tricks and Best Practices
- .NET框架源码解读之SSCLI的调试支持
- WebApi 插件式构建方案:发现并加载程序集
- solr特点三: 排序样例汇总