moto web application
修订版 | e31aeeecdfda5a9d6275429f42fad879fc157fd3 (tree) |
---|---|
时间 | 2014-01-30 11:40:10 |
作者 | astoria-d <astoria-d@mail...> |
Commiter | astoria-d |
push socket unified
@@ -215,14 +215,14 @@ public class ChatBean implements Serializable { | ||
215 | 215 | |
216 | 216 | conn.close(); |
217 | 217 | |
218 | - PushContext pushContext = PushContextFactory.getDefault().getPushContext(); | |
219 | - | |
220 | - ChatBean.ChatMessage cm = new ChatBean.ChatMessage(); | |
218 | + PushMsg pm = PushMsg.newChatMessage(); | |
219 | + PushMsg.ChatMessage cm = pm.getChatMessage(); | |
221 | 220 | cm.setMsg(msg); |
222 | 221 | cm.setDate(dt); |
223 | 222 | cm.setUname(userBean.getUid()); |
224 | 223 | |
225 | - pushContext.push("/" + chatRoom, cm); | |
224 | + PushContext pushContext = PushContextFactory.getDefault().getPushContext(); | |
225 | + pushContext.push("/" + chatRoom, pm); | |
226 | 226 | } |
227 | 227 | catch (SQLException se) { |
228 | 228 | log.severe("sql err!!!"); |
@@ -0,0 +1,56 @@ | ||
1 | +package motoSample; | |
2 | + | |
3 | + | |
4 | +public class PushMsg { | |
5 | + | |
6 | + public static class LoginUser { | |
7 | + String uid; | |
8 | + boolean login; | |
9 | + | |
10 | + public void setUid(String uid) { this.uid = uid; } | |
11 | + public void setLogin(boolean login) { this.login = login; } | |
12 | + public String getUid() { return uid; } | |
13 | + public boolean getLogin() { return login; } | |
14 | + } | |
15 | + | |
16 | + public static class ChatMessage { | |
17 | + private String uname; | |
18 | + private String msg; | |
19 | + private String date; | |
20 | + | |
21 | + public String getUname() { return uname; } | |
22 | + public String getMsg() { return msg; } | |
23 | + public String getDate() { return date; } | |
24 | + public void setUname(String uname) { this.uname = uname; } | |
25 | + public void setMsg(String msg) { this.msg = msg; } | |
26 | + public void setDate(String date) { this.date = date; } | |
27 | + } | |
28 | + | |
29 | + private String pushType; | |
30 | + private LoginUser lu; | |
31 | + private ChatMessage cm; | |
32 | + | |
33 | + public String getPushType() { return pushType; } | |
34 | + public void setPushType(String pushType) { this.pushType = pushType; } | |
35 | + public LoginUser getLoginUser () { return lu; } | |
36 | + public void setLoginUser (LoginUser lu) { this.lu = lu; } | |
37 | + public ChatMessage getChatMessage () { return cm; } | |
38 | + public void setChatMessage (ChatMessage cm) { this.cm = cm; } | |
39 | + | |
40 | + public final static String PUSH_TYPE_LOGIN = "PUSH_TYPE_LOGIN"; | |
41 | + public final static String PUSH_TYPE_CHAT_MESSAGE = "PUSH_TYPE_CHAT_MESSAGE"; | |
42 | + | |
43 | + public static PushMsg newLoginPushMsg() { | |
44 | + PushMsg pm = new PushMsg(); | |
45 | + pm.setPushType(PUSH_TYPE_LOGIN); | |
46 | + pm.setLoginUser(new LoginUser()); | |
47 | + return pm; | |
48 | + } | |
49 | + | |
50 | + public static PushMsg newChatMessage() { | |
51 | + PushMsg pm = new PushMsg(); | |
52 | + pm.setPushType(PUSH_TYPE_CHAT_MESSAGE); | |
53 | + pm.setChatMessage(new ChatMessage()); | |
54 | + return pm; | |
55 | + } | |
56 | +} |
@@ -90,14 +90,6 @@ public class UserBean implements Serializable { | ||
90 | 90 | } |
91 | 91 | } |
92 | 92 | |
93 | - public static class LoginUser { | |
94 | - String uid; | |
95 | - boolean login; | |
96 | - public void setUid(String uid) { this.uid = uid; } | |
97 | - public void setLogin(boolean login) { this.login = login; } | |
98 | - public String getUid() { return uid; } | |
99 | - public boolean getLogin() { return login; } | |
100 | - } | |
101 | 93 | public String doLogin() { |
102 | 94 | |
103 | 95 | try { |
@@ -130,12 +122,14 @@ public class UserBean implements Serializable { | ||
130 | 122 | //notify log in message |
131 | 123 | for (SelectItem si : flights) { |
132 | 124 | String chatRoom = si.getValue().toString(); |
133 | - LoginUser lu = new LoginUser(); | |
125 | + | |
126 | + PushMsg pm = PushMsg.newLoginPushMsg(); | |
127 | + PushMsg.LoginUser lu = pm.getLoginUser(); | |
134 | 128 | lu.setUid(uid); |
135 | 129 | lu.setLogin(true); |
136 | 130 | |
137 | 131 | PushContext pushContext = PushContextFactory.getDefault().getPushContext(); |
138 | - pushContext.push("/login_" + chatRoom, lu); | |
132 | + pushContext.push("/" + chatRoom, pm); | |
139 | 133 | } |
140 | 134 | |
141 | 135 | ///after login, redirect to the user specified url. |
@@ -167,15 +161,17 @@ public class UserBean implements Serializable { | ||
167 | 161 | //notify log in message |
168 | 162 | for (SelectItem si : flights) { |
169 | 163 | String chatRoom = si.getValue().toString(); |
170 | - LoginUser lu = new LoginUser(); | |
164 | + PushMsg pm = PushMsg.newLoginPushMsg(); | |
165 | + PushMsg.LoginUser lu = pm.getLoginUser(); | |
171 | 166 | lu.setUid(uid); |
172 | 167 | lu.setLogin(false); |
173 | 168 | |
174 | 169 | PushContext pushContext = PushContextFactory.getDefault().getPushContext(); |
175 | - pushContext.push("/login_" + chatRoom, lu); | |
170 | + pushContext.push("/" + chatRoom, pm); | |
176 | 171 | } |
177 | 172 | loginMemberBean.login(uid, false); |
178 | 173 | context.getExternalContext().invalidateSession(); |
174 | + //login = false; | |
179 | 175 | return "/login.jsf"; |
180 | 176 | } |
181 | 177 |
@@ -4,12 +4,24 @@ | ||
4 | 4 | xmlns:f="http://java.sun.com/jsf/core" |
5 | 5 | xmlns:ui="http://java.sun.com/jsf/facelets"> |
6 | 6 | |
7 | - <ui:composition template="/WEB-INF/templates/moto-template.xhtml"> | |
7 | + <ui:composition | |
8 | + template="/WEB-INF/templates/moto-template.xhtml" | |
9 | + xmlns:p="http://primefaces.org/ui" | |
10 | + > | |
8 | 11 | <ui:define name="pageTitle">jsf chat app</ui:define> |
9 | 12 | |
10 | 13 | <ui:define name="ext-html-header"> |
11 | 14 | <script type="text/javascript"> |
12 | 15 | //<![CDATA[ |
16 | + function handleMessage(data) { | |
17 | + if (data.pushType == 'PUSH_TYPE_CHAT_MESSAGE') { | |
18 | + handleChatMsg(data.chatMessage); | |
19 | + } | |
20 | + else if (data.pushType == 'PUSH_TYPE_LOGIN') { | |
21 | + handleLogin(data.loginUser); | |
22 | + } | |
23 | + } | |
24 | + | |
13 | 25 | function handleChatMsg(data) { |
14 | 26 | var msgList = document.getElementById("chatForm:msgList"); |
15 | 27 |
@@ -46,6 +58,9 @@ | ||
46 | 58 | |
47 | 59 | //]]> |
48 | 60 | </script> |
61 | + | |
62 | +<p:socket onMessage="handleMessage" channel="/#{chatBean.chatRoom}" /> | |
63 | + | |
49 | 64 | </ui:define> |
50 | 65 | |
51 | 66 | <ui:define name="pageHeader" /> |
@@ -56,16 +71,10 @@ | ||
56 | 71 | <h:outputText value="#{chatBean.viewId}"/> |
57 | 72 | <br /> |
58 | 73 | |
59 | - <h:form id="chatForm" | |
60 | - xmlns:a4j="http://richfaces.org/a4j" | |
61 | - xmlns:p="http://primefaces.org/ui" | |
62 | - xmlns:rich="http://richfaces.org/rich"> | |
74 | + <h:form id="chatForm"> | |
63 | 75 | |
64 | 76 | <h:inputHidden id="viewId" value="#{chatBean.viewId}"/> |
65 | 77 | |
66 | -<p:socket onMessage="handleChatMsg" channel="/#{chatBean.chatRoom}" /> | |
67 | -<p:socket onMessage="handleLogin" channel="/login_#{chatBean.chatRoom}" /> | |
68 | - | |
69 | 78 | chat room : |
70 | 79 | <h:selectOneMenu id="chatRoom" value="#{chatBean.chatRoom}" onchange="submit()" |
71 | 80 | valueChangeListener="#{chatBean.chatRoomChanged}" > |