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

Fix keepAlive reconnection #23

Merged
merged 1 commit into from
Mar 2, 2020
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
4 changes: 4 additions & 0 deletions CHANGELOG.md
Original file line number Diff line number Diff line change
@@ -1,3 +1,7 @@
## 3.2.1

- Fix an issue where `keepAlive` would only allow a single reconnection.

## 3.2.0

- Re-expose `isInKeepAlivePeriod` flag on `SseConnection`. This flag will be
Expand Down
15 changes: 7 additions & 8 deletions lib/src/server/sse_handler.dart
Original file line number Diff line number Diff line change
Expand Up @@ -163,16 +163,15 @@ class SseHandler {
unawaited(connection._closedCompleter.future.then((_) {
_connections.remove(clientId);
}));
// Remove connection when it is remotely closed or the stream is
// cancelled.
channel.stream.listen((_) {
// SSE is unidirectional. Responses are handled through POST requests.
}, onDone: () {
connection._handleDisconnect();
});

_connectionController.add(connection);
}
// Remove connection when it is remotely closed or the stream is
// cancelled.
channel.stream.listen((_) {
// SSE is unidirectional. Responses are handled through POST requests.
}, onDone: () {
_connections[clientId]?._handleDisconnect();
});
});
return shelf.Response.notFound('');
}
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: 3.2.0
version: 3.2.1
homepage: https://github.com/dart-lang/sse
description: >-
Provides client and server functionality for setting up bi-directional
Expand Down
16 changes: 15 additions & 1 deletion test/sse_test.dart
Original file line number Diff line number Diff line change
Expand Up @@ -6,6 +6,7 @@
import 'dart:async';
import 'dart:io';

import 'package:async/async.dart';
import 'package:shelf/shelf.dart' as shelf;
import 'package:shelf/shelf_io.dart' as io;
import 'package:shelf_static/shelf_static.dart';
Expand Down Expand Up @@ -195,7 +196,20 @@ void main() {
// Ensure we can still round-trip data on the original connection and that
// the connection is no longer marked keep-alive once it's reconnected.
connection.sink.add('bar');
expect(await connection.stream.first, 'bar');
var queue = StreamQueue(connection.stream);
expect(await queue.next, 'bar');
expect(connection.isInKeepAlivePeriod, isFalse);

// Now check that we can reconnect multiple times.
closeSink(connection);
maxPumps = 50;
while (!connection.isInKeepAlivePeriod && maxPumps-- > 0) {
await pumpEventQueue(times: 1);
}
expect(connection.isInKeepAlivePeriod, isTrue);
expect(handler.numberOfClients, 1);
connection.sink.add('bar');
expect(await queue.next, 'bar');
expect(connection.isInKeepAlivePeriod, isFalse);
});

Expand Down