| OLD | NEW |
| 1 // Copyright (c) 2014, the Dart project authors. Please see the AUTHORS file | 1 // Copyright (c) 2014, 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 channel.web_socket; | 5 library channel.web_socket; |
| 6 | 6 |
| 7 import 'dart:async'; | 7 import 'dart:async'; |
| 8 import 'dart:convert'; | 8 import 'dart:convert'; |
| 9 import 'dart:io'; | 9 import 'dart:io'; |
| 10 | 10 |
| (...skipping 67 matching lines...) Expand 10 before | Expand all | Expand 10 after Loading... |
| 78 WebSocketServerChannel(this.socket); | 78 WebSocketServerChannel(this.socket); |
| 79 | 79 |
| 80 @override | 80 @override |
| 81 void listen(void onRequest(Request request), {void onError(), void onDone()})
{ | 81 void listen(void onRequest(Request request), {void onError(), void onDone()})
{ |
| 82 socket.listen((data) => readRequest(data, onRequest), onError: onError, | 82 socket.listen((data) => readRequest(data, onRequest), onError: onError, |
| 83 onDone: onDone); | 83 onDone: onDone); |
| 84 } | 84 } |
| 85 | 85 |
| 86 @override | 86 @override |
| 87 void sendNotification(Notification notification) { | 87 void sendNotification(Notification notification) { |
| 88 socket.add(JSON.encode(notification.toJson())); | 88 ServerCommunicationChannel.ToJson.start(); |
| 89 String jsonEncoding = JSON.encode(notification.toJson()); |
| 90 ServerCommunicationChannel.ToJson.stop(); |
| 91 socket.add(jsonEncoding); |
| 89 } | 92 } |
| 90 | 93 |
| 91 @override | 94 @override |
| 92 void sendResponse(Response response) { | 95 void sendResponse(Response response) { |
| 93 socket.add(JSON.encode(response.toJson())); | 96 ServerCommunicationChannel.ToJson.start(); |
| 97 String jsonEncoding = JSON.encode(response.toJson()); |
| 98 ServerCommunicationChannel.ToJson.stop(); |
| 99 socket.add(jsonEncoding); |
| 94 } | 100 } |
| 95 | 101 |
| 96 /** | 102 /** |
| 97 * Read a request from the given [data] and use the given function to handle | 103 * Read a request from the given [data] and use the given function to handle |
| 98 * the request. | 104 * the request. |
| 99 */ | 105 */ |
| 100 void readRequest(Object data, void onRequest(Request request)) { | 106 void readRequest(Object data, void onRequest(Request request)) { |
| 101 if (data is String) { | 107 if (data is String) { |
| 102 // Parse the string as a JSON descriptor and process the resulting | 108 // Parse the string as a JSON descriptor and process the resulting |
| 103 // structure as a request. | 109 // structure as a request. |
| 110 ServerCommunicationChannel.FromJson.start(); |
| 104 Request request = new Request.fromString(data); | 111 Request request = new Request.fromString(data); |
| 112 ServerCommunicationChannel.FromJson.stop(); |
| 105 if (request == null) { | 113 if (request == null) { |
| 106 sendResponse(new Response.invalidRequestFormat()); | 114 sendResponse(new Response.invalidRequestFormat()); |
| 107 return; | 115 return; |
| 108 } | 116 } |
| 109 onRequest(request); | 117 onRequest(request); |
| 110 } else if (data is List<int>) { | 118 } else if (data is List<int>) { |
| 111 // TODO(brianwilkerson) Implement a more efficient protocol. | 119 // TODO(brianwilkerson) Implement a more efficient protocol. |
| 112 sendResponse(new Response.invalidRequestFormat()); | 120 sendResponse(new Response.invalidRequestFormat()); |
| 113 } else { | 121 } else { |
| 114 sendResponse(new Response.invalidRequestFormat()); | 122 sendResponse(new Response.invalidRequestFormat()); |
| 115 } | 123 } |
| 116 } | 124 } |
| 117 | 125 |
| 118 @override | 126 @override |
| 119 void close() { | 127 void close() { |
| 120 socket.close(WebSocketStatus.NORMAL_CLOSURE); | 128 socket.close(WebSocketStatus.NORMAL_CLOSURE); |
| 121 } | 129 } |
| 122 } | 130 } |
| OLD | NEW |