JML是Java实现即时通讯软件MSN的lib包,目前支持的协议为MSNP8-MSNP15。
JML的官网:http://sourceforge.net/projects/java-jml/
【一】、基本介绍
附件提供测试代码和lib文件:
msn demo代码:my_demo_code.zip
lib包:jml-1.0b4.jar
官方example文件:example.zip
JML的lib还依赖的其他第三方lib包:
cindy.jar
commons-logging.jar
httpcore.jar
第三方lib包下载:第三方lib.zip
【二】、DEMO代码
简单实现了MSN收发消息、好友添加等等基本功能。
MyJmsnClient.java
Java代码
package michael.msn.jml;
import java.util.Scanner;
import net.sf.jml.MsnContact;
import net.sf.jml.MsnMessenger;
import net.sf.jml.MsnProtocol;
import net.sf.jml.MsnUserStatus;
import net.sf.jml.impl.MsnMessengerFactory;
import org.apache.log4j.PropertyConfigurator;
/**
* @author michael
*
*/
public class MyJmsnClient {
private String msnEmail;
private String msnPassword;
private MsnMessenger msn;
/**
* @param msnEmail
* @param msnPassword
*/
public MyJmsnClient(String msnEmail, String msnPassword) {
this.msnEmail = msnEmail;
this.msnPassword = msnPassword;
}
/**
* 启动
*/
public void start() {
// 创建MsnMessenger
msn = MsnMessengerFactory.createMsnMessenger(msnEmail, msnPassword);
// 设置登录协议 MSNP15 可以发送离线消息
msn.setSupportedProtocol(new MsnProtocol[] { MsnProtocol.MSNP15 });
// 设置用户状态
msn.getOwner().setInitStatus(MsnUserStatus.ONLINE);
// 注册事件监听
msn.addListener(new MyJmsnSimpleAdapter());
msn.login();// 登录
}
/**
* msn logout
*/
public void stop() {
msn.logout();
}
/**
* 给所有的人发消息
* @param msg
*/
public void sendAllMsg(String msg) {
MsnContact[] contactList = msn.getContactList().getContacts();
for (MsnContact con : contactList) {
msn.sendText(con.getEmail(), msg);
}
}
/**
* @param args
*/
public static void main(String[] args) {
PropertyConfigurator.configure(MyJmsnClient.class
.getResource("/log4j.properties"));
MyJmsnClient client = new MyJmsnClient("yourmsnemail", "yourpassword");
System.out.println(" MyJmsnClient start ...");
client.start();
try {
Thread.sleep(5000);
} catch (Exception e) {
e.printStackTrace();
}
//获取控制台输入的信息发送给所有的好友
Scanner input = new Scanner(System.in);
while (input.hasNext()) {
String s = input.next();
if ("quit".equals(s)) {
System.out.println(" MSN exit by quit");
client.stop();
break;
} else {
client.sendAllMsg(s);
}
}
System.out.println("=====================");
}
}
MyJmsnSimpleAdapter.java
Java代码
package michael.msn.jml;
import net.sf.jml.MsnContact;
import net.sf.jml.MsnFileTransfer;
import net.sf.jml.MsnList;
import net.sf.jml.MsnMessenger;
import net.sf.jml.MsnSwitchboard;
import net.sf.jml.event.MsnAdapter;
import net.sf.jml.message.MsnControlMessage;
import net.sf.jml.message.MsnEmailInitEmailData;
import net.sf.jml.message.MsnInstantMessage;
import org.apache.log4j.Logger;
/**
* 实现了一个简单的MsnAdapter
* @author michael
*
*/
public class MyJmsnSimpleAdapter extends MsnAdapter {
private static final Logger logger = Logger
.getLogger(MyJmsnSimpleAdapter.class);
/**
* @see net.sf.jml.event.MsnAdapter#contactAddCompleted(net.sf.jml.MsnMessenger,
* net.sf.jml.MsnContact, net.sf.jml.MsnList)
* @param pMessenger
* @param pContact
* @param pList
*/
@Override
public void contactAddCompleted(MsnMessenger pMessenger,
MsnContact pContact, MsnList pList) {
super.contactAddCompleted(pMessenger, pContact, pList);
try {
pMessenger.sendText(pContact.getEmail(), pContact.getFriendlyName()
+ " 你好,已经加为好友");
} catch (Exception e) {
logger.error("contactAddCompleted sendText error", e);
}
}
/**
* @see net.sf.jml.event.MsnAdapter#contactAddedMe(net.sf.jml.MsnMessenger,
* net.sf.jml.MsnContact)
* @param pMessenger
* @param pContact
*/
@Override
public void contactAddedMe(MsnMessenger pMessenger, MsnContact pContact) {
super.contactAddedMe(pMessenger, pContact);
logger.info("user: " + pContact.getEmail() + " Add me.");
try {
pMessenger.addFriend(pContact.getEmail(), pContact
.getFriendlyName());
} catch (Exception e) {
logger.error("addFriend error:", e);
}
}
/**
* @see net.sf.jml.event.MsnAdapter#contactListInitCompleted(net.sf.jml.MsnMessenger)
* @param pMessenger
*/
@Override
public void contactListInitCompleted(MsnMessenger pMessenger) {
super.contactListInitCompleted(pMessenger);
MsnContact[] cons = pMessenger.getContactList().getContactsInList(
MsnList.AL);
logger.info("你的联系人数量=" + cons.length);
for (MsnContact con : cons) {
logger.info("联系人:" + con.getEmail().getEmailAddress() + " 状态:"
+ con.getStatus().getDisplayStatus());
}
}
/**
* @see net.sf.jml.event.MsnAdapter#contactListSyncCompleted(net.sf.jml.MsnMessenger)
* @param pMessenger
*/
@Override
public void contactListSyncCompleted(MsnMessenger pMessenger) {
super.contactListSyncCompleted(pMessenger);
logger.info("MSN contactListSyncCompleted.");
}
/**
* @see net.sf.jml.event.MsnAdapter#contactRemoveCompleted(net.sf.jml.MsnMessenger,
* net.sf.jml.MsnContact, net.sf.jml.MsnList)
* @param pMessenger
* @param pContact
* @param pList
*/
@Override
public void contactRemoveCompleted(MsnMessenger pMessenger,
MsnContact pContact, MsnList pList) {
super.contactRemoveCompleted(pMessenger, pContact, pList);
logger.info("user: " + pContact.getEmail() + " Remove Completed.");
}
/**
* @see net.sf.jml.event.MsnAdapter#contactRemovedMe(net.sf.jml.MsnMessenger,
* net.sf.jml.MsnContact)
* @param pMessenger
* @param pContact
*/
@Override
public void contactRemovedMe(MsnMessenger pMessenger, MsnContact pContact) {
super.contactRemovedMe(pMessenger, pContact);
logger.info("user: " + pContact.getEmail() + " Remove me.");
}
/**
* @see net.sf.jml.event.MsnAdapter#controlMessageReceived(net.sf.jml.MsnSwitchboard,
* net.sf.jml.message.MsnControlMessage, net.sf.jml.MsnContact)
* @param pSwitchboard
* @param pMessage
* @param pContact
*/
@Override
public void controlMessageReceived(MsnSwitchboard pSwitchboard,
MsnControlMessage pMessage, MsnContact pContact) {
super.controlMessageReceived(pSwitchboard, pMessage, pContact);
logger.info(pContact.getFriendlyName() + " 正在输入文字......");
}
/**
* @see net.sf.jml.event.MsnAdapter#exceptionCaught(net.sf.jml.MsnMessenger,
* java.lang.Throwable)
* @param pMessenger
* @param pThrowable
*/
@Override
public void exceptionCaught(MsnMessenger pMessenger, Throwable pThrowable) {
super.exceptionCaught(pMessenger, pThrowable);
logger.error("exceptionCaught :", pThrowable);
}
/**
* @see net.sf.jml.event.MsnAdapter#fileTransferRequestReceived(net.sf.jml.MsnFileTransfer)
* @param pTransfer
*/
@Override
public void fileTransferRequestReceived(MsnFileTransfer pTransfer) {
super.fileTransferRequestReceived(pTransfer);
logger.info("receive fileTransferRequest from "
+ pTransfer.getContact().getDisplayName());
}
/**
* @see net.sf.jml.event.MsnAdapter#initialEmailDataReceived(net.sf.jml.MsnSwitchboard,
* net.sf.jml.message.MsnEmailInitEmailData, net.sf.jml.MsnContact)
* @param pSwitchboard
* @param pMessage
* @param pContact
*/
@Override
public void initialEmailDataReceived(MsnSwitchboard pSwitchboard,
MsnEmailInitEmailData pMessage, MsnContact pContact) {
super.initialEmailDataReceived(pSwitchboard, pMessage, pContact);
logger.info("receive email from " + pContact.getDisplayName());
}
/**
* @see net.sf.jml.event.MsnAdapter#instantMessageReceived(net.sf.jml.MsnSwitchboard,
* net.sf.jml.message.MsnInstantMessage, net.sf.jml.MsnContact)
* @param pSwitchboard
* @param pMessage
* @param pContact
*/
@Override
public void instantMessageReceived(MsnSwitchboard pSwitchboard,
MsnInstantMessage pMessage, MsnContact pContact) {
super.instantMessageReceived(pSwitchboard, pMessage, pContact);
logger.info("received from: " + pContact.getDisplayName()
+ " with msg: " + pMessage.getContent());
try {
MsnInstantMessage msg = new MsnInstantMessage();
msg.setContent("你好!我是机器人Michael,请勿回复消息。"
+ "\r\n你可以访问我的blog http://sjsky.iteye.com");
msg.setFontRGBColor(255);
pSwitchboard.sendMessage(msg);// 发送信息
pSwitchboard.close();// 关闭对话框
} catch (Exception e) {
e.printStackTrace();
}
}
/**
* @see net.sf.jml.event.MsnAdapter#loginCompleted(net.sf.jml.MsnMessenger)
* @param pMessenger
*/
@Override
public void loginCompleted(MsnMessenger pMessenger) {
super.loginCompleted(pMessenger);
logger.info("MSN login success with ID="
+ pMessenger.getOwner().getEmail());
}
/**
* @see net.sf.jml.event.MsnAdapter#logout(net.sf.jml.MsnMessenger)
* @param pMessenger
*/
@Override
public void logout(MsnMessenger pMessenger) {
super.logout(pMessenger);
logger.info("MSN logout with ID=" + pMessenger.getOwner().getEmail());
}
/**
* @see net.sf.jml.event.MsnAdapter#offlineMessageReceived(java.lang.String,
* java.lang.String, java.lang.String, net.sf.jml.MsnContact)
* @param pBody
* @param pContentType
* @param pEncoding
* @param pContact
*/
@Override
public void offlineMessageReceived(String pBody, String pContentType,
String pEncoding, MsnContact pContact) {
super.offlineMessageReceived(pBody, pContentType, pEncoding, pContact);
logger.info("receive offline msg from: "
+ pContact.getEmail().getEmailAddress() + " msg:"
+ pContact.getPersonalMessage());
}
/**
* @see net.sf.jml.event.MsnAdapter#ownerDisplayNameChanged(net.sf.jml.MsnMessenger)
* @param pMessenger
*/
@Override
public void ownerDisplayNameChanged(MsnMessenger pMessenger) {
super.ownerDisplayNameChanged(pMessenger);
logger.info("MSN DisplayName : "
+ pMessenger.getOwner().getOldDisplayName() + " -> "
+ pMessenger.getOwner().getDisplayName());
}
/**
* @see net.sf.jml.event.MsnAdapter#ownerStatusChanged(net.sf.jml.MsnMessenger)
* @param pMessenger
*/
@Override
public void ownerStatusChanged(MsnMessenger pMessenger) {
super.ownerStatusChanged(pMessenger);
logger
.info("MSN status :"
+ pMessenger.getOwner().getOldStatus()
.getDisplayStatus() + " -> "
+ pMessenger.getOwner().getStatus().getDisplayStatus());
}
}
-----------------------------------分 ------------------------------------隔 ------------------------------------线 --------------------------------------
jml-1.0b4.jar (315.9 KB)
下载次数: 14
example.zip (11.5 KB)
下载次数: 12
第三方lib.zip (408.3 KB)
下载次数: 21
my_demo_code.zip (2.9 KB)
下载次数: 18