|
楼主 |
发表于 2007-3-29 17:33:46
|
显示全部楼层
用到的几个代码
SCListener.java
-------------------------------
package qiy.qq;
import javax.servlet.*;
public class SCListener implements ServletContextListener {
private MyQQ qq = null;
public void contextInitialized(ServletContextEvent event) {
String status = "[QQ] SCListener监听开启.";
event.getServletContext().log(status);
System.out.println(status);
qq = new MyQQ();
}
public void contextDestroyed(ServletContextEvent event) {
String status = "[QQ] SCListener监听关闭.";
event.getServletContext().log(status);
System.out.println(status);
if(qq != null) {
qq.stop();
}
}
}
----------------------------------
MyQQ.java
-------------------------------------
package qiy.qq;
import edu.tsinghua.lumaqq.qq.QQ;
import edu.tsinghua.lumaqq.qq.QQClient;
import edu.tsinghua.lumaqq.qq.Util;
import edu.tsinghua.lumaqq.qq.beans.QQUser;
import edu.tsinghua.lumaqq.qq.events.QQEvent;
import edu.tsinghua.lumaqq.qq.events.IQQListener;
import edu.tsinghua.lumaqq.qq.net.PortGateFactory;
public class MyQQ implements IQQListener {
private static final long serialVersionUID = 3258417235386053939L;
private int friendQQ = 22****;
private String msg = "QIY-ROBOT启动成功.";
private int retry = 5; //登录重试次数
private QQClient client;
protected MyQQ() {
try {
System.out.println("I'm here");
// 开始创建各种对象,登录,然后发送消息,登出,注意我们不再使用sleep来等待操作完成
// 创建QQClient和QQUser
client = new QQClient();
System.out.println("I'm here2");
client.setConnectionPoolFactory(new PortGateFactory());
System.out.println("I'm here3");
QQUser user = new QQUser(182****, "*******");
// 把自己添加成为QQListener
client.addQQListener(this);
// 设置参数
user.setUdp(true);
client.setUser(user);
client.setLoginServer("58.60.14.33");
client.setRobotMode(true);
client.setRobot(new DummyRobot());
// 登录
System.out.println("[QQ] QIY-ROBOT登录...");
client.login();
} catch (Exception e1) {
e1.printStackTrace();
}
}
public void stop() {
System.out.println("[QQ] QIY-ROBOT登出...");
client.logout();
client.release();
}
/* (non-Javadoc)
* @see edu.tsinghua.lumaqq.qq.events.QQListener#qqEvent
(edu.tsinghua.lumaqq.qq.events.QQEvent)
*/
public void qqEvent(QQEvent e) {
switch(e.type) {
case QQEvent.QQ_LOGIN_SUCCESS:
processLoginSuccess();
break;
case QQEvent.QQ_LOGIN_FAIL:
processLoginFail(e);
break;
case QQEvent.QQ_LOGIN_UNKNOWN_ERROR:
processLoginUnknownError();
break;
/*
case QQEvent.QQ_SEND_IM_SUCCESS:
processSendIMSuccess();
break;
*/
case QQEvent.QQ_CHANGE_STATUS_SUCCESS:
processChangeStatusSuccess();
break;
case QQEvent.QQ_CHANGE_STATUS_FAIL:
processChangeStatusFail();
break;
case QQEvent.QQ_OPERATION_TIMEOUT:
if(e.operation == QQ.QQ_CMD_SEND_IM)
processSendIMFail();
break;
case QQEvent.QQ_KEEP_ALIVE_SUCCESS:
break;
case QQEvent.QQ_CONNECTION_LOST:
System.out.println("[QQ] QQ_CONNECTION_LOST");
if(retry > 0) {
System.out.println("[QQ] 连接丢失, 重新登录(retry=" + retry-- + ")");
try {
client.login();
} catch (Exception e1) {
e1.printStackTrace();
}
}
break;
case QQEvent.QQ_KICKED_OUT_BY_SYSTEM:
if(retry > 0) {
System.out.println("[QQ] QQ号在其他地方登陆, 重新登录(retry=" + retry-- + ")");
try {
client.login();
} catch (Exception e1) {
e1.printStackTrace();
}
} else {
System.out.println("[QQ] QQ号在其他地方登陆, 已达最大重试次数, 退出!");
}
break;
case QQEvent.QQ_ADDED_BY_OTHERS:
System.out.println("[QQ] 有人将我加为好友");
break;
case QQEvent.QQ_ADDED_BY_OTHERS_EX:
System.out.println("[QQ] 有人将我加为好友EX");
break;
case QQEvent.QQ_REQUEST_ADD_ME:
System.out.println("[QQ] 有人请求加我为好友");
break;
case QQEvent.QQ_REQUEST_ADD_ME_EX:
System.out.println("[QQ] 有人请求加我为好友EX");
break;
case QQEvent.QQ_REQUEST_ADD_OTHER_APPROVED_AND_ADD:
System.out.println("[QQ] 有人请求加我为好友时,我同意并且加他为好友");
break;
case QQEvent.QQ_ADD_FRIEND_SUCCESS:
System.out.println("[QQ] 我添加一个好友成功");
break;
}
}
/**
* 初始改变状态失败事件
*/
private void processChangeStatusFail() {
System.out.println("[QQ] QIY-ROBOT状态改变失败,自动登出...");
client.logout();
client.release();
}
/**
* 处理改变状态成功事件
*/
private void processChangeStatusSuccess() {
System.out.println("[QQ] QIY-ROBOT准备就绪.");
client.sendIM(friendQQ, Util.getBytes(msg));
}
/**
* 处理发送消息失败事件
*/
private void processSendIMFail() {
System.out.println("[QQ] 消息发送失败.");
//client.logout();
//client.release();
}
/**
* 处理发送消息成功事件
*/
private void processSendIMSuccess() {
System.out.println("[QQ] 消息发送成功.");
//client.logout();
//client.release();
}
/**
* 处理登陆成功事件
*/
private void processLoginSuccess() {
if(!client.getUser().isLoggedIn()) {
System.out.println("[QQ] QIY-ROBOT登录成功.");
}
}
/**
* 处理登陆密码错误事件
* @param e
*/
private void processLoginFail(QQEvent e) {
System.out.println("[QQ] QIY-ROBOT登录失败(密码错误)");
}
/**
* 处理登陆未知错误事件
*/
private void processLoginUnknownError() {
System.out.println("[QQ] QIY-ROBOT登录失败(未知错误)");
}
}
-------------------------------- |
|