Skip to content

Instantly share code, notes, and snippets.

@timo

timo/README.md Secret

Last active June 13, 2019 23:35
Show Gist options
  • Star 0 You must be signed in to star a gist
  • Fork 0 You must be signed in to fork a gist
  • Save timo/b5fedaf5bea40e8d97ebf257addb25d0 to your computer and use it in GitHub Desktop.
Save timo/b5fedaf5bea40e8d97ebf257addb25d0 to your computer and use it in GitHub Desktop.
Cro::WebSocket shall react to the socket disconnecting.

This works for the server side, i.e. when a client drops from the network. however, when a client is connected to some server and the server disappears, nothing happens :(

diff --git a/lib/Cro/WebSocket/Client/Connection.pm6 b/lib/Cro/WebSocket/Client/Connection.pm6
index ce3ef37..1dec51b 100644
--- a/lib/Cro/WebSocket/Client/Connection.pm6
+++ b/lib/Cro/WebSocket/Client/Connection.pm6
@@ -53,16 +53,18 @@ class Cro::WebSocket::Client::Connection {
|@after
).transformer($in.map(-> $data { Cro::TCP::Message.new(:$data) }));
+ my $instance;
+
my $pp-out = Cro.compose(
|@before,
Cro::WebSocket::MessageSerializer.new,
Cro::WebSocket::FrameSerializer.new(:mask)
- ).transformer($sender.Supply);
+ ).transformer($sender.Supply.on-close({ note "closing websocket client because the network connection closed."; $instance.close }));
- my $instance = self.bless:
- :$in, :$out, :$sender, receiver => $receiver.Supply, :$closer, :$pong, :$closed;
+ $instance = self.bless:
+ :$in, :$out, :$sender, receiver => $receiver.Supply.on-close({ note "closing websocket client receiver supply because the network connection closed"; $instance.close }), :$closer, :$pong, :$closed;
- $pp-in.tap:
+ $pp-in.on-close({ note "closing websocket client because pp-in closed."; $instance.close }).tap:
{
if .is-data {
$receiver.emit: $_;
@@ -85,7 +87,7 @@ class Cro::WebSocket::Client::Connection {
}
},
quit => { $receiver.quit($_) };
- $pp-out.tap: { $out.emit: .data }, quit => { $out.quit($_) };
+ $pp-out.on-close({ note "closing websocket client because pp-out closed"; $instance.close }).tap: { $out.emit: .data }, quit => { $out.quit($_) };
$instance;
}
diff --git a/lib/Cro/WebSocket/Handler.pm6 b/lib/Cro/WebSocket/Handler.pm6
index 3c39094..17f5aa8 100644
--- a/lib/Cro/WebSocket/Handler.pm6
+++ b/lib/Cro/WebSocket/Handler.pm6
@@ -12,19 +12,21 @@ class Cro::WebSocket::Handler does Cro::Transform {
}
method transformer(Supply:D $in) {
+ my $on-close = Promise.new if &!block.count == 2;
+ my $on-close-vow = $on-close.?vow;
+
+ sub keep-close-promise($m = Nil) {
+ with $on-close-vow {
+ $on-close-vow.keep($m);
+ $on-close-vow = Nil;
+ }
+ }
+
+
supply {
my $supplier = Supplier::Preserving.new;
- my $on-close = Promise.new if &!block.count == 2;
- my $on-close-vow = $on-close.?vow;
my $end = False;
- sub keep-close-promise($m = Nil) {
- with $on-close-vow {
- $on-close-vow.keep($m);
- $on-close-vow = Nil;
- }
- }
-
my class CloseMessage {
has $.message;
}
@@ -114,6 +116,6 @@ class Cro::WebSocket::Handler does Cro::Transform {
}
}
}
- }
+ }.on-close({ note "keep the close-promise because on-close"; keep-close-promise("Client closed the connection") });
}
}
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment