moto web application
リビジョン | 1816d8604e55b94b08df43ab939ef7b4b32ebd96 (tree) |
---|---|
日時 | 2014-02-03 17:27:16 |
作者 | astoria-d <astoria-d@mail...> |
コミッター | astoria-d |
clean up
@@ -43,18 +43,4 @@ public class AtmosRes { | ||
43 | 43 | } |
44 | 44 | } |
45 | 45 | |
46 | - /** | |
47 | - * Simple annotated class that demonstrate how {@link org.atmosphere.config.managed.Encoder} and {@link org.atmosphere.config.managed.Decoder | |
48 | - * can be used. | |
49 | - * | |
50 | - * @param message an instance of {@link Message} | |
51 | - * @return | |
52 | - * @throws IOException | |
53 | - @org.atmosphere.config.service.Message(encoders = {JacksonEncoder.class}, decoders = {JacksonDecoder.class}) | |
54 | - public Message onMessage(Message message) throws IOException { | |
55 | - logger.info("{} just send {}", message.getAuthor(), message.getMessage()); | |
56 | - return message; | |
57 | - } | |
58 | - */ | |
59 | - | |
60 | 46 | } |
\ No newline at end of file |
@@ -28,9 +28,6 @@ import java.util.Date; | ||
28 | 28 | import java.text.SimpleDateFormat; |
29 | 29 | import java.util.UUID; |
30 | 30 | |
31 | -//import org.primefaces.push.PushContext; | |
32 | -//import org.primefaces.push.PushContextFactory; | |
33 | - | |
34 | 31 | import org.atmosphere.cpr.Broadcaster; |
35 | 32 | import org.atmosphere.cpr.BroadcasterFactory; |
36 | 33 | import org.codehaus.jackson.map.ObjectMapper; |
@@ -227,9 +224,6 @@ public class ChatBean implements Serializable { | ||
227 | 224 | cm.setDate(dt); |
228 | 225 | cm.setUname(userBean.getUid()); |
229 | 226 | |
230 | - //PushContext pushContext = PushContextFactory.getDefault().getPushContext(); | |
231 | - //pushContext.push("/" + chatRoom, pm); | |
232 | - | |
233 | 227 | Broadcaster b = BroadcasterFactory.getDefault().lookup("/chat", true); |
234 | 228 | ObjectMapper mapper = new ObjectMapper(); |
235 | 229 | try { |
@@ -18,8 +18,10 @@ import javax.servlet.http.HttpSession; | ||
18 | 18 | import java.util.ArrayList; |
19 | 19 | import javax.faces.model.SelectItem; |
20 | 20 | |
21 | -//import org.primefaces.push.PushContext; | |
22 | -//import org.primefaces.push.PushContextFactory; | |
21 | +import org.atmosphere.cpr.Broadcaster; | |
22 | +import org.atmosphere.cpr.BroadcasterFactory; | |
23 | +import org.codehaus.jackson.map.ObjectMapper; | |
24 | +import java.io.IOException; | |
23 | 25 | |
24 | 26 | @ManagedBean |
25 | 27 | @SessionScoped |
@@ -123,12 +125,19 @@ public class UserBean implements Serializable { | ||
123 | 125 | for (SelectItem si : flights) { |
124 | 126 | String chatRoom = si.getValue().toString(); |
125 | 127 | |
126 | - /*PushMsg pm = PushMsg.newLoginPushMsg(); | |
127 | - pm.lu.uid = uid; | |
128 | - pm.lu.login = true; | |
129 | -*/ | |
130 | - //PushContext pushContext = PushContextFactory.getDefault().getPushContext(); | |
131 | - //pushContext.push("/" + chatRoom, pm); | |
128 | + PushMsg pm = PushMsg.newLoginPushMsg(); | |
129 | + PushMsg.LoginUser lu = pm.getLoginUser(); | |
130 | + lu.setUid(uid); | |
131 | + lu.setLogin(true); | |
132 | + | |
133 | + Broadcaster b = BroadcasterFactory.getDefault().lookup("/chat", true); | |
134 | + ObjectMapper mapper = new ObjectMapper(); | |
135 | + try { | |
136 | + String json = mapper.writeValueAsString(pm); | |
137 | + b.broadcast(json); | |
138 | + } catch (IOException e) { | |
139 | + log.severe("json encode error."); | |
140 | + } | |
132 | 141 | } |
133 | 142 | |
134 | 143 | ///after login, redirect to the user specified url. |
@@ -160,12 +169,19 @@ public class UserBean implements Serializable { | ||
160 | 169 | //notify log in message |
161 | 170 | for (SelectItem si : flights) { |
162 | 171 | String chatRoom = si.getValue().toString(); |
163 | - /*PushMsg pm = PushMsg.newLoginPushMsg(); | |
164 | - pm.lu.uid = uid; | |
165 | - pm.lu.login = false; | |
166 | -*/ | |
167 | - //PushContext pushContext = PushContextFactory.getDefault().getPushContext(); | |
168 | - //pushContext.push("/" + chatRoom, pm); | |
172 | + PushMsg pm = PushMsg.newLoginPushMsg(); | |
173 | + PushMsg.LoginUser lu = pm.getLoginUser(); | |
174 | + lu.setUid(uid); | |
175 | + lu.setLogin(false); | |
176 | + | |
177 | + Broadcaster b = BroadcasterFactory.getDefault().lookup("/chat", true); | |
178 | + ObjectMapper mapper = new ObjectMapper(); | |
179 | + try { | |
180 | + String json = mapper.writeValueAsString(pm); | |
181 | + b.broadcast(json); | |
182 | + } catch (IOException e) { | |
183 | + log.severe("json encode error."); | |
184 | + } | |
169 | 185 | } |
170 | 186 | loginMemberBean.login(uid, false); |
171 | 187 | context.getExternalContext().invalidateSession(); |
@@ -74,19 +74,12 @@ $(function () { | ||
74 | 74 | var content = $('#content'); |
75 | 75 | //var input = $('#input'); |
76 | 76 | var input = $('#chatForm\\:msg'); |
77 | - var submitBtn = $('#chatForm\\:postBtn'); | |
78 | - var inputStatus = $('#chatForm\\:inputStatus'); | |
79 | - | |
77 | + | |
80 | 78 | var status = $('#status'); |
81 | - var myName = false; | |
82 | - var author = null; | |
83 | - var logged = false; | |
84 | 79 | var socket = atmosphere; |
85 | 80 | var subSocket; |
86 | 81 | var transport = 'websocket'; |
87 | 82 | |
88 | - inputStatus.value = 'init'; | |
89 | - | |
90 | 83 | // We are now ready to cut the request |
91 | 84 | var request = { url: '/moto_web_app/chat', |
92 | 85 | contentType : "application/json", |
@@ -110,7 +103,7 @@ $(function () { | ||
110 | 103 | |
111 | 104 | request.onClientTimeout = function(r) { |
112 | 105 | content.html($('<p>', { text: 'Client closed the connection after a timeout. Reconnecting in ' + request.reconnectInterval })); |
113 | - subSocket.push(atmosphere.util.stringifyJSON({ author: author, message: 'is inactive and closed the connection. Will reconnect in ' + request.reconnectInterval })); | |
106 | + //subSocket.push(atmosphere.util.stringifyJSON({ author: author, message: 'is inactive and closed the connection. Will reconnect in ' + request.reconnectInterval })); | |
114 | 107 | input.attr('disabled', 'disabled'); |
115 | 108 | setTimeout(function (){ |
116 | 109 | subSocket = socket.subscribe(request); |
@@ -134,28 +127,19 @@ $(function () { | ||
134 | 127 | var message = response.responseBody; |
135 | 128 | try { |
136 | 129 | var json = atmosphere.util.parseJSON(message); |
137 | - handleMessage(json); | |
138 | 130 | } catch (e) { |
139 | 131 | console.log('This doesn\'t look like a valid JSON: ', message); |
140 | 132 | return; |
141 | 133 | } |
142 | - | |
143 | 134 | |
135 | + handleMessage(json); | |
144 | 136 | input.removeAttr('disabled').focus(); |
145 | - /*if (!logged && myName) { | |
146 | - logged = true; | |
147 | - status.text(myName + ': ').css('color', 'blue'); | |
148 | - } else { | |
149 | - var me = json.author == author; | |
150 | - var date = typeof(json.time) == 'string' ? parseInt(json.time) : json.time; | |
151 | - addMessage(json.author, json.message, me ? 'blue' : 'black', new Date(date)); | |
152 | - }*/ | |
153 | 137 | }; |
154 | 138 | |
155 | 139 | request.onClose = function(response) { |
156 | 140 | content.html($('<p>', { text: 'Server closed the connection after a timeout' })); |
157 | 141 | if (subSocket) { |
158 | - subSocket.push(atmosphere.util.stringifyJSON({ author: author, message: 'disconnecting' })); | |
142 | + //subSocket.push(atmosphere.util.stringifyJSON({ author: author, message: 'disconnecting' })); | |
159 | 143 | } |
160 | 144 | input.attr('disabled', 'disabled'); |
161 | 145 | }; |
@@ -163,7 +147,6 @@ $(function () { | ||
163 | 147 | request.onError = function(response) { |
164 | 148 | content.html($('<p>', { text: 'Sorry, but there\'s some problem with your ' |
165 | 149 | + 'socket or the server is down' })); |
166 | - logged = false; | |
167 | 150 | }; |
168 | 151 | |
169 | 152 | request.onReconnect = function(request, response) { |
@@ -172,82 +155,9 @@ $(function () { | ||
172 | 155 | }; |
173 | 156 | |
174 | 157 | subSocket = socket.subscribe(request); |
175 | - /* | |
176 | - input.keydown(function(e) { | |
177 | - //console.log(e); | |
178 | - console.log("keydown. kc: " + e.keyCode + ", st: " + inputStatus.value); | |
179 | - if (e.keyCode === 13) { | |
180 | - var msg = $(this).val(); | |
181 | - | |
182 | - // First message is always the author's name | |
183 | - if (author == null) { | |
184 | - author = msg; | |
185 | - } | |
186 | - | |
187 | - //subSocket.push(atmosphere.util.stringifyJSON({ author: author, message: msg })); | |
188 | -// $(this).val(''); | |
189 | - | |
190 | -// input.attr('disabled', 'disabled'); | |
191 | - if (myName === false) { | |
192 | - myName = msg; | |
193 | - } | |
194 | - inputStatus.value = 'dataSubmit'; | |
195 | - inputStatus.change(); | |
196 | - } | |
197 | - }); | |
198 | - | |
199 | - inputStatus.change(function () { | |
200 | - console.log('status changed: ' + inputStatus.value); | |
201 | - if (inputStatus.value == 'dataProcessed') { | |
202 | - alert('bbb'); | |
203 | - inputStatus.value = 'pushRequested'; | |
204 | - inputStatus.change(); | |
205 | - } | |
206 | - }); | |
207 | - | |
208 | - function pushMsg(uid, msg) { | |
209 | - subSocket.push(atmosphere.util.stringifyJSON({ author: uid, message: msg })); | |
210 | - } | |
211 | -*/ | |
212 | - function addMessage(author, message, color, datetime) { | |
213 | - content.append('<p><span style="color:' + color + '">' + author + '</span> @ ' + | |
214 | - + (datetime.getHours() < 10 ? '0' + datetime.getHours() : datetime.getHours()) + ':' | |
215 | - + (datetime.getMinutes() < 10 ? '0' + datetime.getMinutes() : datetime.getMinutes()) | |
216 | - + ': ' + message + '</p>'); | |
217 | - } | |
218 | -}); | |
219 | 158 | |
220 | -/* function handlePostBtn(data) { | |
221 | - var buttonElement = data.source; // The HTML DOM element which invoked the ajax event. | |
222 | - var ajaxStatus = data.status; // Can be "begin", "complete" and "success". | |
223 | - //var inputStatus = document.getElementById("chatForm:inputStatus"); | |
224 | - var inputStatus = $('#chatForm\\:inputStatus'); | |
159 | +}); | |
225 | 160 | |
226 | - switch (ajaxStatus) { | |
227 | - case "begin": // This is called right before ajax request is been sent. | |
228 | - buttonElement.disabled = true; | |
229 | - break; | |
230 | - | |
231 | - case "complete": // This is called right after ajax response is received. | |
232 | - // We don't want to enable it yet here, right? | |
233 | - break; | |
234 | - | |
235 | - case "success": // This is called when ajax response is successfully processed. | |
236 | - buttonElement.disabled = false; | |
237 | - | |
238 | - var msg = document.getElementById("chatForm:msg"); | |
239 | - msg.value = ''; | |
240 | - msg.focus(); | |
241 | - //pushMsg('aaa', 'bss'); | |
242 | - inputStatus.value = "dataProcessed"; | |
243 | - console.log("ajax return. st: " + inputStatus.value); | |
244 | - inputStatus.change(); | |
245 | - | |
246 | - break; | |
247 | - } | |
248 | - } | |
249 | - */ | |
250 | - | |
251 | 161 | //]]> |
252 | 162 | </script> |
253 | 163 |
@@ -312,7 +222,6 @@ $(function () { | ||
312 | 222 | <f:ajax execute="viewId chatRoom msg" /> |
313 | 223 | <!-- f:ajax execute="viewId chatRoom msg" onevent="handlePostBtn" / --> |
314 | 224 | </h:commandButton> |
315 | - <h:inputHidden id="inputStatus" value=""/> | |
316 | 225 | |
317 | 226 | </td> |
318 | 227 | <td> |
@@ -343,7 +252,6 @@ $(function () { | ||
343 | 252 | <div id="content"></div> |
344 | 253 | <div> |
345 | 254 | <span id="status">Connecting...</span> |
346 | - <input type="text" id="input" disabled="disabled"/> | |
347 | 255 | </div> |
348 | 256 | |
349 | 257 | </ui:define> |