TA的每日心情 | 衰 2021-2-2 11:21 |
---|
签到天数: 36 天 [LV.5]常住居民I
|
在下根据网上的相关资料,摸索着写了一个DWR框架推模式的小聊天室,可实现私聊功能。 - y7 u/ W" i6 h, w3 V* L: Q' O
功能基本正常,但是偶尔会出现信息显示不同步的问题,现将源码奉上,希望各位高手能指点一二。ChatManager.java
% E5 a! g# s4 v3 {8 o8 O p: ]0 N2 V5 S( _
- package services;
% ]6 b! c1 o* U! v4 h! s! A) E6 u
, G# `$ U7 R6 \& q4 b* U: w8 k- import java.util.ArrayList;
& U' x6 F$ g* o+ @: _7 h6 D+ ] - import java.util.Collection;" b- Z4 h4 G0 h2 [
- import java.util.List;
6 M$ V! w1 w$ @* L - L! y' W# W8 J: O" e o
- import javax.servlet.http.HttpServletRequest;
; A0 T1 |$ q5 N$ E- U - import javax.servlet.http.HttpSession;4 h6 Y) n/ j; j- u5 S9 D1 `! y
* {: I5 Y4 s! y p- import org.directwebremoting.ScriptSession;0 B: _" L& \- Y0 N% w
- import org.directwebremoting.WebContext;$ w- a* S l' }( t- C1 M
- import org.directwebremoting.WebContextFactory;. `8 `9 ?4 v; Z) E& u! C4 |
- import org.directwebremoting.proxy.dwr.Util;
( Z1 F0 q: A7 V- @& @% H
- Y0 L0 F$ d5 b- import pojo.MessageInfo;
. i- e2 H% ?8 x2 J# m2 A - import pojo.UserInfo;* H. R/ o. d4 c% r7 C& W
, w- l% j) w0 L7 l0 P% ?. Q- public class ChatManager {8 _5 C- J! m ~7 `9 d% @' p* J
- public static List<UserInfo> onlineUsersList=new ArrayList<UserInfo>();' D4 m8 |: Q3 U
- public static List<UserInfo> sendUserList=new ArrayList<UserInfo>();
4 I2 C- F+ A6 |$ f; A& X - public static List<MessageInfo> messageList=new ArrayList<MessageInfo>();
% S# C, Y2 i5 ], \/ n - public ChatManager(){
% v& v& b9 v9 l - UserInfo userInfo = new UserInfo("0","所有人");
$ w2 o8 B$ u% P6 \! ]8 u - sendUserList.add(userInfo);
2 }2 Z) S4 u. s9 ~1 }; p - }
& d, m8 V7 [% {1 i0 a$ I* q - / _" ?6 O% b; g0 b
- public String updateOnlineUsers(String userName,boolean flag) {
5 B% D7 k$ A6 }$ s9 F h* |" C - UserInfo userInfo=null;- n# r( `7 X, C* x; ^) ?( z0 J& S
- WebContext context=WebContextFactory.get();+ x: {% c" h6 Z2 |# y, I) l
- HttpSession session = context.getSession();# G, g" a: w5 T# [* c& r
- if (flag) {2 y! I$ e: O/ {% x- A9 M5 p# E
- userInfo = new UserInfo(session.getId(),userName);* G$ `- v" f& B" l* I& z6 y) Y
- onlineUsersList.add(userInfo);
+ b) G6 ?: E2 {* f. l - sendUserList.add(userInfo);
4 d/ F9 J X: G( h - this.setScriptSessionFlag(userInfo);
$ }5 Y9 S5 I8 Q8 q! G l& k+ y - }
# F6 M6 k' ^. u% a, ?6 S7 I$ r - String currentPage = "/chat/index.jsp";1 z" w Y X$ H9 p, n9 O. r5 c
- Collection sessionsByPage = context.getScriptSessionsByPage(currentPage);% N" a1 N5 b. n, X( y0 i' A- m) p* z
- Util allPageUtil = new Util(sessionsByPage);
x; i y2 f5 k& M - allPageUtil.removeAllOptions("onlineList");0 j( M0 y/ o6 L Q) t6 h
- allPageUtil.addOptions("onlineList", onlineUsersList, "userName");
0 n3 E" |2 r8 u. b* t) C% V - allPageUtil.removeAllOptions("userList");, [$ l3 g9 X6 e$ J
- allPageUtil.addOptions("userList", sendUserList, "userId", "userName");5 W2 o7 { Y# H9 `
- if (!flag) {( Z9 E) a" y$ E b" ], @
- return null;( i- \. @ }# o3 ~
- }
! L! {9 O; i" ^9 m1 c5 [# S7 _ - return userInfo.getUserId();0 ^( A \8 y9 p) K% a3 A5 w' C
- }' I M. k. ~8 d' f4 `: [; p1 ?
-
+ G' r( Y7 [ ?; x, y6 D2 e B: w - public void setScriptSessionFlag(UserInfo userInfo) {
! ^$ r- ~( u8 X7 ~0 ?( m2 |( m - WebContextFactory.get().getScriptSession().setAttribute("userInfo", userInfo);
, P8 H) [, r7 E - }
: Y) T. Z/ ?/ b: l$ | X( Q -
4 X! e1 D: v4 N5 G - public Collection<ScriptSession> getScriptSessionFlag(String userId) {
. l$ z! T! j# W+ v( ^) w - Collection<ScriptSession> list=new ArrayList<ScriptSession>();- F+ k- Y! |. U3 D# Z
- Collection<ScriptSession> sessions=new ArrayList<ScriptSession>();" C' y) l f" Z- L. X6 f3 Z
- WebContext context = WebContextFactory.get();
6 U9 d# t7 `/ n+ v, f9 w - String currentPage = "/chat/index.jsp";
% y+ b& m* T2 q! D4 X4 c - Collection sessionsByPage = context.getScriptSessionsByPage(currentPage);* u3 \( \& s5 B) C7 a6 d
- sessions.addAll(sessionsByPage);
2 k, R. @, G. Q3 r - if ("0".equals(userId)) {, Y! \' d/ K+ S: Y
- list=sessions;+ j, r3 U2 p/ W- b( ~
- }else{/ g( a4 M: p) n6 p$ b& ]
- for (ScriptSession session : sessions) {
6 U# `: r/ o/ h# z5 Y* r* A - UserInfo userInfo=(UserInfo) session.getAttribute("userInfo");
2 z5 M+ T4 j2 H+ ` - if (userId.equals(userInfo.getUserId())) {# J6 z7 y/ J) G! `& o: m
- list.add(session);1 |0 ?) Z$ G% R; I. z. U
- break;
! i4 l) }9 Y- G0 E" s - }
5 P: v; `' D2 z! M' r9 D/ R7 a - }( e3 F0 q3 W: L9 A
- }
$ r! B3 i8 H! n/ Z1 A# ]3 T - return list;
, h) [5 y$ ?9 A# @0 V) F - }
/ A# b6 w3 Y( U* c4 e5 j; i% F. l -
3 ^4 i4 A( z5 t0 n3 ?$ }! Z3 T' T - public void sendMessage(String sender,String receiverId,String msg) {
8 ? Q* w. u: Z3 G; }7 O - Collection<ScriptSession> scriptSessionFlag = this.getScriptSessionFlag(receiverId);4 p; H4 W9 m. {6 c0 D! z0 H; M0 c6 G
- MessageInfo messageInfo = new MessageInfo(sender,receiverId,msg);/ ^! Y( R7 {0 q7 v/ p$ g1 d, ~
- messageList.add(messageInfo);/ [1 W8 Y/ V: G6 Q& s
- Util receiverUtil = new Util(scriptSessionFlag);
5 P. ]) ^- I+ ~ - receiverUtil.removeAllOptions("messageList");
* G& e& O' K4 y8 m7 A5 h - receiverUtil.addOptions("messageList", messageList, "msg");
4 Y* Q; d( I$ B% P2 \ - }
3 T9 k" u6 h! Q0 p# [5 v: ~& Z3 P - }- f2 C* f0 z7 {+ K) j2 a9 g
复制代码
/ }, j {+ ` @( e6 `% e项目源码下载地址:点击下载
8 I7 E+ }3 R( _* `
0 `1 L: X1 l- B- F |
|