Java implements MSN Messenger features

xiaoxiao2021-03-06  37

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. You can download from http://sourceforge.net/projects/jmsn / site, the home page of the project is basically Korean, including its API documentation. 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.jar

The JMSN login interface and the main window are 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.mimeMsNM.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 the input of Ctrl C to log out the login of the MSN

Runtime.Getruntime (). Addshutdownhook (New Msndaemon ());

}

/ **

* User abort programs 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) {

THISSENGER = Messenger;

}

/ **

* Someone is entering information

* /

Public void progresstyping

SwitchboardSession SS,

MsnFriend Friend,

String type {

System.out.println (Friend.getloginname () "Enter information ...");

}

/ **

* Execute this method when receiving the message

* /

Public void instantMessageReceived

SwitchboardSession SS,

MsnFriend Friend,

MIMEMESSAGE MIME) {

System.out.print ("Receive 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 logincumplete (msnfriend own) {

System.out.println (oon.getloginname () "login ok");

}

/ **

* Execute this method after logging in

* /

Public void loginerror (string header) {

System.out.println ("Login Failed:" Header);

}

/ **

* Do this method when friends are offline

* /

Public void Useroffline (String Loginname) {

System.out.println ("User" Loginname "Logout.");

}

/ **

* Execute this method when friends go online

* /

Public void useronline (msnfriend friend) {

System.out.println ("User" Friend.getFriendlyName () "Login.");

}

/ **

* Some people add me as a friend

* /

Public void whoseddedme (msnfriend friend) {

System.out.println ("User" Friend.getLoginName () "AddME.");

Try {

Messenger.addfriend (Friend.getLoginname ());

} catch (exception e) {

E.PrintStackTrace ();

}

}

/ **

* Someone executed when I removed me from my friends list

* /

Public void whoremovedme (msnfriend friends) {

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 are used to represent my friends and MSN information, others we need to understand is MsnMessenger and Msnadapter. Of course, the premise is that we don't need other features outside the chat, such as file transmission, and more. Class MsnMessenger corresponds to 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. The picture below is a screenshot of the run: About multiplayer chat: MSN has another good function is to chat at the same time, MSNM-lib is very good for this function. The first parameter type of the method INSTANTMESSAGERECEIVED is defined in msnadapter to switchboardSession. When receiving a message, we can get a session ID of multiplayers from this parameter, and you can read all friends who participate in the current chat through getMsnFriends. When you want to send a message, you have to read all your friends from the SwitchboardSession and send them information. About document transfer: It may be the only unique inade with MSNM-LIB, or is not perfect enough. After testing, I found that the two machines using Microsoft's MSN program can be transmitted normally. JMSN cannot be transmitted. The error message is said to be connected, and the two machines are not in the same subnet. I believe that MSNM-LIB does not process this feature. Since there is no two machines directly connected to the Internet, there is no way to make a test with the file transfer of JMSN, I hope the new version of MSNM-LIB can solve this problem. Summary: Although there is a bit flaw in file transfer, the functions provided by MSNM-LIB have been great. At least when I saw it first, I said: Yes, this is what I want! This article is intended to describe how to use MSNM-LIB to complete a simple MSN client, so if it makes it role in the actual application system, the readers must have more ideas than me, such as whether you can use it to enrich Customer service channels, etc., of course, these are more than our topic. If you find any problems in use, you are welcome to study together.

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

New Post(0)