TA的每日心情 | 衰 2021-2-2 11:21 |
---|
签到天数: 36 天 [LV.5]常住居民I
|
在下根据网上的相关资料,摸索着写了一个DWR框架推模式的小聊天室,可实现私聊功能。
" {) v9 D) \. R& ^4 @5 V& H功能基本正常,但是偶尔会出现信息显示不同步的问题,现将源码奉上,希望各位高手能指点一二。ChatManager.java0 Q B' m2 V) R- H( w$ @! d
$ D5 b2 _0 _' s P
- package services;
! V6 y5 c, H- w" ~! ?$ d - - ?0 S4 o; N0 T, [# D
- import java.util.ArrayList;
9 Z" M! U# E$ S% |4 M- o - import java.util.Collection;9 T$ u) X% t) ?7 s# E
- import java.util.List;
0 f5 v: r$ b7 P$ m' _4 Z+ r- [ - + J$ M2 B9 P5 }
- import javax.servlet.http.HttpServletRequest;
) A. I' R% ^" ]+ {( m9 Q/ S& V - import javax.servlet.http.HttpSession;5 E" l' K w- q; n0 ?, c
- & h& o0 g( |0 j; t: `. {
- import org.directwebremoting.ScriptSession;2 h2 J) h/ ~6 ]7 g. {
- import org.directwebremoting.WebContext;
% `; U$ D4 K; W8 U - import org.directwebremoting.WebContextFactory;
+ d J- Q4 o# F6 R2 V8 X8 r' L3 ] - import org.directwebremoting.proxy.dwr.Util;$ U' s, N8 M9 y
- 4 |2 O" e5 ~( W$ @ j
- import pojo.MessageInfo;
; z T: F' Y7 s3 _; R - import pojo.UserInfo;
$ ?! }3 S% E; ?3 b$ k! X
2 p5 C, K7 D! g0 F/ J1 T- public class ChatManager {, Y9 C% L7 U. V6 F2 _( B
- public static List<UserInfo> onlineUsersList=new ArrayList<UserInfo>();6 ~2 ~/ t2 G- G' l7 J
- public static List<UserInfo> sendUserList=new ArrayList<UserInfo>();% \ k# h9 u* S
- public static List<MessageInfo> messageList=new ArrayList<MessageInfo>();
' [5 ?" _; \+ i- s - public ChatManager(){
- S/ H+ c( k; f7 O - UserInfo userInfo = new UserInfo("0","所有人");% l) m, w% F: G* v6 q7 P4 u
- sendUserList.add(userInfo);- G" S& U9 v* n \# d a1 G
- }
$ {# C6 a$ @3 Y6 T, V/ l/ @" J4 J8 @$ p -
C. A& Y' ?8 Y- j - public String updateOnlineUsers(String userName,boolean flag) {' W. N4 i$ e( j6 m1 Q9 S
- UserInfo userInfo=null;
) q" o# w2 |9 b* O0 A - WebContext context=WebContextFactory.get();
# M8 ]: W6 e- t& F' R/ F - HttpSession session = context.getSession();
2 ]: O2 A+ Y7 F. Z7 j4 i: h) k+ X+ A - if (flag) {; s- o6 T+ o) s& o' J8 G) t8 I
- userInfo = new UserInfo(session.getId(),userName);
b5 f9 q( v. P& I7 H$ r - onlineUsersList.add(userInfo);
3 X6 P: b2 H7 w5 D/ {/ t - sendUserList.add(userInfo);$ u, H& M, T; ~2 |: L. k
- this.setScriptSessionFlag(userInfo);3 I9 L2 A7 K, i# o- ~
- }
5 S3 ]; E; {) ] - String currentPage = "/chat/index.jsp";% X" |( _" F! d6 G
- Collection sessionsByPage = context.getScriptSessionsByPage(currentPage);
3 B7 W6 }0 K9 T) u! G2 O, d - Util allPageUtil = new Util(sessionsByPage);5 [3 o! k% _. ~; V
- allPageUtil.removeAllOptions("onlineList");
0 j2 G% G3 c5 o' { - allPageUtil.addOptions("onlineList", onlineUsersList, "userName");4 f0 o- r; \) q
- allPageUtil.removeAllOptions("userList");
% s' t* ^7 I: w; [" [ - allPageUtil.addOptions("userList", sendUserList, "userId", "userName");
' J6 F( _. g9 c! l( d" P - if (!flag) {
) T, E# O* E* @. X2 j2 ]' f. g - return null;; K: z2 p7 D% j% e% K
- }
6 H1 X* ?) J" {/ W4 J/ I4 o - return userInfo.getUserId();0 P) v/ ?/ { }! ]* d1 {
- }6 A: O& ~# E3 Z
-
4 L. w, z) A/ Q1 \0 k - public void setScriptSessionFlag(UserInfo userInfo) {9 [- n. R7 N, [: Z" a' U
- WebContextFactory.get().getScriptSession().setAttribute("userInfo", userInfo);* M; B1 u0 _5 `5 j# g! w+ N% U7 ^
- }
" P, [! q; n2 i& J ]6 s7 i - + S/ A8 [2 j! `( P0 H3 z; U. j
- public Collection<ScriptSession> getScriptSessionFlag(String userId) {
; H+ j4 z5 g4 H% S1 {5 X - Collection<ScriptSession> list=new ArrayList<ScriptSession>();
' {* }6 K$ D. l1 a! V% V8 M - Collection<ScriptSession> sessions=new ArrayList<ScriptSession>();
" L- ], S4 B/ f! G% D( w - WebContext context = WebContextFactory.get();
! |. z5 r u4 t% `1 K: L7 U I. K - String currentPage = "/chat/index.jsp";
+ y3 W# |3 Y I9 Y' d - Collection sessionsByPage = context.getScriptSessionsByPage(currentPage);
! {& O2 `6 t7 ^7 e - sessions.addAll(sessionsByPage);+ H4 S) J/ V6 Q; H) I5 |
- if ("0".equals(userId)) {
$ B' k$ ?" D* E( s* N3 @ - list=sessions;/ Q; R X F% n% B9 C
- }else{7 P. v ^ Q* k! R2 |- l- p
- for (ScriptSession session : sessions) {
% g2 \5 @* u" F @: f% \! V7 c4 | - UserInfo userInfo=(UserInfo) session.getAttribute("userInfo");
* P3 d5 p$ y" {; M' t9 `0 A - if (userId.equals(userInfo.getUserId())) {
* @8 Q4 W! |& q# u' g - list.add(session);
3 U4 `; q# v; j9 @* Q - break;
0 H, W* d0 F: e* ~/ j. Y' U/ D: i - }, v# A4 \9 ?; e) ~
- }6 ?0 }! y* r9 R1 X, p
- }9 A4 M% f0 [8 _0 z+ n [
- return list;
: B0 p6 |& K& h; c - }
4 A, Z* b/ \6 e, l% b& ?- g) C -
8 F* T" l) g# ^ Q' y7 H% D- H9 } - public void sendMessage(String sender,String receiverId,String msg) {
% L# _. a% P+ @ - Collection<ScriptSession> scriptSessionFlag = this.getScriptSessionFlag(receiverId);
- o: Z& r# n$ C/ ]' o - MessageInfo messageInfo = new MessageInfo(sender,receiverId,msg);; u5 P7 A8 ~# m4 l' u: K
- messageList.add(messageInfo);
: L( F" m" V& z - Util receiverUtil = new Util(scriptSessionFlag);, b9 d1 h* c3 f! X; A7 I: [
- receiverUtil.removeAllOptions("messageList");& s6 [9 _: f! m: Z9 x* x
- receiverUtil.addOptions("messageList", messageList, "msg");) @* |1 s" f* h" ~
- }
( X; ^: {& l0 L# ` - }( _& O, `+ h1 o6 T% c' M; V
复制代码 * U6 F9 J5 _5 K: l2 Y
项目源码下载地址:点击下载+ }. B& |/ Q, G. m3 X5 f2 V& ]* c
4 v' W' m8 g* D% l7 q( k3 V, Z, t |
|