Skip to content

Commit 210b0da

Browse files
authored
Merge pull request dart-archive/sse#2 from dart-lang/cleanup
Cleanup
2 parents 89fc0d9 + 6d7ced4 commit 210b0da

File tree

12 files changed

+3285
-3112
lines changed

12 files changed

+3285
-3112
lines changed

pkgs/sse/.travis.yml

Lines changed: 5 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -40,3 +40,8 @@ script:
4040
cache:
4141
directories:
4242
- $HOME/.pub-cache
43+
44+
# Only building master means that we don't run two builds for each pull request.
45+
branches:
46+
only:
47+
- master

pkgs/sse/CHANGELOG.md

Lines changed: 8 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,8 @@
1+
## 1.0.0
2+
3+
- Internal cleanup.
4+
5+
6+
## 0.0.1
7+
8+
- Initial commit.

pkgs/sse/example/index.dart

Lines changed: 15 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,15 @@
1+
// Copyright (c) 2019, 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+
import 'package:sse/client/sse_client.dart';
6+
7+
/// A basic example which should be used in a browser that supports SSE.
8+
void main() {
9+
var channel = SseClient('/sseHandler');
10+
11+
channel.stream.listen((s) {
12+
// Listen for messages and send the back.
13+
channel.sink.add(s);
14+
});
15+
}

pkgs/sse/example/server.dart

Lines changed: 21 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,21 @@
1+
// Copyright (c) 2019, 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+
import 'package:shelf/shelf_io.dart' as io;
6+
import 'package:sse/server/sse_handler.dart';
7+
8+
/// A basic server which sets up an SSE handler.
9+
///
10+
/// When a client connnects it will send a simple message and print the
11+
/// response.
12+
void main() async {
13+
var handler = SseHandler(Uri.parse('/sseHandler'));
14+
await io.serve(handler.handler, 'localhost', 0);
15+
var connections = handler.connections;
16+
while (await connections.hasNext) {
17+
var connection = await connections.next;
18+
connection.sink.add('foo');
19+
connection.stream.listen(print);
20+
}
21+
}

pkgs/sse/lib/client/sse_client.dart

Lines changed: 11 additions & 3 deletions
Original file line numberDiff line numberDiff line change
@@ -1,8 +1,13 @@
1+
// Copyright (c) 2019, 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+
15
import 'dart:async';
26
import 'dart:convert';
37
import 'dart:html';
48

59
import 'package:http/browser_client.dart';
10+
import 'package:logging/logging.dart';
611
import 'package:stream_channel/stream_channel.dart';
712
import 'package:uuid/uuid.dart';
813

@@ -17,6 +22,8 @@ class SseClient extends StreamChannelMixin<String> {
1722

1823
final _client = new BrowserClient()..withCredentials = true;
1924

25+
final _logger = Logger('SseClient');
26+
2027
EventSource _eventSource;
2128

2229
String _serverUrl;
@@ -39,7 +46,8 @@ class SseClient extends StreamChannelMixin<String> {
3946

4047
/// Add messages to this [StreamSink] to send them to the server.
4148
///
42-
/// The message added to the sink has to be JSON encodable.
49+
/// The message added to the sink has to be JSON encodable. Messages that fail
50+
/// to encode will be logged through a [Logger].
4351
@override
4452
StreamSink<String> get sink => _outgoingController.sink;
4553

@@ -79,8 +87,8 @@ class SseClient extends StreamChannelMixin<String> {
7987
var encoded = jsonEncode(message);
8088
try {
8189
await _client.post(_serverUrl, body: encoded);
82-
} catch (_) {
83-
// Ignore any error
90+
} catch (e) {
91+
_logger.warning('Unable to encode outgoing message: $e');
8492
}
8593
}
8694
}

pkgs/sse/lib/server/sse_handler.dart

Lines changed: 6 additions & 2 deletions
Original file line numberDiff line numberDiff line change
@@ -1,3 +1,7 @@
1+
// Copyright (c) 2019, 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+
15
import 'dart:async';
26
import 'dart:convert';
37

@@ -26,6 +30,7 @@ class SseConnection extends StreamChannelMixin<String> {
2630
SseConnection(this._sink, this._clientId) {
2731
_outgoingController.stream.listen((data) {
2832
if (!_closeCompleter.isCompleted) {
33+
// JSON encode the message to escape new lines.
2934
_sink.add('data: ${json.encode(data)}\n');
3035
_sink.add('\n');
3136
}
@@ -34,12 +39,11 @@ class SseConnection extends StreamChannelMixin<String> {
3439

3540
Future get onClose => _closeCompleter.future;
3641

37-
///
3842
/// The message added to the sink has to be JSON encodable.
3943
@override
4044
StreamSink<String> get sink => _outgoingController.sink;
4145

42-
// Add messages to this [StreamSink] to send them to the server.
46+
// Add messages to this [StreamSink] to send them to the server.
4347
/// [Stream] of messages sent from the server to this client.
4448
///
4549
/// A message is a decoded JSON object.

pkgs/sse/pubspec.yaml

Lines changed: 1 addition & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -1,5 +1,5 @@
11
name: sse
2-
version: 0.0.1
2+
version: 1.0.0
33
author: Dart Team <[email protected]>
44
homepage: https://github.com/dart-lang/sse
55
description: >-

pkgs/sse/test/sse_test.dart

Lines changed: 6 additions & 2 deletions
Original file line numberDiff line numberDiff line change
@@ -1,3 +1,7 @@
1+
// Copyright (c) 2019, 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+
15
@TestOn('vm')
26
import 'dart:async';
37
import 'dart:io';
@@ -40,15 +44,15 @@ void main() {
4044
});
4145

4246
test('Multiple clients can connect', () async {
43-
var connections = await handler.connections;
47+
var connections = handler.connections;
4448
await webdriver.get('http://localhost:${server.port}');
4549
await connections.next;
4650
await webdriver.get('http://localhost:${server.port}');
4751
await connections.next;
4852
});
4953

5054
test('Routes data correctly', () async {
51-
var connections = await handler.connections;
55+
var connections = handler.connections;
5256
await webdriver.get('http://localhost:${server.port}');
5357
var connectionA = await connections.next;
5458
await webdriver.get('http://localhost:${server.port}');

pkgs/sse/test/web/index.dart

Lines changed: 5 additions & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -1,8 +1,12 @@
1+
// Copyright (c) 2019, 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+
15
import 'dart:html';
26

37
import 'package:sse/client/sse_client.dart';
48

5-
main() {
9+
void main() {
610
var channel = SseClient('/test');
711

812
document.querySelector('button').onClick.listen((_) {

0 commit comments

Comments
 (0)