TA的每日心情 | 衰 2021-2-2 11:21 |
---|
签到天数: 36 天 [LV.5]常住居民I
|
在下根据网上的相关资料,摸索着写了一个DWR框架推模式的小聊天室,可实现私聊功能。 4 r" e1 P3 w8 v$ z2 V# S
功能基本正常,但是偶尔会出现信息显示不同步的问题,现将源码奉上,希望各位高手能指点一二。ChatManager.java. e& ~0 b: {! r3 z0 _% o: R
3 w8 l& A* w4 A) B3 Z
- package services;
' G1 S4 E6 K4 j# p; n/ t6 f) e2 m
- S2 z% P4 r0 k0 O/ k" Z- import java.util.ArrayList;
' F% K- X( ~5 o' }% o8 c: Y: T2 y - import java.util.Collection;, d3 L+ n: p4 P5 w& X
- import java.util.List;
. r% o+ x& \9 X4 O5 N: A% l
, v2 t* g- y8 g" r" j4 E+ l0 N- import javax.servlet.http.HttpServletRequest;9 I' K7 t6 W) H" @. L
- import javax.servlet.http.HttpSession;
3 o2 X+ d6 S9 j/ @ - ' I5 X' A! w! ~5 `; |1 A: O% v, ^
- import org.directwebremoting.ScriptSession;( n4 f' Y9 G% l' t1 g( ^- X
- import org.directwebremoting.WebContext;
$ S* g# P$ N' @+ a - import org.directwebremoting.WebContextFactory;% s0 M d; ]1 Y' O# ^
- import org.directwebremoting.proxy.dwr.Util;; u; d) i2 d& X2 L1 s! x# }& o
x$ }: r7 _# }; Y& [- import pojo.MessageInfo;
3 G' f1 ~* ^ g+ v; N - import pojo.UserInfo;" @9 L5 M* A4 T$ ^2 M' ]% p" T
% {2 H6 F1 I. I: w) ?9 I) [- public class ChatManager {
0 e: L0 [8 c3 `$ H& P9 m' i - public static List<UserInfo> onlineUsersList=new ArrayList<UserInfo>();- J8 D$ |5 P, h5 ]" w
- public static List<UserInfo> sendUserList=new ArrayList<UserInfo>();0 w2 a6 o0 h# \- x- z9 T: C, H; g
- public static List<MessageInfo> messageList=new ArrayList<MessageInfo>();1 h& X1 h& k) x$ s8 |8 f
- public ChatManager(){
2 o& g& c/ V* f* G! y - UserInfo userInfo = new UserInfo("0","所有人");6 S# ?* D1 [" z5 J
- sendUserList.add(userInfo);
' C- V! w: N/ \& S2 k7 _4 n+ m& O3 ]/ b - }
0 }+ [5 _7 ]; `/ O0 V8 L -
8 } P9 ?5 x1 Y' u) H/ o - public String updateOnlineUsers(String userName,boolean flag) {
% c) F" u- V) z+ v - UserInfo userInfo=null;
- P& I$ p2 H! e7 u - WebContext context=WebContextFactory.get(); r) i( E+ V0 s+ \. G+ O
- HttpSession session = context.getSession();' S3 z9 J: u; K( t
- if (flag) {
7 d7 D6 l8 h8 Q7 I% H9 k1 E; X9 V - userInfo = new UserInfo(session.getId(),userName);
6 q- o9 y% G: n - onlineUsersList.add(userInfo);0 A" Z' u! ~" n) k7 r
- sendUserList.add(userInfo);
6 j: ]( A0 D+ z5 ?8 c7 v9 @ - this.setScriptSessionFlag(userInfo);
0 v w1 I1 x( E) L1 V3 f - }4 d6 C- Z3 q) o9 S2 k' i# D
- String currentPage = "/chat/index.jsp";8 I" Y6 A: K" r% S* D& L
- Collection sessionsByPage = context.getScriptSessionsByPage(currentPage);2 V3 n% u8 f* `' J
- Util allPageUtil = new Util(sessionsByPage);
3 h& d7 R' {/ T( Y- N1 a1 K: _ - allPageUtil.removeAllOptions("onlineList");" Q. M/ Z4 b1 Y6 P! @( W
- allPageUtil.addOptions("onlineList", onlineUsersList, "userName");' |0 F) M( c' _. \: h
- allPageUtil.removeAllOptions("userList");& [: d/ O- W9 V" H
- allPageUtil.addOptions("userList", sendUserList, "userId", "userName");2 ^& n* t) J4 C, e! A
- if (!flag) {' g3 E$ S4 y/ i
- return null;+ ?2 c, S, U0 y4 y2 w6 {
- }9 K; ` ?4 f6 S' c4 ]; o
- return userInfo.getUserId();9 ]+ H9 Y, M& p- d! a M$ d
- }0 N/ \. d' f% E! a. U6 Q2 {' C
- 7 h( M6 W9 L1 {2 h6 I- q9 Z+ O
- public void setScriptSessionFlag(UserInfo userInfo) {& j# a: [! j* T/ W' ^8 }2 Y
- WebContextFactory.get().getScriptSession().setAttribute("userInfo", userInfo);7 M- U/ f) \' A, ]6 N
- }
/ Y, T$ @/ F1 j3 }# r% [ - $ p- P( m9 d% F1 ]4 f
- public Collection<ScriptSession> getScriptSessionFlag(String userId) {5 t" ?! \; |7 q# d8 C$ G
- Collection<ScriptSession> list=new ArrayList<ScriptSession>();6 |3 a, [ d8 r: a! \
- Collection<ScriptSession> sessions=new ArrayList<ScriptSession>();
. E7 U0 F2 x: z* n- u; N3 A2 M/ s - WebContext context = WebContextFactory.get();- Q1 k- o% F) B
- String currentPage = "/chat/index.jsp";5 _) G" `" p { T8 [% a2 |% ~
- Collection sessionsByPage = context.getScriptSessionsByPage(currentPage);
; T& T* X5 z% |1 u' `/ I. {" z - sessions.addAll(sessionsByPage);: b! Y/ e8 A. c' ]
- if ("0".equals(userId)) {
3 w% y0 F$ b7 g/ B# e - list=sessions;/ D% h0 X( L: Q* ?
- }else{9 i" T& S- l8 J+ ?
- for (ScriptSession session : sessions) {9 F4 W K- N6 n' ` T H
- UserInfo userInfo=(UserInfo) session.getAttribute("userInfo");
0 {+ z/ W9 m# S" V0 @ - if (userId.equals(userInfo.getUserId())) {5 {1 A/ s, O8 S9 r6 w2 P( a
- list.add(session);' r3 f0 K& @* k& U2 p+ ~" q) }/ \
- break;7 \/ H* K& U4 M
- }
. {/ b# R% j9 d) x3 q - }# k) ?. L$ J- |' p% E
- }
" {5 q- F" \- n8 x7 @" m2 [$ M0 j - return list;5 P" }7 O' y* c+ V: q
- }
, q$ I5 g( v* b -
- i6 {+ P b0 a+ r) N6 @# _ - public void sendMessage(String sender,String receiverId,String msg) {- g( b4 `& f- [5 i z8 j' U( }
- Collection<ScriptSession> scriptSessionFlag = this.getScriptSessionFlag(receiverId);7 e: q& l. q j2 Z; ?4 H7 g
- MessageInfo messageInfo = new MessageInfo(sender,receiverId,msg);6 w9 `# M+ O, A
- messageList.add(messageInfo);, x6 j' s8 m$ i
- Util receiverUtil = new Util(scriptSessionFlag);
6 y( L# h8 i9 W! R - receiverUtil.removeAllOptions("messageList");( Z5 N# _4 F- B
- receiverUtil.addOptions("messageList", messageList, "msg");
9 a ^2 f4 G$ _) S* O+ d/ F - }
! b5 k3 T$ @( ]% z - }
( S# X( s5 \8 i" N3 a9 l# T6 g
复制代码 8 k; d$ F3 B+ T3 V) O! z
项目源码下载地址:点击下载
0 L4 H! _2 d5 f# o
9 K7 D% @* }& z! E% k- s |
|