听说Yahoo Messenger开放了协议文档,因此开始有第三方的客户端实现。其中jYMSG是一个Java的Yahoo Messenger通讯API的实现。本页面给出的是一个很简单的基于此API的程序示例,用于帮助理解和测试jYMSG API,而并不是一个完整的Yahoo客户端。当然,jYMSG官方网站也提供了很多示例程序可以参考。

截止2006年3月,jYMSG的最新版本是0.6版本,与Yahoo的协议稍有不兼容,会影响程序运行。有其他网友更新了API中的ymsg_network_v0_6.jar到0.61版本,解决了这个问题。需要的可以搜索一下,新的文件名就是ymsg_network_v0_61.jar 。

程序依赖的库文件共有ymsg_support_v0_6.jar、ymsg_test_v0_6.jar、ymsg_network_v0_61.jar三个包。


jYMSG的事件监听类:MySessionListener.java

import java.awt.Toolkit;

import javax.swing.JTextArea;

import ymsg.network.LoginRefusedException;
import ymsg.network.event.SessionChatEvent;
import ymsg.network.event.SessionConferenceEvent;
import ymsg.network.event.SessionErrorEvent;
import ymsg.network.event.SessionEvent;
import ymsg.network.event.SessionExceptionEvent;
import ymsg.network.event.SessionFileTransferEvent;
import ymsg.network.event.SessionFriendEvent;
import ymsg.network.event.SessionListener;
import ymsg.network.event.SessionNewMailEvent;
import ymsg.network.event.SessionNotifyEvent;

public class MySessionListener implements SessionListener
{
    private YahooClient ss;
    public MySessionListener(YahooClient ss)
    {
        this.ss = ss;
    }

    public void buzzReceived(SessionEvent ev)
    {        
        Toolkit.getDefaultToolkit().beep();
        System.out.println("buzzReceived");
    }
    public void chatConnectionClosed(SessionEvent ev)
    {
        System.out.println("chatConnectionClosed");
    }
    public void chatLogoffReceived(SessionChatEvent ev)
    {
        System.out.println("chatLogoffReceived");
    }
    public void chatLogonReceived(SessionChatEvent ev)
    {
        System.out.println("chatLogonReceived");
    }
    public void chatMessageReceived(SessionChatEvent ev)
    {
        System.out.println("chatMessageReceived");
    }
    public void chatUserUpdateReceived(SessionChatEvent ev)
    {
        System.out.println("chatUserUpdateReceived");
    }
    public void conferenceInviteReceived(SessionConferenceEvent ev)
    {
        System.out.println("conferenceInviteReceived");    
    }
    public void conferenceInviteDeclinedReceived(SessionConferenceEvent ev)
    {
        System.out.println("conferenceInviteDeclinedReceived");
    }
    public void conferenceLogoffReceived(SessionConferenceEvent ev)
    {
        System.out.println("conferenceLogoffReceived");
    }
    public void conferenceLogonReceived(SessionConferenceEvent ev)
    {
        System.out.println("conferenceLogonReceived");
    }
    public void conferenceMessageReceived(SessionConferenceEvent ev)
    {
        System.out.println("conferenceMessageReceived");
    }
    public void connectionClosed(SessionEvent ev)
    {
        System.out.println("Connection Closed");
    }
    public void contactRejectionReceived(SessionEvent ev)
    {
        System.out.println("contactRejectionReceived");
    }
    public void contactRequestReceived(SessionEvent ev)
    {
        System.out.println("contactRequestReceived");
    }
    public void errorMessageReceived(SessionErrorEvent ev)
    {
        System.out.println("Error Message Received");
    }
    public void fileTransferReceived(SessionFileTransferEvent ev)
    {
        System.out.println("fileTransferReceived");
    }
    public void friendAddedReceived(SessionFriendEvent ev)
    {
        System.out.println("friendAddedReceived");
    }
    public void friendRemovedReceived(SessionFriendEvent ev)
    {
        System.out.println("friendRemovedReceived");
    }
    public void friendsUpdateReceived(SessionFriendEvent ev)
    {
        System.out.println("friendsUpdateReceived");
    }
    public void inputExceptionThrown(SessionExceptionEvent ev)
    {
        System.out.println("Input Exception Thrown");
    }
    public void listReceived(SessionEvent ev)
    {
        System.out.println("listReceived");
    }
    public void messageReceived(SessionEvent ev)
    {
        Toolkit.getDefaultToolkit().beep();
        System.out.println("messageReceived");
        this.ss.add();
        System.out.println(ev.getFrom() + ": " + ev.getMessage()+'\n');
    }
    public void newMailReceived(SessionNewMailEvent ev)
    {
        System.out.println("newMailReceived");
    }
    public void notifyReceived(SessionNotifyEvent ev)
    {
        System.out.println("notifyReceived");
    }
    public void offlineMessageReceived(SessionEvent ev)
    {
        System.out.println("offlineMessageReceived");
    }
    public void errorPacketReceived(SessionErrorEvent sev)
    {
        System.out.println("Error Packet Received");
    }
}

