Chromium Code Reviews
chromiumcodereview-hr@appspot.gserviceaccount.com (chromiumcodereview-hr) | Please choose your nickname with Settings | Help | Chromium Project | Gerrit Changes | Sign out
(281)

Side by Side Diff: samples/chat/chat_server_lib.dart

Issue 23596007: Remove usage of dart:json. (Closed) Base URL: https://dart.googlecode.com/svn/branches/bleeding_edge/dart
Patch Set: Rebase. Created 7 years, 3 months ago
Use n/p to move between diff chunks; N/P to move between comments. Draft comments are only viewable by you.
Jump to:
View unified diff | Download patch | Annotate | Revision Log
OLDNEW
1 // Copyright (c) 2013, the Dart project authors. Please see the AUTHORS file 1 // Copyright (c) 2013, 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_server; 5 library chat_server;
6 import 'dart:async'; 6 import 'dart:async';
7 import 'dart:io'; 7 import 'dart:io';
8 import 'dart:isolate'; 8 import 'dart:isolate';
9 import 'dart:json' as json; 9 import "dart:convert";
10 import 'dart:math'; 10 import 'dart:math';
11 11
12 void startChatServer() { 12 void startChatServer() {
13 var server = new ChatServer(); 13 var server = new ChatServer();
14 server.init(); 14 server.init();
15 port.receive(server.dispatch); 15 port.receive(server.dispatch);
16 } 16 }
17 17
18 class ChatServer extends IsolatedServer { 18 class ChatServer extends IsolatedServer {
19 } 19 }
(...skipping 276 matching lines...) Expand 10 before | Expand all | Expand 10 after
296 static const String notFoundPageHtml = """ 296 static const String notFoundPageHtml = """
297 <html><head> 297 <html><head>
298 <title>404 Not Found</title> 298 <title>404 Not Found</title>
299 </head><body> 299 </head><body>
300 <h1>Not Found</h1> 300 <h1>Not Found</h1>
301 <p>The requested URL was not found on this server.</p> 301 <p>The requested URL was not found on this server.</p>
302 </body></html>"""; 302 </body></html>""";
303 303
304 void _sendJSONResponse(HttpResponse response, Map responseData) { 304 void _sendJSONResponse(HttpResponse response, Map responseData) {
305 response.headers.set("Content-Type", "application/json; charset=UTF-8"); 305 response.headers.set("Content-Type", "application/json; charset=UTF-8");
306 response.write(json.stringify(responseData)); 306 response.write(JSON.encode(responseData));
307 response.close(); 307 response.close();
308 } 308 }
309 309
310 void redirectPageHandler(HttpRequest request, 310 void redirectPageHandler(HttpRequest request,
311 HttpResponse response, 311 HttpResponse response,
312 String redirectPath) { 312 String redirectPath) {
313 if (_redirectPage == null) { 313 if (_redirectPage == null) {
314 _redirectPage = redirectPageHtml.codeUnits; 314 _redirectPage = redirectPageHtml.codeUnits;
315 } 315 }
316 response.statusCode = HttpStatus.FOUND; 316 response.statusCode = HttpStatus.FOUND;
(...skipping 57 matching lines...) Expand 10 before | Expand all | Expand 10 after
374 // Join request: 374 // Join request:
375 // { "request": "join", 375 // { "request": "join",
376 // "handle": <handle> } 376 // "handle": <handle> }
377 void _joinHandler(HttpRequest request, HttpResponse response) { 377 void _joinHandler(HttpRequest request, HttpResponse response) {
378 StringBuffer body = new StringBuffer(); 378 StringBuffer body = new StringBuffer();
379 request.listen( 379 request.listen(
380 (data) => body.write(new String.fromCharCodes(data)), 380 (data) => body.write(new String.fromCharCodes(data)),
381 onDone: () { 381 onDone: () {
382 String data = body.toString(); 382 String data = body.toString();
383 if (data != null) { 383 if (data != null) {
384 var requestData = json.parse(data); 384 var requestData = JSON.decode(data);
385 if (requestData["request"] == "join") { 385 if (requestData["request"] == "join") {
386 String handle = requestData["handle"]; 386 String handle = requestData["handle"];
387 if (handle != null) { 387 if (handle != null) {
388 // New user joining. 388 // New user joining.
389 User user = _topic._userJoined(handle); 389 User user = _topic._userJoined(handle);
390 390
391 // Send response. 391 // Send response.
392 Map responseData = new Map(); 392 Map responseData = new Map();
393 responseData["response"] = "join"; 393 responseData["response"] = "join";
394 responseData["sessionId"] = user.sessionId; 394 responseData["sessionId"] = user.sessionId;
(...skipping 12 matching lines...) Expand all
407 407
408 // Leave request: 408 // Leave request:
409 // { "request": "leave", 409 // { "request": "leave",
410 // "sessionId": <sessionId> } 410 // "sessionId": <sessionId> }
411 void _leaveHandler(HttpRequest request, HttpResponse response) { 411 void _leaveHandler(HttpRequest request, HttpResponse response) {
412 StringBuffer body = new StringBuffer(); 412 StringBuffer body = new StringBuffer();
413 request.listen( 413 request.listen(
414 (data) => body.write(new String.fromCharCodes(data)), 414 (data) => body.write(new String.fromCharCodes(data)),
415 onDone: () { 415 onDone: () {
416 String data = body.toString(); 416 String data = body.toString();
417 var requestData = json.parse(data); 417 var requestData = JSON.decode(data);
418 if (requestData["request"] == "leave") { 418 if (requestData["request"] == "leave") {
419 String sessionId = requestData["sessionId"]; 419 String sessionId = requestData["sessionId"];
420 if (sessionId != null) { 420 if (sessionId != null) {
421 // User leaving. 421 // User leaving.
422 _topic._userLeft(sessionId); 422 _topic._userLeft(sessionId);
423 423
424 // Send response. 424 // Send response.
425 Map responseData = new Map(); 425 Map responseData = new Map();
426 responseData["response"] = "leave"; 426 responseData["response"] = "leave";
427 _sendJSONResponse(response, responseData); 427 _sendJSONResponse(response, responseData);
(...skipping 11 matching lines...) Expand all
439 // "sessionId": <sessionId>, 439 // "sessionId": <sessionId>,
440 // "message": <message> } 440 // "message": <message> }
441 void _messageHandler(HttpRequest request, HttpResponse response) { 441 void _messageHandler(HttpRequest request, HttpResponse response) {
442 StringBuffer body = new StringBuffer(); 442 StringBuffer body = new StringBuffer();
443 request.listen( 443 request.listen(
444 (data) => body.write(new String.fromCharCodes(data)), 444 (data) => body.write(new String.fromCharCodes(data)),
445 onDone: () { 445 onDone: () {
446 String data = body.toString(); 446 String data = body.toString();
447 _messageCount++; 447 _messageCount++;
448 _messageRate.record(1); 448 _messageRate.record(1);
449 var requestData = json.parse(data); 449 var requestData = JSON.decode(data);
450 if (requestData["request"] == "message") { 450 if (requestData["request"] == "message") {
451 String sessionId = requestData["sessionId"]; 451 String sessionId = requestData["sessionId"];
452 if (sessionId != null) { 452 if (sessionId != null) {
453 // New message from user. 453 // New message from user.
454 bool success = _topic._userMessage(requestData); 454 bool success = _topic._userMessage(requestData);
455 455
456 // Send response. 456 // Send response.
457 if (success) { 457 if (success) {
458 Map responseData = new Map(); 458 Map responseData = new Map();
459 responseData["response"] = "message"; 459 responseData["response"] = "message";
(...skipping 14 matching lines...) Expand all
474 // { "request": "receive", 474 // { "request": "receive",
475 // "sessionId": <sessionId>, 475 // "sessionId": <sessionId>,
476 // "nextMessage": <nextMessage>, 476 // "nextMessage": <nextMessage>,
477 // "maxMessages": <maxMesssages> } 477 // "maxMessages": <maxMesssages> }
478 void _receiveHandler(HttpRequest request, HttpResponse response) { 478 void _receiveHandler(HttpRequest request, HttpResponse response) {
479 StringBuffer body = new StringBuffer(); 479 StringBuffer body = new StringBuffer();
480 request.listen( 480 request.listen(
481 (data) => body.write(new String.fromCharCodes(data)), 481 (data) => body.write(new String.fromCharCodes(data)),
482 onDone: () { 482 onDone: () {
483 String data = body.toString(); 483 String data = body.toString();
484 var requestData = json.parse(data); 484 var requestData = JSON.decode(data);
485 if (requestData["request"] == "receive") { 485 if (requestData["request"] == "receive") {
486 String sessionId = requestData["sessionId"]; 486 String sessionId = requestData["sessionId"];
487 int nextMessage = requestData["nextMessage"]; 487 int nextMessage = requestData["nextMessage"];
488 int maxMessages = requestData["maxMessages"]; 488 int maxMessages = requestData["maxMessages"];
489 if (sessionId != null && nextMessage != null) { 489 if (sessionId != null && nextMessage != null) {
490 490
491 void sendResponse(messages) { 491 void sendResponse(messages) {
492 // Send response. 492 // Send response.
493 Map responseData = new Map(); 493 Map responseData = new Map();
494 responseData["response"] = "receive"; 494 responseData["response"] = "receive";
(...skipping 176 matching lines...) Expand 10 before | Expand all | Expand 10 after
671 } 671 }
672 } 672 }
673 673
674 int _timeRange; 674 int _timeRange;
675 List<int> _buckets; 675 List<int> _buckets;
676 int _currentBucket; 676 int _currentBucket;
677 int _currentBucketTime; 677 int _currentBucketTime;
678 num _bucketTimeRange; 678 num _bucketTimeRange;
679 int _sum; 679 int _sum;
680 } 680 }
OLDNEW

Powered by Google App Engine
This is Rietveld 408576698