Use Java to implement personalized MSN Chat

Source: Internet
Author: User

Use Java to implement personalized MSN Chat

MSN Messenger, the instant messaging software developed by Microsoft, the world's No. 1 software vendor, is closely integrated with windows operating systems and the entire Microsoft Product Family, simple and practical, stable performance, and common features in the world, are soon accepted by Chinese users. At present, their users are growing exponentially. However, developers are excited that the software also provides open APIs and open communication protocols.

The famous MSN Plus is a plug-in developed using its API to expand the MSN Messenger function. The jMSN we will introduce today is a java api that encapsulates the communication protocol opened by MSN Messenger. Through this API, developers can use JAVA to simulate the MSN Messenger software, the author of the API also provides an MSN client software that is more powerful than MSN Messenger in some aspects written in JAVA.

Because the cross-platform JAVA language is used for development, the software can run on other operating systems at the same time. Currently, various Linux systems and Mac OS have been tested, of course, there are also Windows operating systems.

JMSN is an open source API developed by South Koreans. This is a headache for me, but it doesn't matter, because jMSN is very simple. It doesn't matter if I don't read the instructions under any special circumstances.

The jMSN homepage provides two parts for download, as shown in. jmsn is a complete JAVA application, which can be directly run after download and decompression. The running interface is similar to Microsoft's MSN Messenger, these operations are very consistent. If your operating system is Linux or other systems, you can directly use it to replace Microsoft programs. The other one is msnm-lib. This is the API we will introduce today. It is just a development kit and already contains this package in the jmsn component.

 

You may want to try out the built-in jmsn program to see what functions can be achieved first? The directory after extracting the jmsn package contains an executable file. However, if your JDK is not installed using the installer, you are advised not to execute it and it will not find jre.

You can use the command line to start this program. The advantage is that you can also see the information printed during running.

Run the following command to start jMSN:

 

      java -jar jmsn.jar

 

Shows the jMSN logon interface and main window:

 

 

It should be said that this interface is very similar to MSN Messenger. You can use it to send and receive messages. In the command line window that starts jMSN, You can see detailed information about the communication between jMSN and the server.

We will introduce the general situation of j MSN and what functions it can accomplish.

Next, let's get to know how to use the APIS provided by jMSN:

Msnm-lib to implement these functions.

