<?php
include_once $_SERVER['DOCUMENT_ROOT'] . '/include/shared-manual.inc';
$TOC = array();
$TOC_DEPRECATED = array();
$PARENTS = array();
include_once dirname(__FILE__) ."/toc/class.swoole-event.inc";
$setup = array (
  'home' => 
  array (
    0 => 'index.php',
    1 => 'PHP Manual',
  ),
  'head' => 
  array (
    0 => 'UTF-8',
    1 => 'zh',
  ),
  'this' => 
  array (
    0 => 'swoole-event.write.php',
    1 => 'Swoole\\Event::write',
    2 => 'Write data to socket asynchronously',
  ),
  'up' => 
  array (
    0 => 'class.swoole-event.php',
    1 => 'Swoole\\Event',
  ),
  'prev' => 
  array (
    0 => 'swoole-event.wait.php',
    1 => 'Swoole\\Event::wait',
  ),
  'next' => 
  array (
    0 => 'class.swoole-exception.php',
    1 => 'Swoole\\Exception',
  ),
  'alternatives' => 
  array (
  ),
  'source' => 
  array (
    'lang' => 'en',
    'path' => 'reference/swoole/swoole/event/write.xml',
  ),
  'history' => 
  array (
  ),
);
$setup["toc"] = $TOC;
$setup["toc_deprecated"] = $TOC_DEPRECATED;
$setup["parents"] = $PARENTS;
manual_setup($setup);

contributors($setup);

?>
<div id="swoole-event.write" class="refentry">
 <div class="refnamediv">
  <h1 class="refname">Swoole\Event::write</h1>
  <p class="verinfo">(PECL swoole &gt;= 1.9.0)</p><p class="refpurpose"><span class="refname">Swoole\Event::write</span> &mdash; <span class="dc-title">Write data to socket asynchronously</span></p>

 </div>

 <div class="refsect1 description" id="refsect1-swoole-event.write-description">
  <h3 class="title">说明</h3>
  <div class="methodsynopsis dc-description">
   <span class="modifier">public</span> <span class="modifier">static</span> <span class="modifier">function</span> <span class="methodname"><strong>Swoole\Event::write</strong></span>(<span class="methodparam"><span class="type"><a href="language.types.mixed.php" class="type mixed">mixed</a></span> <code class="parameter">$fd</code></span>, <span class="methodparam"><span class="type"><a href="language.types.mixed.php" class="type mixed">mixed</a></span> <code class="parameter">$data</code></span>): <span class="type"><a href="language.types.boolean.php" class="type bool">bool</a></span></div>

  <p class="simpara">
   Makes data sending asynchronous for stream/sockets resources.
  </p>
  <blockquote class="note"><p><strong class="note">注意</strong>: 
   <p class="simpara">
    If data is continuously written to a socket but the peer cannot read fast enough, the socket buffer
    will eventually fill up. In this case, the Swoole underlying layer will store the excess data
    in an in-memory buffer and wait for the writable event to trigger before attempting to write
    to the socket again. However, if the in-memory buffer also becomes full, Swoole will throw a
    &quot;pipe buffer overflow, reactor will block&quot; error and switch to blocking mode, pausing further
    writes until space becomes available.
   </p>
  </p></blockquote>
  <blockquote class="note"><p><strong class="note">注意</strong>: 
   <p class="simpara">
    The buffer-full return of false is an atomic operation—it guarantees
    either complete success (all data written) or total failure (nothing written).
   </p>
  </p></blockquote>
  <div class="warning"><strong class="warning">警告</strong>
   <p class="simpara">
    Event::write cannot be used with SSL/TLS-encrypted streams or sockets (tunneled connections).
   </p>
  </div>
  <div class="warning"><strong class="warning">警告</strong>
   <p class="simpara">
    Upon successful execution, Event::write will automatically switch the $socket to non-blocking mode.
   </p>
  </div>
 </div>


 <div class="refsect1 parameters" id="refsect1-swoole-event.write-parameters">
  <h3 class="title">参数</h3>
  <dl>
   
    <dt><code class="parameter">fd</code></dt>
    <dd>
     <p class="simpara">
      File descriptor (stream/socket resource, integer, or object).
     </p>
    </dd>
   
   
    <dt><code class="parameter">data</code></dt>
    <dd>
     <p class="simpara">
      Data to send (length must not exceed socket buffer size).
     </p>
    </dd>
   
  </dl>
 </div>


 <div class="refsect1 returnvalues" id="refsect1-swoole-event.write-returnvalues">
  <h3 class="title">返回值</h3>
  <p class="simpara">
   成功时返回 <strong><code><a href="reserved.constants.php#constant.true">true</a></code></strong>， 或者在失败时返回 <strong><code><a href="reserved.constants.php#constant.false">false</a></code></strong>。
  </p>
 </div>


 <div class="refsect1 examples" id="refsect1-swoole-event.write-examples">
  <h3 class="title">示例</h3>
  <p class="para">
   <div class="example" id="example-1">
    <p><strong>示例 #1 <span class="function"><strong>Swoole\Event::write()</strong></span> example</strong></p>
    <div class="example-contents">
     <div class="phpcode"><pre><code style="color: #000000"><span style="color: #0000BB">&lt;?php
</span><span style="color: #007700">use </span><span style="color: #0000BB">Swoole\Event</span><span style="color: #007700">;

</span><span style="color: #0000BB">$fp </span><span style="color: #007700">= </span><span style="color: #0000BB">stream_socket_client</span><span style="color: #007700">(</span><span style="color: #DD0000">'tcp://127.0.0.1:9501'</span><span style="color: #007700">);
</span><span style="color: #0000BB">$data </span><span style="color: #007700">= </span><span style="color: #0000BB">str_repeat</span><span style="color: #007700">(</span><span style="color: #DD0000">'A'</span><span style="color: #007700">, </span><span style="color: #0000BB">1024 </span><span style="color: #007700">* </span><span style="color: #0000BB">1024</span><span style="color: #007700">*</span><span style="color: #0000BB">2</span><span style="color: #007700">);
</span><span style="color: #0000BB">Event</span><span style="color: #007700">::</span><span style="color: #0000BB">add</span><span style="color: #007700">(</span><span style="color: #0000BB">$fp</span><span style="color: #007700">, function(</span><span style="color: #0000BB">$fp</span><span style="color: #007700">) {
     echo </span><span style="color: #0000BB">fread</span><span style="color: #007700">(</span><span style="color: #0000BB">$fp</span><span style="color: #007700">);
});
</span><span style="color: #0000BB">Event</span><span style="color: #007700">::</span><span style="color: #0000BB">write</span><span style="color: #007700">(</span><span style="color: #0000BB">$fp</span><span style="color: #007700">, </span><span style="color: #0000BB">$data</span><span style="color: #007700">);
</span><span style="color: #0000BB">?&gt;</span></code></pre></div>
    </div>

   </div>
  </p>
 </div>

</div><?php manual_footer($setup); ?>