Skip to content
This repository was archived by the owner on Jan 6, 2025. It is now read-only.

Cleanup #2

Merged
merged 4 commits into from
Feb 15, 2019
Merged
Show file tree
Hide file tree
Changes from all commits
Commits
File filter

Filter by extension

Filter by extension


Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
5 changes: 5 additions & 0 deletions .travis.yml
Original file line number Diff line number Diff line change
Expand Up @@ -40,3 +40,8 @@ script:
cache:
directories:
- $HOME/.pub-cache

# Only building master means that we don't run two builds for each pull request.
branches:
only:
- master
8 changes: 8 additions & 0 deletions CHANGELOG.md
Original file line number Diff line number Diff line change
@@ -0,0 +1,8 @@
## 1.0.0

- Internal cleanup.


## 0.0.1

- Initial commit.
15 changes: 15 additions & 0 deletions example/index.dart
Original file line number Diff line number Diff line change
@@ -0,0 +1,15 @@
// Copyright (c) 2019, the Dart project authors. Please see the AUTHORS file
// for details. All rights reserved. Use of this source code is governed by a
// BSD-style license that can be found in the LICENSE file.

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

/// A basic example which should be used in a browser that supports SSE.
void main() {
var channel = SseClient('/sseHandler');

channel.stream.listen((s) {
// Listen for messages and send the back.
channel.sink.add(s);
});
}
21 changes: 21 additions & 0 deletions example/server.dart
Original file line number Diff line number Diff line change
@@ -0,0 +1,21 @@
// Copyright (c) 2019, the Dart project authors. Please see the AUTHORS file
// for details. All rights reserved. Use of this source code is governed by a
// BSD-style license that can be found in the LICENSE file.

import 'package:shelf/shelf_io.dart' as io;
import 'package:sse/server/sse_handler.dart';

/// A basic server which sets up an SSE handler.
///
/// When a client connnects it will send a simple message and print the
/// response.
void main() async {
var handler = SseHandler(Uri.parse('/sseHandler'));
await io.serve(handler.handler, 'localhost', 0);
var connections = handler.connections;
while (await connections.hasNext) {
var connection = await connections.next;
connection.sink.add('foo');
connection.stream.listen(print);
}
}
14 changes: 11 additions & 3 deletions lib/client/sse_client.dart
Original file line number Diff line number Diff line change
@@ -1,8 +1,13 @@
// Copyright (c) 2019, the Dart project authors. Please see the AUTHORS file
// for details. All rights reserved. Use of this source code is governed by a
// BSD-style license that can be found in the LICENSE file.

import 'dart:async';
import 'dart:convert';
import 'dart:html';

import 'package:http/browser_client.dart';
import 'package:logging/logging.dart';
import 'package:stream_channel/stream_channel.dart';
import 'package:uuid/uuid.dart';

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

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

final _logger = Logger('SseClient');

EventSource _eventSource;

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

/// Add messages to this [StreamSink] to send them to the server.
///
/// The message added to the sink has to be JSON encodable.
/// The message added to the sink has to be JSON encodable. Messages that fail
/// to encode will be logged through a [Logger].
@override
StreamSink<String> get sink => _outgoingController.sink;

Expand Down Expand Up @@ -79,8 +87,8 @@ class SseClient extends StreamChannelMixin<String> {
var encoded = jsonEncode(message);
try {
await _client.post(_serverUrl, body: encoded);
} catch (_) {
// Ignore any error
} catch (e) {
_logger.warning('Unable to encode outgoing message: $e');
Copy link
Contributor

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

Note that this won't actually log anything anywhere by default... not necessarily an issue in itself but it might be worth documenting somewhere that you have to set up a log handler to see logs?

Copy link
Contributor Author

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

Added a corresponding doc comment.

}
}
}
8 changes: 6 additions & 2 deletions lib/server/sse_handler.dart
Original file line number Diff line number Diff line change
@@ -1,3 +1,7 @@
// Copyright (c) 2019, the Dart project authors. Please see the AUTHORS file
// for details. All rights reserved. Use of this source code is governed by a
// BSD-style license that can be found in the LICENSE file.

import 'dart:async';
import 'dart:convert';

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

Future get onClose => _closeCompleter.future;

///
/// The message added to the sink has to be JSON encodable.
@override
StreamSink<String> get sink => _outgoingController.sink;

// Add messages to this [StreamSink] to send them to the server.
// Add messages to this [StreamSink] to send them to the server.
/// [Stream] of messages sent from the server to this client.
///
/// A message is a decoded JSON object.
Expand Down
2 changes: 1 addition & 1 deletion pubspec.yaml
Original file line number Diff line number Diff line change
@@ -1,5 +1,5 @@
name: sse
version: 0.0.1
version: 1.0.0
author: Dart Team <[email protected]>
homepage: https://github.com/dart-lang/sse
description: >-
Expand Down
8 changes: 6 additions & 2 deletions test/sse_test.dart
Original file line number Diff line number Diff line change
@@ -1,3 +1,7 @@
// Copyright (c) 2019, the Dart project authors. Please see the AUTHORS file
// for details. All rights reserved. Use of this source code is governed by a
// BSD-style license that can be found in the LICENSE file.

@TestOn('vm')
import 'dart:async';
import 'dart:io';
Expand Down Expand Up @@ -40,15 +44,15 @@ void main() {
});

test('Multiple clients can connect', () async {
var connections = await handler.connections;
var connections = handler.connections;
await webdriver.get('http://localhost:${server.port}');
await connections.next;
await webdriver.get('http://localhost:${server.port}');
await connections.next;
});

test('Routes data correctly', () async {
var connections = await handler.connections;
var connections = handler.connections;
await webdriver.get('http://localhost:${server.port}');
var connectionA = await connections.next;
await webdriver.get('http://localhost:${server.port}');
Expand Down
6 changes: 5 additions & 1 deletion test/web/index.dart
Original file line number Diff line number Diff line change
@@ -1,8 +1,12 @@
// Copyright (c) 2019, the Dart project authors. Please see the AUTHORS file
// for details. All rights reserved. Use of this source code is governed by a
// BSD-style license that can be found in the LICENSE file.

import 'dart:html';

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

main() {
void main() {
var channel = SseClient('/test');

document.querySelector('button').onClick.listen((_) {
Expand Down
Loading