note
kikuchiyo
<p>The problem is that the <code> my $handle</code> variable is private to the callback sub, and is not referenced anywhere else. So when that sub returns (immediately after push_writing your "Hello" message), it will be destroyed, and with it, your connection.</p>
<p>A solution is including a reference to it in one of AE::Handle's callbacks, e.g.:</p>
<code>
on_error => sub {
my ($hdl, $fatal, $msg) = @_;
AE::log error => $msg;
$hdl->destroy();
undef $handle; # <------
},
</code>
<p>This way, the callback sub closes over the variable, so its reference count will be nonzero when the sub returns and thus your connection will stay alive.</p>
11115014
11115014