OLD | NEW |
1 // Copyright (c) 2012, the Dart project authors. Please see the AUTHORS file | 1 // Copyright (c) 2012, the Dart project authors. Please see the AUTHORS file |
2 // for details. All rights reserved. Use of this source code is governed by a | 2 // for details. All rights reserved. Use of this source code is governed by a |
3 // BSD-style license that can be found in the LICENSE file. | 3 // BSD-style license that can be found in the LICENSE file. |
4 | 4 |
5 library chat; | 5 library chat; |
6 import 'dart:html'; | 6 import 'dart:html'; |
7 import 'dart:json' as jsonlib; | 7 import 'dart:json' as jsonlib; |
8 | 8 |
9 void main() { | 9 void main() { |
10 new Chat().start(); | 10 new Chat().start(); |
(...skipping 199 matching lines...) Expand 10 before | Expand all | Expand 10 after Loading... |
210 _messageInput.focus(); | 210 _messageInput.focus(); |
211 showStatus("Status..."); | 211 showStatus("Status..."); |
212 } | 212 } |
213 | 213 |
214 void uiAddMessage(Map message) { | 214 void uiAddMessage(Map message) { |
215 ParagraphElement p = new Element.tag('p'); | 215 ParagraphElement p = new Element.tag('p'); |
216 String formattedTime = formatMessageTime(message["received"]); | 216 String formattedTime = formatMessageTime(message["received"]); |
217 String from = message["from"]; | 217 String from = message["from"]; |
218 StringBuffer text = new StringBuffer("$formattedTime $from "); | 218 StringBuffer text = new StringBuffer("$formattedTime $from "); |
219 if (message["type"] == "join") { | 219 if (message["type"] == "join") { |
220 text.add("joined"); | 220 text.write("joined"); |
221 } else if (message["type"] == "message") { | 221 } else if (message["type"] == "message") { |
222 text.add(message["message"]); | 222 text.write(message["message"]); |
223 } else if (message["type"] == "leave") { | 223 } else if (message["type"] == "leave") { |
224 text.add("left"); | 224 text.write("left"); |
225 } else { | 225 } else { |
226 text.add("timeout"); | 226 text.write("timeout"); |
227 } | 227 } |
228 p.text = text.toString(); | 228 p.text = text.toString(); |
229 _messages.insertAdjacentElement('afterBegin', p); | 229 _messages.insertAdjacentElement('afterBegin', p); |
230 if (_messages.children.length > 20) { | 230 if (_messages.children.length > 20) { |
231 _messages.children.removeLast(); | 231 _messages.children.removeLast(); |
232 } | 232 } |
233 } | 233 } |
234 | 234 |
235 String formatMessageTime(String received) { | 235 String formatMessageTime(String received) { |
236 DateTime date = DateTime.parse(received); | 236 DateTime date = DateTime.parse(received); |
237 StringBuffer formattedTime = new StringBuffer(); | 237 StringBuffer formattedTime = new StringBuffer(); |
238 if (date.hour < 10) formattedTime.add("0"); | 238 if (date.hour < 10) formattedTime.write("0"); |
239 formattedTime.add(date.hour); | 239 formattedTime.write(date.hour); |
240 formattedTime.add(":"); | 240 formattedTime.write(":"); |
241 if (date.minute < 10) formattedTime.add("0"); | 241 if (date.minute < 10) formattedTime.write("0"); |
242 formattedTime.add(date.minute); | 242 formattedTime.write(date.minute); |
243 formattedTime.add(":"); | 243 formattedTime.write(":"); |
244 if (date.second < 10) formattedTime.add("0"); | 244 if (date.second < 10) formattedTime.write("0"); |
245 formattedTime.add(date.second); | 245 formattedTime.write(date.second); |
246 return formattedTime.toString(); | 246 return formattedTime.toString(); |
247 } | 247 } |
248 | 248 |
249 String formatUpTime(int upTime) { | 249 String formatUpTime(int upTime) { |
250 var upTime = (upTime ~/ 1000); | 250 var upTime = (upTime ~/ 1000); |
251 int hours = (upTime ~/ (60 * 60)); | 251 int hours = (upTime ~/ (60 * 60)); |
252 upTime = upTime % (60 * 60); | 252 upTime = upTime % (60 * 60); |
253 int minutes = (upTime ~/ 60); | 253 int minutes = (upTime ~/ 60); |
254 upTime = upTime % 60; | 254 upTime = upTime % 60; |
255 int seconds = upTime; | 255 int seconds = upTime; |
256 StringBuffer formattedTime = new StringBuffer(); | 256 StringBuffer formattedTime = new StringBuffer(); |
257 if (hours < 10) formattedTime.add("0"); | 257 if (hours < 10) formattedTime.write("0"); |
258 formattedTime.add(hours); | 258 formattedTime.write(hours); |
259 formattedTime.add(":"); | 259 formattedTime.write(":"); |
260 if (minutes < 10) formattedTime.add("0"); | 260 if (minutes < 10) formattedTime.write("0"); |
261 formattedTime.add(minutes); | 261 formattedTime.write(minutes); |
262 formattedTime.add(":"); | 262 formattedTime.write(":"); |
263 if (seconds < 10) formattedTime.add("0"); | 263 if (seconds < 10) formattedTime.write("0"); |
264 formattedTime.add(seconds); | 264 formattedTime.write(seconds); |
265 return formattedTime.toString(); | 265 return formattedTime.toString(); |
266 } | 266 } |
267 | 267 |
268 void protocolError() { | 268 void protocolError() { |
269 uiJoin(); | 269 uiJoin(); |
270 showStatus("Protocol error!"); | 270 showStatus("Protocol error!"); |
271 } | 271 } |
272 | 272 |
273 void showStatus(status) { | 273 void showStatus(status) { |
274 _statusText.text = status; | 274 _statusText.text = status; |
(...skipping 31 matching lines...) Expand 10 before | Expand all | Expand 10 after Loading... |
306 Element _chatSection; | 306 Element _chatSection; |
307 InputElement _messageInput; | 307 InputElement _messageInput; |
308 Element _messages; | 308 Element _messages; |
309 Element _statusText; | 309 Element _statusText; |
310 | 310 |
311 String _session = null; | 311 String _session = null; |
312 int _nextMessage = 0; | 312 int _nextMessage = 0; |
313 HttpRequest _pollRequest = null; | 313 HttpRequest _pollRequest = null; |
314 | 314 |
315 } | 315 } |
OLD | NEW |