TA的每日心情 | 衰 2021-2-2 11:21 |
---|
签到天数: 36 天 [LV.5]常住居民I
|
在下根据网上的相关资料,摸索着写了一个DWR框架推模式的小聊天室,可实现私聊功能。 ( T$ c5 p& `! @9 x2 Y! T- G
功能基本正常,但是偶尔会出现信息显示不同步的问题,现将源码奉上,希望各位高手能指点一二。ChatManager.java* d' @1 @4 x, I
9 g! p# X, x8 V6 _, A- D: ~- package services;" n5 f# R% m) B/ F
- 8 `3 F: N" _/ `1 u3 V
- import java.util.ArrayList;
# ]+ e. h1 f! e% j - import java.util.Collection;1 M) f, G7 \0 K/ A) ^
- import java.util.List;4 O6 Z. `4 T ~8 C! e& M
- 8 b% d3 Y" x( I B
- import javax.servlet.http.HttpServletRequest;
" }9 `" V1 s' L; e6 Z - import javax.servlet.http.HttpSession;2 k, R5 N6 L! B
( @+ D: h+ _" T- import org.directwebremoting.ScriptSession;
. [% H' q) [! ]' v - import org.directwebremoting.WebContext;- J- D5 a8 f1 s% K& V( @
- import org.directwebremoting.WebContextFactory;, N$ ^( |& H: ~( _* J- Y7 U
- import org.directwebremoting.proxy.dwr.Util;/ d. \: F7 E$ a z0 z- p
- # k: X# q. W$ X1 A9 u- t# I
- import pojo.MessageInfo;$ m+ D) m. q+ I6 A& C2 ~, W9 R
- import pojo.UserInfo;% v, }0 d& i i' H
+ @1 x% E/ {' a; ^: J) F- public class ChatManager {& M; M# q' F8 @1 o% w+ T
- public static List<UserInfo> onlineUsersList=new ArrayList<UserInfo>();
1 @! G$ }) K. M2 n7 P# Q - public static List<UserInfo> sendUserList=new ArrayList<UserInfo>();
4 G( Z. ]- S# b& ]" R - public static List<MessageInfo> messageList=new ArrayList<MessageInfo>();0 e1 O/ `2 G+ ?) o% `. u
- public ChatManager(){3 J* [$ i5 a; d) w x* e# k
- UserInfo userInfo = new UserInfo("0","所有人");
. P/ I& L- k% `0 U ^0 g' f - sendUserList.add(userInfo);7 _% H9 M4 |$ _3 _5 i; r# L6 x7 D0 J0 L
- }
: m8 `- q, a0 q8 s; i, I- I -
! G& k4 C% M, K# T - public String updateOnlineUsers(String userName,boolean flag) {
~3 Q0 E# G& w9 n$ Z - UserInfo userInfo=null;1 y" |& J7 U* O9 z
- WebContext context=WebContextFactory.get();
. y1 k$ O% ^: ^9 v- E - HttpSession session = context.getSession();. s+ ^+ [/ g' s* w8 |/ h `
- if (flag) {
; P7 P0 z% v" d+ l - userInfo = new UserInfo(session.getId(),userName);- ^: m% K4 |* i) T( d: p. V7 @! A
- onlineUsersList.add(userInfo);) L* U) P7 a C; E
- sendUserList.add(userInfo);
3 r8 |+ J% N" X' n, Q, ~/ K) @ - this.setScriptSessionFlag(userInfo);
. t/ i+ v6 m3 b7 g6 T3 q C - }
3 e6 H8 y6 z" A; j) C$ j - String currentPage = "/chat/index.jsp";/ P5 ~- M j# O+ p, b
- Collection sessionsByPage = context.getScriptSessionsByPage(currentPage);" y' h3 t5 p7 c* Z# ~' D( B1 P
- Util allPageUtil = new Util(sessionsByPage);% C/ k1 I+ f! a9 H8 Q: {0 Z+ ]
- allPageUtil.removeAllOptions("onlineList");
" r- }; m2 S9 Z0 @( E6 P9 i! c& | - allPageUtil.addOptions("onlineList", onlineUsersList, "userName");+ E7 | Z' N8 J9 M4 C
- allPageUtil.removeAllOptions("userList");* I4 t) e# |' M( T/ ] K, L
- allPageUtil.addOptions("userList", sendUserList, "userId", "userName");
; i* H6 P q* s5 Z5 A/ `2 ^ - if (!flag) {" \3 X0 w7 A8 e5 t8 H1 G
- return null;1 W2 M) y# c, `% e! i- A: z; E+ s
- }- V6 p7 V* S% W2 N" b
- return userInfo.getUserId();* p" E9 p& U# k2 y/ b _
- }" d" |. Y6 a. v5 p' |
-
3 f* G% ^& [7 z$ _ - public void setScriptSessionFlag(UserInfo userInfo) {6 Z8 x5 G: H2 N- V. @
- WebContextFactory.get().getScriptSession().setAttribute("userInfo", userInfo);' w8 X( ]- E2 g5 ], q$ D
- }3 B9 y z/ [" `8 S Y: \. F* S- v
- * X" m6 l: r/ C8 j, w3 g
- public Collection<ScriptSession> getScriptSessionFlag(String userId) {* j9 |1 h4 p# s( _) s2 n6 n
- Collection<ScriptSession> list=new ArrayList<ScriptSession>();0 ^; e2 p D8 h- g
- Collection<ScriptSession> sessions=new ArrayList<ScriptSession>();
5 C) U7 w, q- b/ s1 K1 \0 V R R - WebContext context = WebContextFactory.get();
2 l, w, l( Q$ ~9 Z0 S9 \- W" I/ b' M - String currentPage = "/chat/index.jsp";1 R) R- p0 Y2 q
- Collection sessionsByPage = context.getScriptSessionsByPage(currentPage);3 M& B: C( W( ]! G2 y- y
- sessions.addAll(sessionsByPage);$ N1 q. j! I5 N3 u4 F% C5 ?7 h7 ~( I
- if ("0".equals(userId)) {
1 V+ c; g! z" l& v - list=sessions;
$ E5 n; I$ f; @* e$ g/ _# K: d - }else{( }$ w- T0 b) r0 ~
- for (ScriptSession session : sessions) {9 B( F& J- y9 m9 I6 B' k6 u
- UserInfo userInfo=(UserInfo) session.getAttribute("userInfo");
. i; j4 b, t3 G6 y/ M - if (userId.equals(userInfo.getUserId())) {; {8 H' @( ]% ]2 U$ g
- list.add(session);
; o& m0 K. [7 a, A& v - break;2 `$ p9 i; f* g
- }
4 b+ z! V% v1 @- O( p1 l - }7 _9 v' Q0 W D; r0 X. _
- }
$ X. r9 M% {" P - return list;; X6 y2 b; j9 [* u; Z: x/ U/ G8 m
- }3 i* N( u; A" [- u
- 6 f# Q# M- F0 v9 d4 ~
- public void sendMessage(String sender,String receiverId,String msg) {
6 s* B- y4 _; F - Collection<ScriptSession> scriptSessionFlag = this.getScriptSessionFlag(receiverId);& P$ Q$ Y9 _8 ]! U/ x
- MessageInfo messageInfo = new MessageInfo(sender,receiverId,msg);. {: ^: l; ]) | y3 P
- messageList.add(messageInfo);
( R- X' L# Q& O6 Q2 |& \5 q* W - Util receiverUtil = new Util(scriptSessionFlag);$ j/ J2 G4 p$ Y9 Q
- receiverUtil.removeAllOptions("messageList");$ {8 \7 `# B3 \8 s E/ d2 @4 i5 r
- receiverUtil.addOptions("messageList", messageList, "msg");
% {- v% n# j0 H0 r - }
( ~* W6 o* O0 X \# D6 d5 P. V - }1 K9 y% _# y# o) X/ H0 L% X. t9 O
复制代码 & A. z: j$ o: c: g# i
项目源码下载地址:点击下载& G# }; F; l; L- G+ G
+ s2 |7 t$ E, ]! i
|
|