| OLD | NEW |
| (Empty) | |
| 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 |
| 3 // BSD-style license that can be found in the LICENSE file. |
| 4 |
| 5 library channel.web_socket; |
| 6 |
| 7 import 'dart:async'; |
| 8 import 'dart:convert'; |
| 9 import 'dart:io'; |
| 10 |
| 11 import 'package:analysis_server/src/protocol.dart'; |
| 12 import 'package:analysis_server/src/channel/channel.dart'; |
| 13 |
| 14 |
| 15 /** |
| 16 * Instances of the class [WebSocketClientChannel] implement a |
| 17 * [ClientCommunicationChannel] that uses a [WebSocket] to communicate with |
| 18 * servers. |
| 19 */ |
| 20 class WebSocketClientChannel implements ClientCommunicationChannel { |
| 21 /** |
| 22 * The socket being wrapped. |
| 23 */ |
| 24 final WebSocket socket; |
| 25 |
| 26 @override |
| 27 Stream<Response> responseStream; |
| 28 |
| 29 @override |
| 30 Stream<Notification> notificationStream; |
| 31 |
| 32 /** |
| 33 * Initialize a new [WebSocket] wrapper for the given [socket]. |
| 34 */ |
| 35 WebSocketClientChannel(this.socket) { |
| 36 Stream jsonStream = socket |
| 37 .where((data) => data is String) |
| 38 .transform(new JsonStreamDecoder()) |
| 39 .where((json) => json is Map) |
| 40 .asBroadcastStream(); |
| 41 responseStream = jsonStream |
| 42 .where((json) => json[Notification.EVENT] == null) |
| 43 .transform(new ResponseConverter()) |
| 44 .asBroadcastStream(); |
| 45 notificationStream = jsonStream |
| 46 .where((json) => json[Notification.EVENT] != null) |
| 47 .transform(new NotificationConverter()) |
| 48 .asBroadcastStream(); |
| 49 } |
| 50 |
| 51 @override |
| 52 Future<Response> sendRequest(Request request) { |
| 53 String id = request.id; |
| 54 socket.add(JSON.encode(request.toJson())); |
| 55 return responseStream.firstWhere((Response response) => response.id == id); |
| 56 } |
| 57 |
| 58 @override |
| 59 Future close() { |
| 60 return socket.close(); |
| 61 } |
| 62 } |
| 63 |
| 64 /** |
| 65 * Instances of the class [WebSocketServerChannel] implement a |
| 66 * [ServerCommunicationChannel] that uses a [WebSocket] to communicate with |
| 67 * clients. |
| 68 */ |
| 69 class WebSocketServerChannel implements ServerCommunicationChannel { |
| 70 /** |
| 71 * The socket being wrapped. |
| 72 */ |
| 73 final WebSocket socket; |
| 74 |
| 75 /** |
| 76 * Initialize a newly create [WebSocket] wrapper to wrap the given [socket]. |
| 77 */ |
| 78 WebSocketServerChannel(this.socket); |
| 79 |
| 80 @override |
| 81 void listen(void onRequest(Request request), {void onError(), void onDone()})
{ |
| 82 socket.listen((data) => readRequest(data, onRequest), onError: onError, |
| 83 onDone: onDone); |
| 84 } |
| 85 |
| 86 @override |
| 87 void sendNotification(Notification notification) { |
| 88 socket.add(JSON.encode(notification.toJson())); |
| 89 } |
| 90 |
| 91 @override |
| 92 void sendResponse(Response response) { |
| 93 socket.add(JSON.encode(response.toJson())); |
| 94 } |
| 95 |
| 96 /** |
| 97 * Read a request from the given [data] and use the given function to handle |
| 98 * the request. |
| 99 */ |
| 100 void readRequest(Object data, void onRequest(Request request)) { |
| 101 if (data is String) { |
| 102 // Parse the string as a JSON descriptor and process the resulting |
| 103 // structure as a request. |
| 104 Request request = new Request.fromString(data); |
| 105 if (request == null) { |
| 106 sendResponse(new Response.invalidRequestFormat()); |
| 107 return; |
| 108 } |
| 109 onRequest(request); |
| 110 } else if (data is List<int>) { |
| 111 // TODO(brianwilkerson) Implement a more efficient protocol. |
| 112 sendResponse(new Response.invalidRequestFormat()); |
| 113 } else { |
| 114 sendResponse(new Response.invalidRequestFormat()); |
| 115 } |
| 116 } |
| 117 |
| 118 @override |
| 119 void close() { |
| 120 socket.close(WebSocketStatus.NORMAL_CLOSURE); |
| 121 } |
| 122 } |
| OLD | NEW |