TA的每日心情 | 衰 2021-2-2 11:21 |
---|
签到天数: 36 天 [LV.5]常住居民I
|
在下根据网上的相关资料,摸索着写了一个DWR框架推模式的小聊天室,可实现私聊功能。
! K$ ~* e1 N$ x% y: x3 w功能基本正常,但是偶尔会出现信息显示不同步的问题,现将源码奉上,希望各位高手能指点一二。ChatManager.java
4 c7 A; ?- O& Q( S% `: N* U- u* V' v: c, C1 f- t
- package services;
. y+ e" C7 a' H" k+ E; w% R - ( P: m. C$ p7 g, B2 f
- import java.util.ArrayList;
9 |% b: w/ |. N- e A) Y - import java.util.Collection;
" I* F; X' l. e* p7 o - import java.util.List;
$ U0 X/ ^) O) y+ U! }8 s% u
9 d9 z8 G. J* F; K3 ]- import javax.servlet.http.HttpServletRequest;
& \6 a4 V2 C. `! }8 r" Y( ?) _1 U - import javax.servlet.http.HttpSession;
. S$ J- s3 Q2 w6 A7 t- d+ r
% j; |6 @: o9 I1 z- import org.directwebremoting.ScriptSession;: c$ g0 x( @: p: @# m9 h# ]& n! E
- import org.directwebremoting.WebContext; u5 M9 a2 y* O5 v
- import org.directwebremoting.WebContextFactory;
# Z, s2 I+ j7 p- |" f9 U - import org.directwebremoting.proxy.dwr.Util;
0 u( A' A2 f/ m3 Y - 2 }/ X# H& {- T" v$ i, Y1 C; p4 P
- import pojo.MessageInfo;, U+ M4 r+ c" Z0 S. B* N3 r
- import pojo.UserInfo;
7 H" r9 Z7 X8 w) [5 M/ P! b% s
2 l4 m3 X6 b: s9 \0 B0 L) L* V- public class ChatManager {9 D6 K8 B+ u* C$ [2 m' Y
- public static List<UserInfo> onlineUsersList=new ArrayList<UserInfo>();- U; K6 d+ b+ T0 [1 I
- public static List<UserInfo> sendUserList=new ArrayList<UserInfo>();6 m% |- @. K+ G8 f0 F2 M
- public static List<MessageInfo> messageList=new ArrayList<MessageInfo>();" P* E; w8 k8 t
- public ChatManager(){
" f% d2 O7 T) C/ O' u: c( i9 H1 [. x - UserInfo userInfo = new UserInfo("0","所有人");
) W* g* T" ~1 {/ e - sendUserList.add(userInfo);
% Z1 f: a, |$ n( N! h - }' u l0 s/ y* X" @. V
- . a- K2 Z5 @( ?9 d K c, A
- public String updateOnlineUsers(String userName,boolean flag) {/ V6 I/ O- q3 W3 T* Y* n3 r# [ [
- UserInfo userInfo=null;
( h3 t7 Y' L( p: C! K - WebContext context=WebContextFactory.get();* e' g6 L4 y4 P R- I: o# D$ j2 k6 X) Z
- HttpSession session = context.getSession();2 |) V' I" l" \8 A0 x
- if (flag) {$ N1 C" o" b( M
- userInfo = new UserInfo(session.getId(),userName);# i# L* j0 ?# r7 X' M Y3 O. z a& T
- onlineUsersList.add(userInfo);, |* n* o9 [% m4 C( x. m
- sendUserList.add(userInfo);( M+ n& ~+ a+ w
- this.setScriptSessionFlag(userInfo);
+ T- ?+ u3 g" T3 ?. G; U - }
5 r. Z1 c8 V. R h - String currentPage = "/chat/index.jsp";9 W& ]7 D9 K$ v. _6 O9 J2 b
- Collection sessionsByPage = context.getScriptSessionsByPage(currentPage);
+ M) j' A; e6 J( S. H5 n' D - Util allPageUtil = new Util(sessionsByPage);) V4 p* R* r5 B# a. l4 _
- allPageUtil.removeAllOptions("onlineList");; W9 ?2 ^. c: a. B$ c
- allPageUtil.addOptions("onlineList", onlineUsersList, "userName");3 U0 B) i$ u$ G7 G# k& |( v0 v
- allPageUtil.removeAllOptions("userList");: h9 I& [* j, R' G
- allPageUtil.addOptions("userList", sendUserList, "userId", "userName");& Y. d+ j# ]4 o
- if (!flag) {
5 A5 u8 P$ N6 X6 `8 D6 P+ ` - return null;7 B$ l7 r: n/ b7 J# z3 R
- }
' t0 Z' O0 N: z1 \4 U0 i - return userInfo.getUserId();# j" Y/ t. M4 y
- }$ R" e9 [- l$ C1 j, a, Z2 q
-
) n# d2 q8 ?8 `: A - public void setScriptSessionFlag(UserInfo userInfo) {1 a v( i2 a$ s+ `, x2 I
- WebContextFactory.get().getScriptSession().setAttribute("userInfo", userInfo);
y/ i8 H4 A/ Z5 Q) c$ G2 Y - }
! w# U* {+ S' B1 C; P! | -
2 ? q8 Q7 ~- a - public Collection<ScriptSession> getScriptSessionFlag(String userId) {* l& T, h' H4 O- \+ C
- Collection<ScriptSession> list=new ArrayList<ScriptSession>();
- ]2 H1 ~9 x5 \+ x/ H) B4 j - Collection<ScriptSession> sessions=new ArrayList<ScriptSession>();
9 `6 E; V: o, N' G$ Q0 Y* E - WebContext context = WebContextFactory.get();
! L( A( n9 P/ Y5 ^ - String currentPage = "/chat/index.jsp";! {$ {. h. B+ g$ ?8 O8 p3 f; j8 {
- Collection sessionsByPage = context.getScriptSessionsByPage(currentPage);
; _2 w$ m5 q1 z0 v+ E2 Q - sessions.addAll(sessionsByPage);. t: L7 T5 S' e8 F& s
- if ("0".equals(userId)) {
- ]% x8 _. R; s% b - list=sessions;3 \4 h: r* l/ U8 G6 o6 N% x) i* e
- }else{
" F+ A# P( E- z+ {* W( a - for (ScriptSession session : sessions) {
( J) T* F' C. y F0 o$ H! w - UserInfo userInfo=(UserInfo) session.getAttribute("userInfo");
. J0 y9 E" b! a5 C9 m8 w2 S - if (userId.equals(userInfo.getUserId())) {4 N6 N8 l, T2 r9 u1 e6 ] a
- list.add(session);& e% ]! H$ p4 }) ]2 k- T$ `: L
- break;
+ u+ H& ]3 [; Q0 L$ A3 d3 @, D; }7 T: c - }
% U0 |+ Q7 [- ?& U, c/ n8 ^% F - }6 u% p3 m. } D: h: C0 g
- }) a4 T- a$ Q# h. l3 e4 }
- return list;9 s0 \5 B' Z" y* q2 j. Z% h5 ^
- }
3 n& R+ D O* p' ~8 P - - c' |) }7 E9 }2 U5 Z$ r+ o
- public void sendMessage(String sender,String receiverId,String msg) {* e; ?. |' o; c( C
- Collection<ScriptSession> scriptSessionFlag = this.getScriptSessionFlag(receiverId);) j; L: p3 F4 {
- MessageInfo messageInfo = new MessageInfo(sender,receiverId,msg);/ t. ^$ k1 q- w5 `' {
- messageList.add(messageInfo);
" X9 r9 i: _) y2 z H4 Q - Util receiverUtil = new Util(scriptSessionFlag);# R4 J, T% Q9 I4 F3 D8 {
- receiverUtil.removeAllOptions("messageList");
& x7 e) p8 i- z9 _% E# T - receiverUtil.addOptions("messageList", messageList, "msg");
% _ o1 h" E4 A& D - }
7 s+ I$ K% D% ]- t) d3 o - }
, ` i4 I+ I" X) ?4 W
复制代码
8 ~; ]+ k* L, @- j2 t项目源码下载地址:点击下载2 _7 o( m* N8 \! a* g# T% j! a) b
9 X8 O' b3 M' |4 T; [ |
|