JML是Java实现即时通讯软件MSN的lib包,目前支持的协议为MSNP8-MSNP15。
JML的官网:http://sourceforge.net/projects/java-jml/
【一】、基本介绍
附件提供测试代码和lib文件:
- msn demo代码:jml_michael_demo_code.zip
- lib包: jml-1.0b4.jar
- 官方example文件:jml-example.zip
JML的lib还依赖的其他第三方lib包:
- cindy.jar
- commons-logging.jar
- httpcore.jar
- 第三方lib包下载:第三方lib.zip
【二】、DEMO代码
简单实现了MSN收发消息、好友添加等等基本功能。
MyJmsnClient.java
1 2 3 4 5 6 7 8 9 10 11 12 13 14 15 16 17 18 19 20 21 22 23 24 25 26 27 28 29 30 31 32 33 34 35 36 37 38 39 40 41 42 43 44 45 46 47 48 49 50 51 52 53 54 55 56 57 58 59 60 61 62 63 64 65 66 67 68 69 70 71 72 73 74 75 76 77 78 79 80 81 82 83 84 85 86 87 88 89 90 91 92 93 94 95 |
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
1 2 3 4 5 6 7 8 9 10 11 12 13 14 15 16 17 18 19 20 21 22 23 24 25 26 27 28 29 30 31 32 33 34 35 36 37 38 39 40 41 42 43 44 45 46 47 48 49 50 51 52 53 54 55 56 57 58 59 60 61 62 63 64 65 66 67 68 69 70 71 72 73 74 75 76 77 78 79 80 81 82 83 84 85 86 87 88 89 90 91 92 93 94 95 96 97 98 99 100 101 102 103 104 105 106 107 108 109 110 111 112 113 114 115 116 117 118 119 120 121 122 123 124 125 126 127 128 129 130 131 132 133 134 135 136 137 138 139 140 141 142 143 144 145 146 147 148 149 150 151 152 153 154 155 156 157 158 159 160 161 162 163 164 165 166 167 168 169 170 171 172 173 174 175 176 177 178 179 180 181 182 183 184 185 186 187 188 189 190 191 192 193 194 195 196 197 198 199 200 201 202 203 204 205 206 207 208 209 210 211 212 213 214 215 216 217 218 219 220 221 222 223 224 225 226 227 228 229 230 231 232 233 234 235 236 237 238 239 240 241 242 243 244 245 246 247 248 249 250 251 252 253 254 255 256 257 258 259 260 261 262 263 264 265 266 267 268 269 270 271 272 273 274 275 |
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()); } } |
原创文章,转载请注明: 转载自micmiu – 软件开发+生活点滴[ http://www.micmiu.com/ ]
0 条评论。