Implement the function of MSN Messager with Java

xiaoxiao2021-03-06  95

Implement MSN Messager with Java 2004-02-23 liudong Click: 88

Implement the function of MSN Messager with Java

The current instant messaging software is very full. Everyone is familiar to the QQ, Microsoft's MSN Messenger and NetEase bubbles, and the three should be famous. However, Tencent's QQ and NetEase bubble bubble did not disclose the communication protocols between its clients and the server, which makes it difficult for developers to use this huge user group to open up additional service channels. MSN Messenger This is the ultimate communication software developed by World Number Software - Microsoft, with its close combination with the Windows operating system and the entire Microsoft product family, is simple and practical, stable, world-general, soon acceptable by Chinese users. At present, its users are growing with geometric numbers. But let the developers are in the software, but also open APIs and open communication protocols. The famous MSN Plus is a plugin for extending the MSN Messenger feature developed by its API. And the JMSN we have to introduce today is the Java API of the MSN Messenger open communication protocol. Through this API developer, you can use the Java language to simulate MSN Messenger software. The author of the API also provides an author written in Java language. Aspects are even more powerful than MSN Messenger. Due to the use of cross-platform Java language development, the software can also run in other operating systems simultaneously, and have a variety of Linux systems and Mac OS that have been tested, and of course there is a window operating system. JMSN is an open source API developed by Koreans.

http://sourceforge.net/projects/jmsn/

Download on the site, the home page of the project basically has Korean, including its API documentation, is Korean. This makes me very headache, but there is no relationship, because JMSN is very simple, if there is no special case, it doesn't matter if you don't look at those instructions. The two parts are provided in the JMSN home page, which is shown below, where JMSN is a complete Java application. After downloading decompression, you can run directly, the running interface is very similar to Microsoft's MSN Messenger, including operation, consistent, If your operating system is Linux or others, you can use it directly to replace Microsoft's programs. The other is MSNM-LIB, this is the API we want to introduce today, it is just a development package, which already contains this package in the JMSN component. You may want to first experience the JMSN combo procedure to see what kind of function can you do? There will be an executable file in the directory after the JMSN compression package, but if your JDK is not installed using the installer, it is recommended that you don't have to execute it, it will not find JRE. You can use the command line to launch this program so that there is a benefit that you can also see the information printed in the run. Start JMSN command: Java -jar jmsn.jarjmsn login interface and the main window as shown below: It should be said that this interface is very similar to MSN Messenger. Users can send and receive messages by it. You can see the details of communication between JMSN and the server in the command line window of the JMSN. As we introduce JMSN probably, we introduce what features it can do. Here we start to learn how to use the JMSN's own API: MSNM-LIB to implement these features. The following figure is the relationship between MSNM-LIB and JMSN including the MSN system, that is, we can complete the details between the MSNM-LIB to complete communication between the MSN server without the details of the specific communication protocol. In fact, Msnm-Lib gives us more things make us develop a MSNM-lib to develop a MSN application is very simple, this is what we mentioned earlier, we can not go all possible Korean. The API documentation is because it is really simple. Minnan language said: It's not worthless! There are so many gossip, now we start to develop our own Java-based cross-platform MSN client programs. I believe that you will feel that the blood expands, it's right, what is more exciting? What is still based on Java, cross-platform! Let's give a running code to complete a simplest function: When someone adds it to a friend, the program automatically adds it to a friend, when someone sends it information, the program automatically replys one of the same information.

OK, the code to complete such a simple function is as follows: / * * Created on 2003-11-21 by liudong * / package jmsn.demo; import Rath.msnm.msnmessenger; import Rath.msnm.switchboardSession; import Rath.msnm.userstatus ; import rath.msnm.entity.MsnFriend; import rath.msnm.event.MsnAdapter; import rath.msnm.msg.MimeMessage; / ** * MSN demo * @author Liudong * / public class MSNDaemon extends Thread {private static MSNMessenger msn; public static void main (String [] args) {msn = new MSNMessenger ( "youraccount@hotmail.com", "password"); msn.setInitialStatus (UserStatus.ONLINE); msn.addMsnListener (new MSNAdapter (msn)) Msn.login (); System.Out.println ("Waiting for the Response ...."); // Capture Ctrl C input to log out of MSN's login runtime.Getruntime (). Addshutdownhook (New Msndaemon () } / ** * User stop program execution * / public void Run () {msn.logout (); system.out.println ("msn logout ok");}} / ** * MSN message event processing class * @Author Liudong * / class msnadapter Extends msnadapter {msnmessenger Messenger; public msnadapter (msnmessenger messenger) {this.Messenger = Messenger;} / ** * Someone is entering letter * / Public void progresstyping (SwitchboardSession SS, MSNFRIEND, STRING TYPINGUSER) {system.out.println (Friend.getLoginName () "Enter information ...");} / ** * When you receive a message the method * / public void instantMessageReceived (SwitchboardSession ss, MsnFriend friend, MimeMessage mime) {System.out.print ( "receives the message:" friend.getFriendlyName () "->"); System.out.println (mime .getMessage (); try {// Send the same reply information to the sender Messenger.sendMessage (Friend.getloginname (), MIME);} catch (Exception E) {E.PrintStackTrace ();}} / ** * After the login is successful, the method is executed * / public void loginnetub (msnfriend own) {system.out.println (oow.getloginname () "

");} / ** * After logging in, execute this method * / public void loginerror (String header) {system.Out.println (" login failed: " header);} / ** * When friends are offline This method * / public void useroffline (String Loginname) {system.out.println ("User" loginname "logout.");} / ** * Perform this method when online * / public void useronline (MsnFriend Friend) {System.out.Println ("User" Friend.getFriendlyName () "Login.");} / ** * Someone adds me as a friend * / public void whoseddedme (msnfriend friends) {system.out.println ("User" "AddME."); Try {Messenger.addFriend (Friend.getLoginName ());} catch (Exception E) {E.PrintStackTrace ();}} / ** * Someone Perform * / public void whoremovedme ("User" Friend.getloginName () "User" "User" "User" "User" "."); Try {messenger.removefriend (Friend. getLoginName ();} catch (Exception E) {E.PrintStackTrace ();}}} In addition to two common objects MsnFriend and MIMEMESSAGE are used to represent my friends and MSN information, the other we need to understand is MsnMessenger And Msnadapter. Of course, we don't need other features outside the chat, such as file transfer, etc. Class MSNMessenger A login session with an account. We just need to tell the MsnMessenger class We log in to the account, password, and the initial state after login, and how we process any information received from the MSN server. In MSNM-LIB, processing MSN information is processed by a Msnadapter class. This class defines how to handle, such as incident, someone adds me as a friend, etc., developers can overload these methods for their own deal with. The class of our own extended MSNADAPTER must tell the MSNMessenger instance know that this is the msn.addmsnlistener (New Msnadapter (MSN)) in our previous code; the class of the Self-extended MSNADAPTER is used to process the passive message, such as someone send me a message Wait. When we want to send a message to others, you need to use MsnMessenger's instance, which is why we want to pass the MsnMessenger instance to the msnadapter, because when we receive any message, we need to reply one of the same information to the sender. . The simple function we have in our previously proposed has been completed, and the reader can test on its own machine. The MSNM-LIB library needs to be used at runtime, which is the msnm.jar file.

转载请注明原文地址:https://www.9cbs.com/read-124501.html

New Post(0)