Add `pingInterval` pass through param (#16)
Configures the construction of the WebSocketChannel.
diff --git a/CHANGELOG.md b/CHANGELOG.md
index 3397968..41b0d5f 100644
--- a/CHANGELOG.md
+++ b/CHANGELOG.md
@@ -1,3 +1,8 @@
+## 0.2.3
+
+* Add `pingInterval` argument to `webSocketHandler`, to be passed through
+ to the created channel.
+
## 0.2.2+5
* Allow `stream_channel` version 2.x
diff --git a/lib/shelf_web_socket.dart b/lib/shelf_web_socket.dart
index bdfa187..7ce32ee 100644
--- a/lib/shelf_web_socket.dart
+++ b/lib/shelf_web_socket.dart
@@ -37,8 +37,14 @@
/// See also the WebSocket spec's discussion of [origin considerations][].
///
/// [origin considerations]: https://tools.ietf.org/html/rfc6455#section-10.2
+///
+/// If [pingInterval] is specified, it will get passed to the created
+/// channel instance, enabling round-trip disconnect detection.
+/// See [WebSocketChannel] for more details.
Handler webSocketHandler(Function onConnection,
- {Iterable<String> protocols, Iterable<String> allowedOrigins}) {
+ {Iterable<String> protocols,
+ Iterable<String> allowedOrigins,
+ Duration pingInterval}) {
if (protocols != null) protocols = protocols.toSet();
if (allowedOrigins != null) {
allowedOrigins =
@@ -55,5 +61,7 @@
onConnection = (webSocket, _) => innerOnConnection(webSocket);
}
- return new WebSocketHandler(onConnection, protocols, allowedOrigins).handle;
+ return new WebSocketHandler(
+ onConnection, protocols, allowedOrigins, pingInterval)
+ .handle;
}
diff --git a/lib/src/web_socket_handler.dart b/lib/src/web_socket_handler.dart
index f1acc9f..90b66e0 100644
--- a/lib/src/web_socket_handler.dart
+++ b/lib/src/web_socket_handler.dart
@@ -18,7 +18,11 @@
/// The set of allowed browser origin connections, or `null`..
final Set<String> _allowedOrigins;
- WebSocketHandler(this._onConnection, this._protocols, this._allowedOrigins);
+ /// The ping interval used for verifying connection, or `null`.
+ final Duration _pingInterval;
+
+ WebSocketHandler(this._onConnection, this._protocols, this._allowedOrigins,
+ this._pingInterval);
/// The [Handler].
Response handle(Request request) {
@@ -74,7 +78,8 @@
if (protocol != null) sink.add("Sec-WebSocket-Protocol: $protocol\r\n");
sink.add("\r\n");
- _onConnection(new WebSocketChannel(channel), protocol);
+ _onConnection(
+ new WebSocketChannel(channel, pingInterval: _pingInterval), protocol);
});
// [request.hijack] is guaranteed to throw a [HijackException], so we'll
diff --git a/pubspec.yaml b/pubspec.yaml
index 310bb84..03dd31a 100644
--- a/pubspec.yaml
+++ b/pubspec.yaml
@@ -1,5 +1,5 @@
name: shelf_web_socket
-version: 0.2.2+5
+version: 0.2.3
description: >-
A shelf handler that wires up a listener for every connection.