TA的每日心情 | 衰 2021-2-2 11:21 |
---|
签到天数: 36 天 [LV.5]常住居民I
|
在下根据网上的相关资料,摸索着写了一个DWR框架推模式的小聊天室,可实现私聊功能。 % j5 t4 N! V3 S1 N- T* C
功能基本正常,但是偶尔会出现信息显示不同步的问题,现将源码奉上,希望各位高手能指点一二。ChatManager.java
7 U1 |: s) p1 w. j+ |! V8 Y) @
6 u, ^3 H; u5 Q4 Z) P8 d% A- package services;) p! [" m# A( Z6 G% |) j0 X5 i- b
- 6 i! Y: n( _/ h) L: T
- import java.util.ArrayList;, s/ m2 \, o: \
- import java.util.Collection;
& Z$ G9 l, i; U8 o- L6 R9 O0 p k$ G - import java.util.List;
. y, H, |) `$ Y( ]- M+ I9 E& v - 6 @( P& J4 v" M5 l3 ~4 R$ y+ d
- import javax.servlet.http.HttpServletRequest;
0 K3 [" ?3 t+ a! K4 E - import javax.servlet.http.HttpSession;
+ J2 s2 y" R: Y
8 q- |6 S5 d. t0 }( r, I- import org.directwebremoting.ScriptSession;: D8 M! R$ `9 _
- import org.directwebremoting.WebContext;3 o) `4 Y* g, P3 z) E. a1 A, ^: F* x
- import org.directwebremoting.WebContextFactory;# B8 h3 E( D" s! N9 M7 j
- import org.directwebremoting.proxy.dwr.Util; h5 M r& m- E6 P# R: k
. J: G( z6 i, w: x4 Z0 Q- import pojo.MessageInfo;
! o1 q& H) D# \/ q g: w - import pojo.UserInfo;
: U- c" t+ r( e1 w# S - % S1 Z0 R. Y8 a/ J
- public class ChatManager {2 f5 H8 Z- R; e4 @' _( x
- public static List<UserInfo> onlineUsersList=new ArrayList<UserInfo>();
5 v4 g. J9 O+ F/ g8 ]3 } - public static List<UserInfo> sendUserList=new ArrayList<UserInfo>();" a% s6 \% q* M
- public static List<MessageInfo> messageList=new ArrayList<MessageInfo>();
0 M6 Y5 \4 ?4 q( w- B$ c$ m - public ChatManager(){
4 F H# C5 Q, J - UserInfo userInfo = new UserInfo("0","所有人"); N; C) K$ S$ t7 s( ?( o
- sendUserList.add(userInfo);+ g5 D+ I* O/ U8 U4 Y y9 L$ x
- }
- \9 F. Q6 S+ A# [3 [ - + H9 Q" |0 _7 L b3 N7 Y" i
- public String updateOnlineUsers(String userName,boolean flag) {
3 `. U) }$ u+ o4 n x( r, r: g - UserInfo userInfo=null;9 _2 j3 a- L; X
- WebContext context=WebContextFactory.get();
$ d/ l' j4 Q, w* x' E: [- U5 b - HttpSession session = context.getSession();
* N! i9 G4 e8 D2 L$ J& ? - if (flag) {; p) Z1 P& z( @
- userInfo = new UserInfo(session.getId(),userName);" `$ [& W0 N' A2 [0 p
- onlineUsersList.add(userInfo);( ?. J9 d, V1 g) M; p! X9 U
- sendUserList.add(userInfo);- P. L- O. l( r4 x6 A3 ?& T
- this.setScriptSessionFlag(userInfo);$ u8 D5 V# m! P
- } l- _1 }% c; ]- l
- String currentPage = "/chat/index.jsp";
t1 g' k6 e+ } }: P - Collection sessionsByPage = context.getScriptSessionsByPage(currentPage);
/ r `. x/ D& n1 O+ N - Util allPageUtil = new Util(sessionsByPage);
. ?) x9 L, J( S U' l+ s - allPageUtil.removeAllOptions("onlineList");
. p2 [- f: W T) `. a6 R - allPageUtil.addOptions("onlineList", onlineUsersList, "userName");
1 Y" G* N- ]6 V - allPageUtil.removeAllOptions("userList");
3 O: t. | X* N8 ]* c - allPageUtil.addOptions("userList", sendUserList, "userId", "userName");
- ?% ]/ Z% Q9 F/ ~ - if (!flag) {. g5 F5 B# [6 _0 S4 e
- return null;
8 X$ ~2 Q1 s4 J g5 o. I! e - }/ {8 C$ N0 K8 L* @
- return userInfo.getUserId();# ~3 T! p) E! ? \4 E
- }/ `5 K/ R+ r/ k
-
, E) ]0 W6 g- Y - public void setScriptSessionFlag(UserInfo userInfo) {
- d" i; j, G" A( ~5 v6 [ - WebContextFactory.get().getScriptSession().setAttribute("userInfo", userInfo);! C* ~% V; R$ l
- }6 g: o' |3 A/ Y: G2 T% q* W
-
B+ ]7 o2 h; L/ V) }* N - public Collection<ScriptSession> getScriptSessionFlag(String userId) { y# N9 ]. g* R. Z% O2 f. j
- Collection<ScriptSession> list=new ArrayList<ScriptSession>();
0 e5 J# Y6 y9 y9 ~5 p2 F# e - Collection<ScriptSession> sessions=new ArrayList<ScriptSession>();! _% ~* y# d/ H8 y& k4 c+ y& J: E
- WebContext context = WebContextFactory.get();' m, a( r2 J* }7 n
- String currentPage = "/chat/index.jsp";
7 {# C" N x4 @+ q% N/ O z - Collection sessionsByPage = context.getScriptSessionsByPage(currentPage);
1 p5 |! z% t8 Q5 ^! p' N1 N4 \ - sessions.addAll(sessionsByPage);
h* A# O+ R a - if ("0".equals(userId)) {
, Q; j, j; c: _& U _# s - list=sessions;
5 y+ G/ g! K) ? - }else{' [- A( R Y0 n, c% K, L! j
- for (ScriptSession session : sessions) {0 M" c& |( Q+ x- z* X9 {
- UserInfo userInfo=(UserInfo) session.getAttribute("userInfo");) C# H( z& b7 I7 P7 M
- if (userId.equals(userInfo.getUserId())) {1 ^+ y8 G& T6 }0 y/ X+ P, [
- list.add(session);
* U" A/ `, A& d& ^% G7 m - break;
3 [% k8 V. Y* N3 {6 v# | - }
- v! o0 R/ |' T; w9 q - }' C `% G- ^# O3 R
- }
) V9 a) { ^8 l0 k) B4 ?# j - return list;
2 N) r- b0 i2 c8 F+ ^ - }
4 Z t; w" H! H( A } - 9 @) K/ c4 Y+ X
- public void sendMessage(String sender,String receiverId,String msg) {- J7 {* e2 S* V
- Collection<ScriptSession> scriptSessionFlag = this.getScriptSessionFlag(receiverId);! S0 w8 {/ R3 R' w3 ]2 k; }
- MessageInfo messageInfo = new MessageInfo(sender,receiverId,msg);
, u( J0 V S/ G - messageList.add(messageInfo);
' n+ w0 {; R$ s. Y4 E - Util receiverUtil = new Util(scriptSessionFlag);( j) c" D+ r* I
- receiverUtil.removeAllOptions("messageList");
, e2 j5 B' Z7 t9 b( y9 n z( s) u D - receiverUtil.addOptions("messageList", messageList, "msg");0 l7 t$ ^2 y- T# p: I1 @
- }
) _! Z& Z# }+ T+ X N& h - }
, b0 |3 y: b. K. W N
复制代码 : E' h/ t e3 P* q
项目源码下载地址:点击下载% h: c4 w# f! M! g
! Y+ V; L8 Z/ y7 ]# y3 U' N |
|