Skip to content

Commit e3a93b0

Browse files
author
Tony
committed
添加发送消息事件
1 parent 9ccd431 commit e3a93b0

File tree

22 files changed

+697
-297
lines changed

22 files changed

+697
-297
lines changed

api/pom.xml

Lines changed: 1 addition & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -21,5 +21,5 @@
2121
</dependency>
2222
</dependencies>
2323

24-
24+
2525
</project>

api/src/main/java/iqq/api/bean/IMMsg.java

Lines changed: 15 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -106,6 +106,21 @@ public void setOwner(IMEntity owner) {
106106
this.owner = owner;
107107
}
108108

109+
@Override
110+
public String toString() {
111+
return "IMMsg{" +
112+
"id=" + id +
113+
", sender=" + sender +
114+
", contents=" + contents +
115+
", date=" + date +
116+
", state=" + state +
117+
", direction=" + direction +
118+
", category=" + category +
119+
", font=" + font +
120+
", owner=" + owner +
121+
'}';
122+
}
123+
109124
/***
110125
*
111126
* 消息状态枚举

api/src/main/java/iqq/api/bean/IMRoomCategory.java

Lines changed: 2 additions & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -1,5 +1,6 @@
11
package iqq.api.bean;
22

3+
import java.util.LinkedList;
34
import java.util.List;
45

56
/**
@@ -9,7 +10,7 @@
910
* License : Apache License 2.0
1011
*/
1112
public class IMRoomCategory extends IMCategory {
12-
private List<IMRoom> roomList;
13+
private List<IMRoom> roomList = new LinkedList<>();
1314

1415
public List<IMRoom> getRoomList() {
1516
return roomList;
Lines changed: 69 additions & 60 deletions
Original file line numberDiff line numberDiff line change
@@ -1,71 +1,80 @@
1-
/*
2-
* Licensed to the Apache Software Foundation (ASF) under one or more
3-
* contributor license agreements. See the NOTICE file distributed with
4-
* this work for additional information regarding copyright ownership.
5-
* The ASF licenses this file to You under the Apache License, Version 2.0
6-
* (the "License"); you may not use this file except in compliance with
7-
* the License. You may obtain a copy of the License at
8-
*
9-
* http://www.apache.org/licenses/LICENSE-2.0
10-
*
11-
* Unless required by applicable law or agreed to in writing, software
12-
* distributed under the License is distributed on an "AS IS" BASIS,
13-
* WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied.
14-
* See the License for the specific language governing permissions and
15-
* limitations under the License.
16-
*/
1+
/*
2+
* Licensed to the Apache Software Foundation (ASF) under one or more
3+
* contributor license agreements. See the NOTICE file distributed with
4+
* this work for additional information regarding copyright ownership.
5+
* The ASF licenses this file to You under the Apache License, Version 2.0
6+
* (the "License"); you may not use this file except in compliance with
7+
* the License. You may obtain a copy of the License at
8+
*
9+
* http://www.apache.org/licenses/LICENSE-2.0
10+
*
11+
* Unless required by applicable law or agreed to in writing, software
12+
* distributed under the License is distributed on an "AS IS" BASIS,
13+
* WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied.
14+
* See the License for the specific language governing permissions and
15+
* limitations under the License.
16+
*/
1717

18-
/**
18+
/**
1919
* Project : IQQ_V2.1
2020
* Package : iqq.app.action
2121
* File : IMEventHandlerProxy.java
2222
* Author : solosky < [email protected] >
2323
* Created : 2013-3-16
24-
* License : Apache License 2.0
24+
* License : Apache License 2.0
2525
*/
2626
package iqq.api.event;
2727

28-
import iqq.api.annotation.IMEventHandler;
29-
import org.slf4j.Logger;
30-
import org.slf4j.LoggerFactory;
28+
import iqq.api.annotation.IMEventHandler;
29+
import org.slf4j.Logger;
30+
import org.slf4j.LoggerFactory;
31+
32+
import java.lang.reflect.Method;
33+
import java.util.HashMap;
34+
import java.util.Map;
35+
36+
/**
37+
* 使用这个类简化事件的注册,分发
38+
* 只需在被代理的类使用@IMEventHandler注解需要处理的事件类型即可
39+
*
40+
* @author solosky <[email protected]>
41+
*/
42+
public class IMEventDispatcher implements IMEventListener {
43+
private static final Logger LOG = LoggerFactory.getLogger(IMEventDispatcher.class);
44+
private Map<IMEventType, Method> methodMap = new HashMap<IMEventType, Method>();
3145

32-
import java.lang.reflect.Method;
33-
import java.util.HashMap;
34-
import java.util.Map;
46+
public IMEventDispatcher() {
47+
for (Method m : this.getClass().getDeclaredMethods()) {
48+
if (m.isAnnotationPresent(IMEventHandler.class)) {
49+
IMEventHandler handler = m.getAnnotation(IMEventHandler.class);
50+
this.methodMap.put(handler.value(), m);
51+
if (!m.isAccessible()) {
52+
m.setAccessible(true);
53+
}
54+
}
55+
}
56+
for (Method m : this.getClass().getMethods()) {
57+
if (m.isAnnotationPresent(IMEventHandler.class)) {
58+
IMEventHandler handler = m.getAnnotation(IMEventHandler.class);
59+
this.methodMap.put(handler.value(), m);
60+
if (!m.isAccessible()) {
61+
m.setAccessible(true);
62+
}
63+
}
64+
}
65+
}
3566

36-
/**
37-
*
38-
* 使用这个类简化事件的注册,分发
39-
* 只需在被代理的类使用@IMEventHandler注解需要处理的事件类型即可
40-
*
41-
* @author solosky <[email protected]>
42-
*
43-
*/
44-
public class IMEventDispatcher implements IMEventListener{
45-
private static final Logger LOG = LoggerFactory.getLogger(IMEventDispatcher.class);
46-
private Map<IMEventType, Method> methodMap = new HashMap<IMEventType, Method>();
47-
public IMEventDispatcher(){
48-
for (Method m : this.getClass().getDeclaredMethods()) {
49-
if(m.isAnnotationPresent(IMEventHandler.class)){
50-
IMEventHandler handler = m.getAnnotation(IMEventHandler.class);
51-
this.methodMap.put(handler.value(), m);
52-
if(!m.isAccessible()){
53-
m.setAccessible(true);
54-
}
55-
}
56-
}
57-
}
58-
@Override
59-
public void onIMEvent(IMEvent imEvent){
60-
Method m = methodMap.get(imEvent.getType());
61-
if(m != null){
62-
try {
63-
m.invoke(this, imEvent);
64-
} catch (Throwable e) {
65-
LOG.warn("invoke IMEventHandler Error!! {}.{}(..)", getClass(), m.getName(), e);
66-
}
67-
}else{
68-
LOG.warn("IMEventHandler Not Found!! imEvent = {}", imEvent);
69-
}
70-
}
71-
}
67+
@Override
68+
public void onIMEvent(IMEvent imEvent) {
69+
Method m = methodMap.get(imEvent.getType());
70+
if (m != null) {
71+
try {
72+
m.invoke(this, imEvent);
73+
} catch (Throwable e) {
74+
LOG.warn("invoke IMEventHandler Error!! {}.{}(..)", getClass(), m.getName(), e);
75+
}
76+
} else {
77+
LOG.warn("IMEventHandler Not Found!! imEvent = {}", imEvent);
78+
}
79+
}
80+
}

api/src/main/java/iqq/api/event/IMEventType.java

Lines changed: 1 addition & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -134,7 +134,7 @@ public enum IMEventType {
134134
RECV_CHAT_MSG, //请求保存消息到历史记录中
135135
SHOW_INFO_MSG, //保存一些信息类的消息
136136
UPDATE_UIMSG, //更新一条消息数据到历史记录中
137-
RECV_QQ_MSG, //接受到原始的消息
137+
RECV_RAW_MSG, //接受到原始的消息
138138
MSG_HISTORY_UPDATE, //消息历史记录更新
139139
MSG_HISTORY_FIND, //查找历史消息记录
140140
MSG_SHOWED, //消息已经显示

bridge-webqq/src/main/java/iqq/bridge/test/TestBridge.java

Lines changed: 63 additions & 6 deletions
Original file line numberDiff line numberDiff line change
@@ -1,9 +1,9 @@
11
package iqq.bridge.test;
22

33
import iqq.api.annotation.IMEventHandler;
4-
import iqq.api.bean.IMAccount;
5-
import iqq.api.bean.IMBuddy;
6-
import iqq.api.bean.IMBuddyCategory;
4+
import iqq.api.bean.*;
5+
import iqq.api.bean.content.IMContentItem;
6+
import iqq.api.bean.content.IMTextItem;
77
import iqq.api.bridge.IMApp;
88
import iqq.api.bridge.IMBridge;
99
import iqq.api.event.IMEvent;
@@ -13,6 +13,8 @@
1313
import org.slf4j.Logger;
1414
import org.slf4j.LoggerFactory;
1515

16+
import java.util.ArrayList;
17+
import java.util.Date;
1618
import java.util.LinkedList;
1719
import java.util.List;
1820

@@ -22,28 +24,54 @@
2224
public class TestBridge extends IMEventDispatcher implements IMBridge {
2325
private final Logger logger = LoggerFactory.getLogger(TestBridge.class);
2426
private IMApp mIMApp;
27+
private IMAccount account;
2528

2629
@Override
2730
public void setApp(IMApp imApp) {
2831
mIMApp = imApp;
2932
}
3033

34+
@IMEventHandler(IMEventType.SEND_MSG_REQUEST)
35+
private void processSendMsgRequest(IMEvent imEvent) {
36+
logger.debug("processSendMsgRequest: " + imEvent.getTarget());
37+
38+
IMBuddy buddy = new IMBuddy();
39+
buddy.setId(123);
40+
buddy.setNick("Test");
41+
42+
IMMsg msg = new IMMsg();
43+
msg.setSender(buddy);
44+
msg.setDate(new Date());
45+
msg.setState(IMMsg.State.READ);
46+
msg.setOwner(account);
47+
48+
List<IMContentItem> contents = new ArrayList<>();
49+
IMTextItem text = new IMTextItem("test content...");
50+
contents.add(text);
51+
msg.setContents(contents);
52+
53+
broadcastIMEvent(IMEventType.RECV_RAW_MSG, msg);
54+
}
55+
3156
@IMEventHandler(IMEventType.LOGIN_REQUEST)
3257
private void processLoginRequestEvent(IMEvent imEvent) {
3358
LoginRequest loginRequest = (LoginRequest) imEvent.getTarget();
3459

35-
IMAccount account = new IMAccount();
60+
account = new IMAccount();
61+
account.setId(10);
3662
account.setLoginName(loginRequest.getUsername());
3763
account.setPassword(loginRequest.getPassword());
3864

3965
broadcastIMEvent(IMEventType.LOGIN_SUCCESS, account);
4066
broadcastIMEvent(IMEventType.CLIENT_ONLINE, account);
4167

4268
doGetBuddyList();
69+
doGetGroupList();
70+
doGetRecentList();
4371
}
4472

4573
private void doGetBuddyList() {
46-
List<IMBuddyCategory> imCategories = new LinkedList<IMBuddyCategory>();
74+
List<IMBuddyCategory> imCategories = new LinkedList<>();
4775
for (int i = 0; i < 10; i++) {
4876
IMBuddyCategory buddyCategory = new IMBuddyCategory();
4977
buddyCategory.setName("Category " + i);
@@ -55,10 +83,39 @@ private void doGetBuddyList() {
5583
}
5684
imCategories.add(buddyCategory);
5785
}
58-
86+
5987
broadcastIMEvent(IMEventType.BUDDY_LIST_UPDATE, imCategories);
6088
}
6189

90+
private void doGetGroupList() {
91+
List<IMRoomCategory> roomCategories = new LinkedList<>();
92+
for (int i = 0; i < 8; i++) {
93+
IMRoomCategory roomCategory = new IMRoomCategory();
94+
roomCategory.setName("Category " + i);
95+
for (int j = 0; j < 10; j++) {
96+
IMRoom room = new IMRoom();
97+
room.setNick("Hello World! " + j);
98+
roomCategory.getRoomList().add(room);
99+
}
100+
roomCategories.add(roomCategory);
101+
}
102+
103+
broadcastIMEvent(IMEventType.GROUP_LIST_UPDATE, roomCategories);
104+
}
105+
106+
private void doGetRecentList() {
107+
List<IMBuddy> buddies = new LinkedList<>();
108+
for (int j = 0; j < 10; j++) {
109+
IMBuddy buddy = new IMBuddy();
110+
buddy.setNick("Tony " + j);
111+
buddy.setSign("Hello World! " + j);
112+
buddies.add(buddy);
113+
}
114+
115+
broadcastIMEvent(IMEventType.RECENT_LIST_UPDATE, buddies);
116+
}
117+
118+
62119
protected void broadcastIMEvent(IMEvent event) {
63120
mIMApp.onIMEvent(event);
64121
}

0 commit comments

Comments
 (0)