Is the relationship between msnm-lib and jMSN, including the MSN system, that is to say, we can use msnm-lib to complete communication with the MSN server without worrying about the details of specific communication protocols. In fact, msnm-lib has done more things for us to make it very simple to use msnm-lib to develop an MSN application, this is because we mentioned above that we can not go to the Korean API documentation provided by it, because it is too simple to use it.

 

   Minnan says: It's not worth it! Now we start to develop our own JAVA-based cross-platform MSN client program. I believe that when I hear this sentence, everyone will feel the blood bloomed. That's right. What else is more exciting than writing a program? Besides, it is based on JAVA and cross-platform! Let's first provide a piece of code that can run to complete the simplest function: When someone adds it to a friend, the program automatically adds it to a friend. When someone sends a message to it, the program automatically returns the same message. OK. The code for completing 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 program * @ 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 the Ctrl + C input to log out of MSN's logon Runtime. getRuntime (). addShutdownHook (new MSNDaemon ();}/*** user abort 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 information */public void progressTyping (SwitchboardSession ss, MsnFriend friend, String typingUser) {System. out. println (friend. getLoginName () + "entering information... ");}/*** execute this method when receiving the message */public void instantMessageReceived (SwitchboardSession ss, MsnFriend friend, MimeMessage mime) {System. out. print ("received message:" + friend. getFriendlyName () + "->"); System. out. println (mime. getMessage (); try {// send the same reply message to the sender messenger. sendMessage (friend. getLoginName (), mime);} catch (Exception e) {e. printStackTrace () ;}}/*** run this method after successful logon */public void loginComplete (MsnFriend own) {System. out. println (own. getLoginName () + "Login OK");}/*** run this method after logon fails */public void loginError (String header) {System. out. println ("Login Failed:" + header);}/*** this method is executed when a friend is offline */public void userOffline (String loginName) {System. out. println ("USER" + loginName + "Logout. ");}/*** this method is executed when a friend goes online */public void userOnline (MsnFriend friend) {System. out. println ("USER" + friend. getFriendlyName () + "Login. ");}/*** when someone adds me as a friend, execute */public void whoaddedimethyl (MsnFriend friend) {System. out. println ("USER" + friend. getLoginName () + "Addme. "); try {messenger. addFriend (friend. getLoginName ();} catch (Exception e) {e. printStackTrace ();}}

       /*** Execute */public void whoRemovedMe (MsnFriend friend) {System. out. println ("USER" + friend. getLoginName () + "Remove me. "); try {messenger. removeFriend (friend. getLoginName ();} catch (Exception e) {e. printStackTrace ();}}}

In addition to the two commonly used objects MsnFriend and MimeMessage, they are used to indicate my friends and MSN information. What else we need to know is MSNMessenger and MsnAdapter. Of course, the premise is that we do not need other functions except chat, such as file transmission. MSNMessenger corresponds to a logon session of an account. We only need to tell the MSNMessenger class the account, password, initial status after logon, and how to process any information we receive from the MSN server. In msnm-lib, an MsnAdapter class is used to process MSN information. This class defines how to handle events such as receiving messages and adding me as friends, developers can reload these methods for self-processing. Besides, they are still JAVA-based and cross-platform! Let's first provide a piece of code that can run to complete the simplest function: When someone adds it to a friend, the program automatically adds it to a friend. When someone sends a message to it, the program automatically returns the same message. OK. The code for completing 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 program * @ 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 the Ctrl + C input to log out of MSN's logon Runtime. getRuntime (). addShutdownHook (new MSNDaemon ();}/*** user abort 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 information */public void progressTyping (SwitchboardSession ss, MsnFriend friend, String typingUser) {System. out. println (friend. getLoginName () + "entering information... ");}/*** execute this method when receiving the message */public void instantMessageReceived (SwitchboardSession ss, MsnFriend friend, MimeMessage mime) {System. out. print ("received message:" + friend. getFriendlyName () + "->"); System. out. println (mime. getMessage (); try {// send the same reply message to the sender messenger. sendMessage (friend. getLoginName (), mime);} catch (Exception e) {e. printStackTrace () ;}}/*** run this method after successful logon */public void loginComplete (MsnFriend own) {System. out. println (own. getLoginName () + "Login OK");}/*** run this method after logon fails */public void loginError (String header) {System. out. println ("Login Failed:" + header);}/*** this method is executed when a friend is offline */public void userOffline (String loginName) {System. out. println ("USER" + loginName + "Logout. ");}/*** this method is executed when a friend goes online */public void userOnline (MsnFriend friend) {System. out. println ("USER" + friend. getFriendlyName () + "Login. ");}/*** when someone adds me as a friend, execute */public void whoaddedimethyl (MsnFriend friend) {System. out. println ("USER" + friend. getLoginName () + "Addme. "); try {messenger. addFriend (friend. getLoginName ();} catch (Exception e) {e. printStackTrace ();}}
* Executed when someone deleted me from my friend list

*/

Public void whoRemovedMe (MsnFriend friend ){

System. out. println ("USER" + friend. getLoginName () + "Remove me .");

Try {

Messenger. removeFriend (friend. getLoginName ());

}

Catch (Exception e ){

E. printStackTrace ();

}

}

}
In addition to the two commonly used objects MsnFriend and MimeMessage, they are used to indicate my friends and MSN information. What else we need to know is MSNMessenger and MsnAdapter. Of course, the premise is that we do not need other functions except chat, such as file transmission. MSNMessenger corresponds to a logon session of an account. We only need to tell the MSNMessenger class the account, password, initial status after logon, and how to process any information we receive from the MSN server. In msnm-lib, an MsnAdapter class is used to process MSN information. This class defines how to handle events such as receiving messages and adding me as friends, developers can reload these methods for self-processing.   

 

The MsnAdapter class we need to expand on our own must be known to the MSNMessenger instance, which is the msn in our previous code. addMsnListener (new MSNAdapter (msn); The MsnAdapter class is expanded by itself to process passive messages, such as sending messages to me. MSNMessenger instances are needed when we want to send messages to others, which is why we want to pass MSNMessenger instances to MSNAdapter, because when we receive any message, we need to reply the same message to the sender.

Now, the simple functions we have proposed have been completed. You can test them on your own machine. You need to use the msnm-lib library, that is, the msnm. jar file, to run the program. Is a runtime:

 

Chat with multiple users

Another good feature of MSN is that many people chat at the same time. msnm-lib also supports this feature very well. The first parameter type of the instantMessageReceived method defined in MsnAdapter is SwitchboardSession. When a message is received, we can obtain a session ID for multi-user chat from this parameter, and read all the friends who participate in the current chat through getMsnFriends. To actively send messages, you must read all your friends from SwitchboardSession and send them one by one.

About File Transfer

This may be the only problem I have found about msnm-lib, or it is still incomplete. After testing, we found that the two machines that can normally transmit files using Microsoft's MSN program cannot be transmitted using jMSN. The error message is that the connection times out and the two machines are not in the same subnet. I believe that msnm-lib does not process this function. Since there are no two machines directly connected to the Internet, there is no way to perform a test on jMSN file transmission. We hope the new version of msnm-lib can solve this problem.

Summary

Despite some flaws in file transmission, the features provided by msnm-lib are already amazing. At least when I first saw it, I said: Yes, this is what I want! This article aims to introduce how to use msnm-lib to complete a simple MSN client. If you want it to play a role in the actual application system, readers will certainly have more ideas than I do, for example, whether it can be used to enrich the customer service channels, and so on, of course, these are beyond our questions.

Related Article

Contact Us

The content source of this page is from Internet, which doesn't represent Alibaba Cloud's opinion; products and services mentioned on that page don't have any relationship with Alibaba Cloud. If the content of the page makes you feel confusing, please write us an email, we will handle the problem within 5 days after receiving your email.

If you find any instances of plagiarism from the community, please send an email to: info-contact@alibabacloud.com and provide relevant evidence. A staff member will contact you within 5 working days.

A Free Trial That Lets You Build Big!

Start building with 50+ products and up to 12 months usage for Elastic Compute Service

  • Sales Support

    1 on 1 presale consultation

  • After-Sales Support

    24/7 Technical Support 6 Free Tickets per Quarter Faster Response

  • Alibaba Cloud offers highly flexible support services tailored to meet your exact needs.