Java implementation of MSN Messenger features

Source: Internet
Author: User
Tags command line functions header implement interface joins session id linux
Today's instant messaging software dazzling, we are familiar with nothing more than Tencent's QQ, Microsoft's MSN Messenger and NetEase's bubble, in terms of user volume, the three should be among the top. But Tencent's QQ and NetEase's bubble did not disclose its client-server communication protocol, which makes it difficult for developers to use this huge user group to open up additional service channels. MSN Messenger, an instant messaging software developed by Microsoft, the world's leading software provider, has been quickly accepted by Chinese users with its close integration with Windows operating systems and the entire Microsoft product family, with features such as simplicity, practicality, stability, and universal access, which are now being grown by geometric figures. But what makes developers happy is that the software also provides open APIs and open communication protocols. The famous MSN Plus is a plugin developed using its API to extend the MSN Messenger feature. The jmsn we're going to introduce today is the Java API that encapsulates the communication protocols open by MSN Messenger, which developers can use to simulate the MSN Messenger software in the Java language. The author of the API also provides an MSN client software written in the Java language that is even more powerful than MSN Messenger in one way or another. Because of the Cross-platform Java language development, the software can also run on other operating systems, and has been tested on a variety of Linux systems as well as Mac OS, and of course the Windows operating system.

Jmsn is a South Korean development of the open source API, can be downloaded from the http://sourceforge.net/projects/jmsn/site, the project's homepage is basically Korean-dominated, including its API documentation is the description of the Korean language. This makes me very headache, but it does not matter, because JMSN is very simple, if there is no special circumstances do not look at those instructions and it does not matter. Jmsn's home page offers two parts for download as shown in the following illustration, where Jmsn is a complete Java application that can be run directly after decompression and run in a similar interface to Microsoft's MSN Messenger, including very consistent operations. If your operating system is Linux or anything else, you can use it directly to replace the Microsoft program. The other is Msnm-lib, which is the API we're going to cover today, and it's just a development package that already contains the package in the Jmsn component.





You might want to experience Jmsn's own program to see what you can do with it. There will be an executable file in the directory after extracting the JMSN compressed package, but if your JDK is not installed using Setup, it is recommended that you do not execute it and it will not find the JRE. You can use the command line to start the program, which has the advantage that you can also see the information printed in the run.

To start the JMSN command:

Java-jar Jmsn.jar



The Jmsn login interface and main window are shown in the following illustration:









It should be said that this interface is very similar to MSN Messenger. Users can send and receive messages through it, and so on. You can see the details of communication between JMSN and the server in the command line window that starts jmsn.

In front of us mainly in the introduction of jmsn about the situation, describe what it can accomplish what kind of function. Here we begin to understand how to use Jmsn's own api:msnm-lib to implement these functions. The following figure is the relationship between Msnm-lib and jmsn including the MSN System, which means that we can communicate with the MSN server through Msnm-lib without having to worry about the specifics of the communication protocol. The fact that Msnm-lib has done more for us makes it very easy for us to use Msnm-lib to develop an MSN application, which is what I mentioned earlier that we could not possibly have the Korean API documentation that it provided, because it was so easy to use.





Minnan says: "It's not worth the money!" With all that gossip, now we're starting to develop our own java-based cross-platform MSN client program. I believe that everyone will feel the blood swelling, yes, what is more exciting than to write the program? Not to mention the Java-based, Cross-platform!

Let's start with a piece of code that runs the simplest function: When someone joins a friend, the program automatically joins the friend, and when someone sends it a message, the program automatically responds with the same message. OK, the code to complete such a simple feature 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 input of CTRL + C to unregister MSN logins
Runtime.getruntime (). Addshutdownhook (New Msndaemon ());
}
/**
* User Aborts program execution
*/
public void Run () {
Msn.logout ();
System.out.println ("MSN Logout OK");
}
}
/**
* MSN Message Event handling 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 ();
}
}
/**
* Execute this method after successful login
*/
public void Logincomplete (Msnfriend own) {
System.out.println (Own.getloginname () + "Login OK");
}
/**
* Execute this method after login failure
*/
public void Loginerror (String header) {
System.out.println ("Login Failed:" + header);
}
/**
* Execute this method when friends are offline
*/
public void Useroffline (String loginName) {
System.out.println ("USER" + LoginName + "Logout.");
}
/**
* Implement this method when friends are online
*/
public void Useronline (Msnfriend friend) {
System.out.println ("USER" +friend.getfriendlyname () + "Login.");
}
/**
* When someone adds me to my best friend
*/
public void Whoaddedme (Msnfriend friend) {
System.out.println ("USER" + friend.getloginname () + "AddMe.");
try {
Messenger.addfriend (Friend.getloginname ());
catch (Exception e) {
E.printstacktrace ();
}
}
/**
* Someone took me out when I was removed from my buddy 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 common objects Msnfriend and mimemessage used to represent my friends and MSN information, other things we need to know are Msnmessenger and msnadapter. Of course, the premise is that we do not need other than chat functions, such as file transfer and so on. Class Msnmessenger A login session that corresponds to an account number. We just need to tell the Msnmessenger class the account number, the password, the initial state of the login, and how we can handle any information received from the MSN server. In Msnm-lib, the processing of MSN information is handled by a class called Msnadapter, which defines how to handle events such as receiving messages, adding my friends, and so on, and developers can overload these methods for their own processing. Our own extended Msnadapter class must tell the Msnmessenger instance that this is the Msn.addmsnlistener (new Msnadapter (MSN)) in our previous code. Msnadapter classes are used to handle passive messages, such as when someone sends messages to me. When we want to send the message to others, we need to use the Msnmessenger instance, which is why we have to pass the instance of Msnmessenger to Msnadapter, because when we receive any message, we need to reply to the sender with the same information.

The simple function we proposed earlier is complete, the reader can test on his own machine, the runtime needs to use the Msnm-lib library, that is, the Msnm.jar file. The following figure is a screenshot of the runtime:






Chat about multiple people:

MSN has another good feature is that many people chat at the same time, msnm-lib support for this function is also very good. The first parameter type of the method instantmessagereceived defined in Msnadapter is switchboardsession. When the message is received, we can get a session ID from this parameter for many people chatting, and can read all the friends who participate in the current chat through Getmsnfriends. When you want to send a message actively, you must read all the friends from the switchboardsession and send them messages.

About file Transfers:

Maybe that's the only thing I've found out about Msnm-lib, or the part that's not perfect. After testing found that the use of Microsoft's MSN program can normally transfer files of the two machines with JMSN but unable to transmit, the error message is that the connection timed out, the two machines are not the same subnet. It is believed that Msnm-lib does not handle this function. Since there are no two machines directly connected to the Internet, there is no way to do a test on the jmsn file transfer, hoping that the new version of Msnm-lib will solve the problem.

Summarize:

Although there is a slight flaw in the file transfer, the functionality provided by Msnm-lib has been great, at least when I first saw it, I said, "Yes, that's what I want!" This article is designed to describe how to use Msnm-lib to complete a simple MSN client, and if it works in a real-world application, readers will definitely have more ideas than I do, such as whether it can be used to enrich the channel of customer service and so on, which is more than our problem. If any problems are found in use, please write to the common study.





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.