TA的每日心情 | 衰 2021-2-2 11:21 |
---|
签到天数: 36 天 [LV.5]常住居民I
|
在下根据网上的相关资料,摸索着写了一个DWR框架推模式的小聊天室,可实现私聊功能。 1 C2 U. o0 c' }3 }) J4 q0 E, ]
功能基本正常,但是偶尔会出现信息显示不同步的问题,现将源码奉上,希望各位高手能指点一二。ChatManager.java
& z0 D$ Z3 ~+ h5 g% L/ y3 P, ^+ d- k5 k
- package services;
' ], L9 G. a$ Q* }4 n. V% L1 P
/ U9 r( B% I& y3 M9 ?- import java.util.ArrayList;6 y* @! R! k! n$ a0 j, H
- import java.util.Collection;
4 N" H/ Y! [* h, E. J - import java.util.List;
3 a- x2 ~+ C# e7 X; J - ' a$ K! V* e" J: |
- import javax.servlet.http.HttpServletRequest; c$ [; ~5 m$ h/ z3 S3 N' @
- import javax.servlet.http.HttpSession;/ k0 B9 G0 L4 w7 V
- 2 M; b6 A) l0 b1 Z, B9 Z
- import org.directwebremoting.ScriptSession;" h4 C" ` j: N# m
- import org.directwebremoting.WebContext;
8 A9 B- c* T, @ - import org.directwebremoting.WebContextFactory;
" H$ @$ {& I/ S# F i5 N5 R2 c - import org.directwebremoting.proxy.dwr.Util;
0 M( c" w3 j8 d' D, C. x
+ W7 _8 u. k; D# e0 d3 w- import pojo.MessageInfo;
0 l9 ?, N$ V$ f( D; i9 z; a* H - import pojo.UserInfo;
: ^0 z0 n9 n8 a, g* Q: T - + O4 R8 \# R4 q3 [" Z
- public class ChatManager {
1 D7 {: R5 b* k: a6 e4 a; r+ v \ - public static List<UserInfo> onlineUsersList=new ArrayList<UserInfo>();
% [% T7 z4 ~3 I" U1 R - public static List<UserInfo> sendUserList=new ArrayList<UserInfo>();
( B; z8 u; G$ O; ?! N) v - public static List<MessageInfo> messageList=new ArrayList<MessageInfo>();, |! x. R, }$ v! c
- public ChatManager(){: I0 e% A" U& }' V3 s! C. C
- UserInfo userInfo = new UserInfo("0","所有人");
* t: r7 c# ]1 B( w - sendUserList.add(userInfo);
" z! u+ _3 B. {, T - }
Y3 F3 j: j) Z, H5 o -
6 ^( g: J! I0 D% S l" d - public String updateOnlineUsers(String userName,boolean flag) {
& u4 [! v) I8 K+ w0 \; p: R - UserInfo userInfo=null;
6 U" j5 v7 Q3 M2 l% \" K - WebContext context=WebContextFactory.get();
; d& j( A+ X6 {. `' ] - HttpSession session = context.getSession();) E+ T q+ [6 m3 ]6 q% [
- if (flag) {
; g7 i' \6 B7 f - userInfo = new UserInfo(session.getId(),userName);
& F- s! s' e& @ - onlineUsersList.add(userInfo);/ U4 R7 p5 D+ c0 W* }1 b' t
- sendUserList.add(userInfo);. i; i7 ^0 Y5 d4 A P
- this.setScriptSessionFlag(userInfo);2 i4 \# v d& H/ |) W
- }
( S. H# U0 s8 I4 p) [ - String currentPage = "/chat/index.jsp";# I% r2 ^7 A2 {& R
- Collection sessionsByPage = context.getScriptSessionsByPage(currentPage);6 l( ?3 P7 E" p" d4 `9 ~
- Util allPageUtil = new Util(sessionsByPage);% f8 @/ }* A- p4 v% t
- allPageUtil.removeAllOptions("onlineList");
( E4 y" p7 t1 S7 C' ^! f - allPageUtil.addOptions("onlineList", onlineUsersList, "userName");
$ j: h! p* i" ?9 f- m7 \ - allPageUtil.removeAllOptions("userList");
1 j# {4 J, n/ O9 X/ q X% u: i! t - allPageUtil.addOptions("userList", sendUserList, "userId", "userName");
" q' E7 {8 _; r, j7 t; K - if (!flag) {
0 Z2 N# Z4 Y/ V7 ^7 v- f% i - return null;) e2 `. A* c( Q+ M
- } A% H! p$ Z! Z/ |
- return userInfo.getUserId();
- @: z( c# j2 ? - }
) x0 U( R; P% L1 ]4 [- n: K; I - 2 e L0 i5 x0 h+ Y0 J& c
- public void setScriptSessionFlag(UserInfo userInfo) {
\. f$ q1 N: o - WebContextFactory.get().getScriptSession().setAttribute("userInfo", userInfo);2 X1 R) z# \1 l7 J
- }8 K; Z1 O- I* \( N0 F& ^# p
- % ]8 `2 Z6 x8 B
- public Collection<ScriptSession> getScriptSessionFlag(String userId) {
5 h' J# r8 s! F7 b, l - Collection<ScriptSession> list=new ArrayList<ScriptSession>();
7 {' `9 P, r. B/ h: h - Collection<ScriptSession> sessions=new ArrayList<ScriptSession>();
7 |+ _+ b: F; { i - WebContext context = WebContextFactory.get();
- u, ]' p Q5 d+ \8 x& @- u - String currentPage = "/chat/index.jsp";
* f9 Q6 T2 H6 @4 E# z( p B( W - Collection sessionsByPage = context.getScriptSessionsByPage(currentPage);
9 D0 g" h/ B2 M G. W' u - sessions.addAll(sessionsByPage);1 Y' m w! ]6 H0 b
- if ("0".equals(userId)) {
7 N* e9 v! @+ I' d* G2 t& E. @ - list=sessions;1 u) ?% k5 N; F+ ~% k
- }else{
8 F# ^4 E, x0 R2 A - for (ScriptSession session : sessions) {
! ]4 |! \4 C4 W' \+ L - UserInfo userInfo=(UserInfo) session.getAttribute("userInfo");
2 {5 U9 z: y" g R- ~ u+ { - if (userId.equals(userInfo.getUserId())) {' ^; K$ c* C+ h; i5 _6 f' [
- list.add(session);( \ U+ G3 c* R0 H. Q4 Y
- break;
4 C* J8 c6 C; K/ ^1 W% {# m - }
' d# j2 }; S; u) ^# x" O9 R - }
( s. @7 ?4 K' t: G/ I* l; J) R' g - }. m( S* Q) v! g! L; F
- return list;- {1 e/ n& P, N! q& U- ^1 a3 t
- }
4 c: p: i5 g: {! _+ y9 i8 K8 k/ z, c' Y -
6 Z9 n( z+ k& f& _ - public void sendMessage(String sender,String receiverId,String msg) {; w$ j- X L$ O; h. ^
- Collection<ScriptSession> scriptSessionFlag = this.getScriptSessionFlag(receiverId); Q( W1 O& h: J' y
- MessageInfo messageInfo = new MessageInfo(sender,receiverId,msg);6 |. s0 c# f! Q2 R
- messageList.add(messageInfo);* H& J- M. R! H, \9 h( a
- Util receiverUtil = new Util(scriptSessionFlag);+ ]0 g8 X9 z- G! S3 S+ B
- receiverUtil.removeAllOptions("messageList");6 Z: c9 ?% o; d! w; g N9 {
- receiverUtil.addOptions("messageList", messageList, "msg");9 `. I& O& H- z+ r8 a M7 A
- }
4 ?$ a. d" D a* W* f' n - }2 X7 R* ]) \9 H
复制代码
- |* i6 A% n3 S t6 _! B项目源码下载地址:点击下载. s; [. |+ C. x) A( [8 z& a5 c
+ f- A& u0 D0 j- b6 f
|
|