1
0
mirror of https://github.com/php/doc-fr.git synced 2026-03-24 07:02:06 +01:00
Files
archived-doc-fr/reference/event/eventhttpconnection/setclosecallback.xml
2026-02-19 16:08:49 +01:00

198 lines
5.2 KiB
XML

<?xml version="1.0" encoding="utf-8"?>
<!-- EN-Revision: 22583751fbfdaa3eaa41aeb6470d1343f5cb2c78 Maintainer: yannick Status: ready -->
<!-- Reviewed: no -->
<refentry xml:id="eventhttpconnection.setclosecallback" xmlns="http://docbook.org/ns/docbook" xmlns:xlink="http://www.w3.org/1999/xlink">
<refnamediv>
<refname>EventHttpConnection::setCloseCallback</refname>
<refpurpose>Définit une fonction de rappel lors de la fermeture de la connexion</refpurpose>
</refnamediv>
<refsect1 role="description">
&reftitle.description;
<methodsynopsis>
<modifier>public</modifier>
<type>void</type>
<methodname>EventHttpConnection::setCloseCallback</methodname>
<methodparam>
<type>callable</type>
<parameter>callback</parameter>
</methodparam>
<methodparam choice="opt">
<type>mixed</type>
<parameter>data</parameter>
</methodparam>
</methodsynopsis>
<para>
Définit une fonction de rappel lors de la fermeture de la connexion.
</para>
</refsect1>
<refsect1 role="parameters">
&reftitle.parameters;
<variablelist>
<varlistentry>
<term>
<parameter>callback</parameter>
</term>
<listitem>
<para>
Fonction de rappel à appeler lors de la fermeture de la connexion.
Doit correspondre au prototype suivant :
</para>
<methodsynopsis>
<type>void</type>
<methodname>callback</methodname>
<methodparam
choice="opt">
<type>EventHttpConnection</type>
<parameter>conn</parameter>
<initializer>&null;</initializer>
</methodparam>
<methodparam
choice="opt">
<type>mixed</type>
<parameter>arg</parameter>
<initializer>&null;</initializer>
</methodparam>
</methodsynopsis>
</listitem>
</varlistentry>
</variablelist>
</refsect1>
<refsect1 role="returnvalues">
&reftitle.returnvalues;
<para>
&return.void;
</para>
</refsect1>
<refsect1 role="examples">
&reftitle.examples;
<example>
<title>Exemple avec <methodname>EventHttpConnection::setCloseCallback</methodname></title>
<programlisting role="php">
<![CDATA[
<?php
/*
* Setting up close-connection callback
*
* The script handles closed connections using HTTP API.
*
* Usage:
* 1) Launch the server:
* $ php examples/http_closecb.php 4242
*
* 2) Launch a client in another terminal. Telnet-like
* session should look like the following:
*
* $ nc -t 127.0.0.1 4242
* GET / HTTP/1.0
* Connection: close
*
* The server will output something similar to the following:
*
* HTTP/1.0 200 OK
* Content-Type: multipart/x-mixed-replace;boundary=boundarydonotcross
* Connection: close
*
* <html>
*
* 3) Terminate the client connection abruptly,
* i.e. kill the process, or just press Ctrl-C.
*
* 4) Check if the server called _close_callback.
* The script should output "_close_callback" string to standard output.
*
* 5) Check if the server's process has no orphaned connections,
* e.g. with `lsof` utility.
*/
function _close_callback($conn)
{
echo __FUNCTION__, PHP_EOL;
}
function _http_default($req, $dummy)
{
$conn = $req->getConnection();
$conn->setCloseCallback('_close_callback', NULL);
/*
By enabling Event::READ we protect the server against unclosed conections.
This is a peculiarity of Libevent. The library disables Event::READ events
on this connection, and the server is not notified about terminated
connections.
So each time client terminates connection abruptly, we get an orphaned
connection. For instance, the following is a part of `lsof -p $PID | grep TCP`
command after client has terminated connection:
57-php 15057 ruslan 6u unix 0xffff8802fb59c780 0t0 125187 socket
58:php 15057 ruslan 7u IPv4 125189 0t0 TCP *:4242 (LISTEN)
59:php 15057 ruslan 8u IPv4 124342 0t0 TCP localhost:4242->localhost:37375 (CLOSE_WAIT)
where $PID is our process ID.
The following block of code fixes such kind of orphaned connections.
*/
$bev = $req->getBufferEvent();
$bev->enable(Event::READ);
// Nous devons le libérer explicitement. Voir EventHttpRequest::getConnection
$bev->free(); // Nous devons le libérer explicitement
$req->addHeader(
'Content-Type',
'multipart/x-mixed-replace;boundary=boundarydonotcross',
EventHttpRequest::OUTPUT_HEADER
);
$buf = new EventBuffer();
$buf->add('<html>');
$req->sendReply(200, "OK");
$req->sendReplyChunk($buf);
}
$port = 4242;
if ($argc > 1) {
$port = (int) $argv[1];
}
if ($port <= 0 || $port > 65535) {
exit("Invalid port");
}
$base = new EventBase();
$http = new EventHttp($base);
$http->setDefaultCallback("_http_default", NULL);
$http->bind("0.0.0.0", $port);
$base->loop();
?>
]]>
</programlisting>
</example>
</refsect1>
</refentry>
<!-- Keep this comment at the end of the file
Local variables:
mode: sgml
sgml-omittag:t
sgml-shorttag:t
sgml-minimize-attributes:nil
sgml-always-quote-attributes:t
sgml-indent-step:1
sgml-indent-data:t
indent-tabs-mode:nil
sgml-parent-document:nil
sgml-default-dtd-file:"~/.phpdoc/manual.ced"
sgml-exposed-tags:nil
sgml-local-catalogs:nil
sgml-local-ecat-files:nil
End:
vim600: syn=xml fen fdm=syntax fdl=2 si
vim: et tw=78 syn=sgml
vi: ts=1 sw=1
-->