OLD | NEW |
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:json' as json; |
10 import 'dart:math'; | 10 import 'dart:math'; |
(...skipping 359 matching lines...) Expand 10 before | Expand all | Expand 10 after Loading... |
370 response.contentLength = 0; | 370 response.contentLength = 0; |
371 response.close(); | 371 response.close(); |
372 } | 372 } |
373 | 373 |
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.add(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.parse(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 |
(...skipping 13 matching lines...) Expand all Loading... |
404 } | 404 } |
405 }); | 405 }); |
406 } | 406 } |
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.add(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.parse(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); |
428 } else { | 428 } else { |
429 _protocolError(request, response); | 429 _protocolError(request, response); |
430 } | 430 } |
431 } else { | 431 } else { |
432 _protocolError(request, response); | 432 _protocolError(request, response); |
433 } | 433 } |
434 }); | 434 }); |
435 } | 435 } |
436 | 436 |
437 // Message request: | 437 // Message request: |
438 // { "request": "message", | 438 // { "request": "message", |
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.add(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.parse(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); |
(...skipping 16 matching lines...) Expand all Loading... |
471 } | 471 } |
472 | 472 |
473 // Receive request: | 473 // Receive request: |
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.add(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.parse(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) { |
(...skipping 179 matching lines...) Expand 10 before | Expand all | Expand 10 after Loading... |
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 } |
OLD | NEW |