Skip to content

Commit 141f577

Browse files
emeyexnatebosch
authored andcommitted
Add pingInterval pass through param (flutter#16)
Configures the construction of the WebSocketChannel.
1 parent 8f2ad43 commit 141f577

File tree

4 files changed

+23
-5
lines changed

4 files changed

+23
-5
lines changed

CHANGELOG.md

+5
Original file line numberDiff line numberDiff line change
@@ -1,3 +1,8 @@
1+
## 0.2.3
2+
3+
* Add `pingInterval` argument to `webSocketHandler`, to be passed through
4+
to the created channel.
5+
16
## 0.2.2+5
27

38
* Allow `stream_channel` version 2.x

lib/shelf_web_socket.dart

+10-2
Original file line numberDiff line numberDiff line change
@@ -37,8 +37,14 @@ typedef _BinaryFunction = void Function(Null, Null);
3737
/// See also the WebSocket spec's discussion of [origin considerations][].
3838
///
3939
/// [origin considerations]: https://tools.ietf.org/html/rfc6455#section-10.2
40+
///
41+
/// If [pingInterval] is specified, it will get passed to the created
42+
/// channel instance, enabling round-trip disconnect detection.
43+
/// See [WebSocketChannel] for more details.
4044
Handler webSocketHandler(Function onConnection,
41-
{Iterable<String> protocols, Iterable<String> allowedOrigins}) {
45+
{Iterable<String> protocols,
46+
Iterable<String> allowedOrigins,
47+
Duration pingInterval}) {
4248
if (protocols != null) protocols = protocols.toSet();
4349
if (allowedOrigins != null) {
4450
allowedOrigins =
@@ -55,5 +61,7 @@ Handler webSocketHandler(Function onConnection,
5561
onConnection = (webSocket, _) => innerOnConnection(webSocket);
5662
}
5763

58-
return new WebSocketHandler(onConnection, protocols, allowedOrigins).handle;
64+
return new WebSocketHandler(
65+
onConnection, protocols, allowedOrigins, pingInterval)
66+
.handle;
5967
}

lib/src/web_socket_handler.dart

+7-2
Original file line numberDiff line numberDiff line change
@@ -18,7 +18,11 @@ class WebSocketHandler {
1818
/// The set of allowed browser origin connections, or `null`..
1919
final Set<String> _allowedOrigins;
2020

21-
WebSocketHandler(this._onConnection, this._protocols, this._allowedOrigins);
21+
/// The ping interval used for verifying connection, or `null`.
22+
final Duration _pingInterval;
23+
24+
WebSocketHandler(this._onConnection, this._protocols, this._allowedOrigins,
25+
this._pingInterval);
2226

2327
/// The [Handler].
2428
Response handle(Request request) {
@@ -74,7 +78,8 @@ class WebSocketHandler {
7478
if (protocol != null) sink.add("Sec-WebSocket-Protocol: $protocol\r\n");
7579
sink.add("\r\n");
7680

77-
_onConnection(new WebSocketChannel(channel), protocol);
81+
_onConnection(
82+
new WebSocketChannel(channel, pingInterval: _pingInterval), protocol);
7883
});
7984

8085
// [request.hijack] is guaranteed to throw a [HijackException], so we'll

pubspec.yaml

+1-1
Original file line numberDiff line numberDiff line change
@@ -1,5 +1,5 @@
11
name: shelf_web_socket
2-
version: 0.2.2+5
2+
version: 0.2.3
33

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

0 commit comments

Comments
 (0)