Skip to content

Commit

Permalink
Add pingInterval pass through param (#16)
Browse files Browse the repository at this point in the history
Configures the construction of the WebSocketChannel.
  • Loading branch information
emeyex authored and natebosch committed Apr 17, 2019
1 parent 8f2ad43 commit 141f577
Show file tree
Hide file tree
Showing 4 changed files with 23 additions and 5 deletions.
5 changes: 5 additions & 0 deletions CHANGELOG.md
Original file line number Diff line number Diff line change
@@ -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
Expand Down
12 changes: 10 additions & 2 deletions lib/shelf_web_socket.dart
Original file line number Diff line number Diff line change
Expand Up @@ -37,8 +37,14 @@ typedef _BinaryFunction = void Function(Null, Null);
/// 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 =
Expand All @@ -55,5 +61,7 @@ Handler webSocketHandler(Function onConnection,
onConnection = (webSocket, _) => innerOnConnection(webSocket);
}

return new WebSocketHandler(onConnection, protocols, allowedOrigins).handle;
return new WebSocketHandler(
onConnection, protocols, allowedOrigins, pingInterval)
.handle;
}
9 changes: 7 additions & 2 deletions lib/src/web_socket_handler.dart
Original file line number Diff line number Diff line change
Expand Up @@ -18,7 +18,11 @@ class WebSocketHandler {
/// 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) {
Expand Down Expand Up @@ -74,7 +78,8 @@ class WebSocketHandler {
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
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: shelf_web_socket
version: 0.2.2+5
version: 0.2.3

description: >-
A shelf handler that wires up a listener for every connection.
Expand Down

0 comments on commit 141f577

Please sign in to comment.