客户端实现类YahooClient.java

import ymsg.network.*;
import ymsg.support.*;
import ymsg.network.event.*;

import java.io.*;
import java.util.Properties;

public class YahooClient {

    private String pollRob = null;
    private String message = null;
    private int waitingTime = 0;    //每一步操作的延迟时间;
    private int timeOut = 0;    //发送消息后等待的超时时间;
    private int count = 0;  //临时变量,反映是否有需要接收的消息;
    private int sendTime = 0;   //每个帐号要发送的消息次数;
    private String accFile = "account.txt"; //存储用户名密码的文件名;
    private String errFile = "err.txt"; //存储出错用户名密码的文件名;
    private String toFile = "timeout.txt";  //存储超时用户名密码的文件名;

    public YahooClient(){
        Properties p=new Properties();
        try{
            FileInputStream sf=new FileInputStream("sys.properties");
            p.load(sf);
            this.pollRob=p.getProperty("PollRob");
            this.message=p.getProperty("Message");
            this.waitingTime=1000*Integer.parseInt(p.getProperty("WaitingTime"));
            this.timeOut=1000*Integer.parseInt(p.getProperty("TimeOut"));
            this.sendTime=Integer.parseInt(p.getProperty("SendTime"));
            System.out.println(this.pollRob+" "+this.message+" "+this.waitingTime+" "+this.timeOut+" "+this.sendTime);
        }catch(Exception e){
            System.out.println(e);
        }
    }

    public void myWait(){
        try { Thread.sleep(this.waitingTime); } catch(InterruptedException e) {
            System.out.println(e);
        }
    }

    public boolean check(){
        if (this.count>0){
            return true;
        }else{
            return false;
        }
    }

    public void add(){
        this.count++;
    }

    public void minus(){
        this.count--;
    }

    public static void main(String[] args)
    {
        YahooClient ss = new YahooClient();
//        System.out.println("test start");
        ymsg.network.Session  yahooMessengerSession = null;
        MySessionListener mySessionListener = null;
        // 准备存储用户名密码的文件:
        try{
            FileReader fr=new FileReader(ss.accFile);
            BufferedReader br=new BufferedReader(fr);
            String line=br.readLine();
            FileWriter fw=new FileWriter(ss.errFile);
            FileWriter tw=new FileWriter(ss.toFile);
            int a;
            String name=null;
            String pass=null;
            boolean hasError=false;
            boolean hasTimeOut=false;
            while(line!=null){
                // 读取并处理一组用户名密码:
                hasError=false;
                hasTimeOut=false;
                a=line.indexOf(",");
                name=line.substring(0,a);
                name=name.trim();
                pass=line.substring(a+1,line.length());
                pass=pass.trim();
                System.out.println(name+":"+pass);
                // 模拟雅虎通发消息:
                yahooMessengerSession = new ymsg.network.Session();
                mySessionListener = new MySessionListener(ss);
                yahooMessengerSession.addSessionListener(mySessionListener);
                try
                {
                    // 登陆:
                    yahooMessengerSession.login(name, pass);
                    ss.myWait();
                    // 发送ss.sendTime条消息:
                    for(int i=0;i<ss.sendTime;i++){
                        ss.count=0;
                        yahooMessengerSession.sendMessage(ss.pollRob, ss.message);
                        System.out.println("Sent message "+(i+1)+" :");
                        // 检查是否在ss.timeOut时间内系统回复消息:
                        int j=0;
                        for(j=0;j<ss.timeOut;j+=ss.waitingTime){
                            ss.myWait();
                            if (ss.check()){
                                break;
                            }
                        }
                        if (j>=ss.timeOut){
                            hasTimeOut=true;
                        }
                    }
                }catch (Exception e){
                    System.out.println("====");
                    System.out.println(e);
                    hasError=true;
                }
                // 登出当前帐号:
                ss.myWait();
                try{
                    yahooMessengerSession.logout();
                }catch (Exception e){
                    System.out.println("====");
                    System.out.println(e);
                    hasError=true;
                }                
                line=br.readLine();
                if (hasError) {
                    fw.write(name+","+pass+"\r\n");
                }
                if (hasTimeOut) {
                    tw.write(name+","+pass+"\r\n");
                }
            }
            tw.close();
            fw.close();
            br.close();
            fr.close();
        }catch(Exception e){
            System.out.println("No account file or other I/O ERROR!");
        }
        System.out.println("Finished!");
    }
}
GlossyBlue theme adapted by David Gilbert
Powered by PmWiki