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

Side by Side Diff: lib/src/web_socket_handler.dart

Issue 1758123002: Use the web_socket_channel package. (Closed) Base URL: git@github.com:dart-lang/shelf_web_socket@master
Patch Set: Created 4 years, 9 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
OLDNEW
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 import 'dart:convert'; 5 import 'dart:convert';
6 6
7 import 'package:http_parser/http_parser.dart'; 7 import 'package:web_socket_channel/web_socket_channel.dart';
kevmoo 2016/03/03 00:50:50 sorting?
nweiz 2016/03/03 20:08:22 Done.
8 import 'package:shelf/shelf.dart'; 8 import 'package:shelf/shelf.dart';
9 9
10 /// A class that exposes a handler for upgrading WebSocket requests. 10 /// A class that exposes a handler for upgrading WebSocket requests.
11 class WebSocketHandler { 11 class WebSocketHandler {
12 /// The function to call when a request is upgraded. 12 /// The function to call when a request is upgraded.
13 final Function _onConnection; 13 final Function _onConnection;
14 14
15 /// The set of protocols the user supports, or `null`. 15 /// The set of protocols the user supports, or `null`.
16 final Set<String> _protocols; 16 final Set<String> _protocols;
17 17
(...skipping 45 matching lines...) Expand 10 before | Expand all | Expand 10 after
63 return _forbidden('invalid origin "$origin".'); 63 return _forbidden('invalid origin "$origin".');
64 } 64 }
65 65
66 var protocol = _chooseProtocol(request); 66 var protocol = _chooseProtocol(request);
67 request.hijack((channel) { 67 request.hijack((channel) {
68 var sink = UTF8.encoder.startChunkedConversion(channel.sink); 68 var sink = UTF8.encoder.startChunkedConversion(channel.sink);
69 sink.add( 69 sink.add(
70 "HTTP/1.1 101 Switching Protocols\r\n" 70 "HTTP/1.1 101 Switching Protocols\r\n"
71 "Upgrade: websocket\r\n" 71 "Upgrade: websocket\r\n"
72 "Connection: Upgrade\r\n" 72 "Connection: Upgrade\r\n"
73 "Sec-WebSocket-Accept: ${CompatibleWebSocket.signKey(key)}\r\n"); 73 "Sec-WebSocket-Accept: ${WebSocketChannel.signKey(key)}\r\n");
74 if (protocol != null) sink.add("Sec-WebSocket-Protocol: $protocol\r\n"); 74 if (protocol != null) sink.add("Sec-WebSocket-Protocol: $protocol\r\n");
75 sink.add("\r\n"); 75 sink.add("\r\n");
76 76
77 _onConnection(new WebSocketChannel(channel), protocol); 77 _onConnection(new WebSocketChannel(channel), protocol);
78 }); 78 });
79 79
80 // [request.hijack] is guaranteed to throw a [HijackException], so we'll 80 // [request.hijack] is guaranteed to throw a [HijackException], so we'll
81 // never get here. 81 // never get here.
82 assert(false); 82 assert(false);
83 return null; 83 return null;
(...skipping 40 matching lines...) Expand 10 before | Expand all | Expand 10 after
124 <html> 124 <html>
125 <head><title>$title</title></head> 125 <head><title>$title</title></head>
126 <body> 126 <body>
127 <h1>$title</h1> 127 <h1>$title</h1>
128 <p>$message</p> 128 <p>$message</p>
129 </body> 129 </body>
130 </html> 130 </html>
131 """, headers: {'content-type': 'text/html'}); 131 """, headers: {'content-type': 'text/html'});
132 } 132 }
133 } 133 }
OLDNEW

Powered by Google App Engine
This is Rietveld 408576698