TA的每日心情 | 衰 2021-2-2 11:21 |
---|
签到天数: 36 天 [LV.5]常住居民I
|
在下根据网上的相关资料,摸索着写了一个DWR框架推模式的小聊天室,可实现私聊功能。
: j: y" r; v1 l功能基本正常,但是偶尔会出现信息显示不同步的问题,现将源码奉上,希望各位高手能指点一二。ChatManager.java
; c5 }4 ]6 o! c6 J: G( Z& R' D4 m* [ P0 u6 B" ~
- package services;# U; ~+ }* f4 o/ @* O" _, |
- 9 I; u: u; M, y) q# p
- import java.util.ArrayList;
: e! m) M; Z% |7 e9 _# D$ A - import java.util.Collection;% o# B; t9 r( H
- import java.util.List;5 ?/ E2 t0 [& ?6 \4 J
# i, A. o3 @* n( ~- import javax.servlet.http.HttpServletRequest;
: |/ S( Q8 s$ I i# k - import javax.servlet.http.HttpSession;
) Z& T4 q8 X/ S4 s
G& Y# f$ h! R: P6 W4 }- import org.directwebremoting.ScriptSession;0 z! B4 r! M0 w+ m! T+ J
- import org.directwebremoting.WebContext;
f, k+ {" n l' r5 y, e% \# b - import org.directwebremoting.WebContextFactory;- e- n" `$ l) @5 t& E/ b
- import org.directwebremoting.proxy.dwr.Util;
2 M* S) {. p6 p' k2 z1 k
3 K( D7 U9 h2 u$ K2 |5 m- import pojo.MessageInfo;7 f3 b$ ~' q0 u& K. Y) m5 N9 o& F! i
- import pojo.UserInfo;/ y- M; J: Y0 { N' a3 Z o
5 s4 K9 x9 a& {5 a6 g- public class ChatManager {
: `/ H' p, `; ]8 R - public static List<UserInfo> onlineUsersList=new ArrayList<UserInfo>();7 k4 M$ K( q& Y7 B' L
- public static List<UserInfo> sendUserList=new ArrayList<UserInfo>();8 C/ z* ^9 h* m/ Z. O/ k
- public static List<MessageInfo> messageList=new ArrayList<MessageInfo>();& G8 N9 V& {6 ?2 D6 D
- public ChatManager(){
5 a0 b) g3 }0 \* Q8 G - UserInfo userInfo = new UserInfo("0","所有人");2 |: r9 I" r( Y% b
- sendUserList.add(userInfo);
# V$ I* J2 d- m& z0 y6 Z6 T, w9 [ - }
: ]# Z7 E% t8 Z: c$ t! D - # s" ]. J7 `) |
- public String updateOnlineUsers(String userName,boolean flag) {- Z$ Q- ~1 ~' w6 ]5 U
- UserInfo userInfo=null;
" a ~ U" y6 F5 |, F, Z' Z - WebContext context=WebContextFactory.get();6 l A4 L* J4 P
- HttpSession session = context.getSession();9 u' h9 m% @$ g
- if (flag) {, p1 x' k8 R! G3 V [5 |
- userInfo = new UserInfo(session.getId(),userName);& ]2 i5 u" q' }. o5 t
- onlineUsersList.add(userInfo);7 w, J! J+ z0 I1 t4 U% o5 N# m
- sendUserList.add(userInfo);) [* _+ ^, t! ~
- this.setScriptSessionFlag(userInfo);
1 R7 m$ f0 A+ F) Y5 C4 o - }
9 @( V4 U$ G* f" Y7 F+ }( `, s - String currentPage = "/chat/index.jsp";- ^0 ?: P8 f: o, P g
- Collection sessionsByPage = context.getScriptSessionsByPage(currentPage);
2 v2 _) I5 N1 F - Util allPageUtil = new Util(sessionsByPage);
; b( c! ~1 g7 x2 ~7 v - allPageUtil.removeAllOptions("onlineList");
" K0 q+ u' A2 ^% [: q - allPageUtil.addOptions("onlineList", onlineUsersList, "userName");) \; \; J2 T5 q) H! H5 A' n
- allPageUtil.removeAllOptions("userList");5 @1 o8 x+ W5 R% H+ v
- allPageUtil.addOptions("userList", sendUserList, "userId", "userName");+ s; H0 K7 C% n/ ]
- if (!flag) {
$ k' T. s3 M( x$ u- ^8 t - return null;
) e1 s' h, Q; }+ ~ - }
3 ~! s8 c$ |, b) O. | - return userInfo.getUserId();
+ O5 G3 p$ | _ e - }
% ?% ~5 a/ s. O4 N; c - , q. b1 ~# b Z9 p2 S7 F3 g7 g
- public void setScriptSessionFlag(UserInfo userInfo) {
0 u7 ^' V- Y, F- J- d: R1 P+ W - WebContextFactory.get().getScriptSession().setAttribute("userInfo", userInfo);" m& k0 o8 N: @! \/ W/ Z) d
- }
2 Z0 X/ x. P+ d! A& d9 V2 b - ) l2 o; W8 H8 j& k o0 ~# c
- public Collection<ScriptSession> getScriptSessionFlag(String userId) {' f/ w! u5 M5 W5 O8 I
- Collection<ScriptSession> list=new ArrayList<ScriptSession>();
' N+ t! }" e$ j, ~( S! x) k, o/ Q' E - Collection<ScriptSession> sessions=new ArrayList<ScriptSession>();
$ V8 L. r/ G# }% k9 s) W0 i - WebContext context = WebContextFactory.get();' v, ?8 @/ C* M6 W J" q. P2 [
- String currentPage = "/chat/index.jsp";
4 r+ F5 g0 L. i - Collection sessionsByPage = context.getScriptSessionsByPage(currentPage);
8 ^' z. F* B$ R+ ? - sessions.addAll(sessionsByPage);
& e4 o* ]/ k6 u/ _& J7 w - if ("0".equals(userId)) {
: i' [1 C% f6 Z - list=sessions;
; R% L" a3 a) H! w - }else{! R* I& N5 J0 B5 O: ~, Z, u
- for (ScriptSession session : sessions) {/ C* N3 g- i& H ^5 A" _
- UserInfo userInfo=(UserInfo) session.getAttribute("userInfo");) W- Y! \' H) S4 x6 Z! D
- if (userId.equals(userInfo.getUserId())) {
: X- o& Z7 [' H% t2 f: h% |/ R - list.add(session);
, |5 x/ R; ]% ~" B: ^% q; M( V - break;2 }6 C1 K, [# Z T ?" ~
- }' [0 m) Q$ V) Q& d0 [
- }. \3 z- P- w8 ^
- }
% c/ W% \: T# g - return list;
( v% n9 m: T; u, x- b - }: P+ L8 z# q- `5 T$ k
-
/ Z% y5 I. A V6 } - public void sendMessage(String sender,String receiverId,String msg) {1 |3 G! ^7 s: w1 G8 f" ~7 f
- Collection<ScriptSession> scriptSessionFlag = this.getScriptSessionFlag(receiverId);
& W9 }* m. q( h. J* L( Z' p8 T - MessageInfo messageInfo = new MessageInfo(sender,receiverId,msg);: x, y+ y) ]. ^& V: [, P& N
- messageList.add(messageInfo);) C* Y$ a, A# G9 u
- Util receiverUtil = new Util(scriptSessionFlag);
$ H2 y' b7 X: I) a, H - receiverUtil.removeAllOptions("messageList");
# j+ V5 Z- k5 @! ]* Z- C4 j* k - receiverUtil.addOptions("messageList", messageList, "msg");2 [0 Q: l) `" t/ \. @- M
- }8 x0 h& ?. f$ v1 k6 Y' f
- }& X! U! A* e9 ?! b
复制代码 S9 S5 R+ i; q7 z
项目源码下载地址:点击下载
& b3 u6 K9 O$ P) L, X; p! _( a9 Q5 A1 d6 T- W7 x% a2 w. Z e
|
|