TCPSocket.send

  • Revision slug: Web/API/TCPSocket.send
  • Revision title: TCPSocket.send
  • Revision id: 429463
  • Created:
  • Creator: Jeremie
  • Is current revision? No
  • Comment

Revision Content

{{ non-standard_header() }}

{{ B2GOnlyHeader2('privileged') }}

Summary

The send method is used to buffer data to be send to the server.

Syntax

var theBufferIsFull = instanceOfTCPSocket.send(data[, byteOffset[, byteLength]]);

Parameters

data
The data to write to the socket. Depending on the binaryType option pass to the {{domxref("TCPSocket.open","open()")}} method, it will be a string or an ArrayBuffer.
byteOffset
The offset within the data from which to begin writing. Only relevant for ArrayBuffer data.
byteLength
The number of bytes to write. Only relevant for ArrayBuffer data.

Returns

It returns a boolean as a hint to the caller that they may either continue sending more data immediately, or may want to wait until the other side has read some of the data which has already been written to the socket before buffering more.

  • If send returns true, then less than 64k has been buffered and it's safe to immediately write more.
  • If send returns false, then more than 64k has been buffered, and the caller may wish to wait until the {{event("drain")}} event has been triggered before buffering more data by more calls to send.

Example

var socket = navigator.mozTCPSocket.open("www.mozilla.org", 80);

function pushData() {
  var data;
  // Do stuff that retrieve data

  // Fill the buffer as much as you can
  while(data != null && socket.send(data));
}

// Resume pushing data when the buffer is flushed
socket.ondrain = pushData;

// Start pushing data
pushData();

Specification

Not part of any specification yet; however, this API is discussed at W3C as part of the System Applications Working Group under the Raw Sockets proposal.

See also

Revision Source

<p>{{ non-standard_header() }}</p>
<p>{{ B2GOnlyHeader2('privileged') }}</p>
<h2 id="Summary">Summary</h2>
<p>The <code>send</code> method is used to buffer data to be send to the server.</p>
<h2 id="Syntax">Syntax</h2>
<pre>
var theBufferIsFull = <em>instanceOfTCPSocket</em>.send(<em>data</em>[, <em>byteOffset</em>[, <em>byteLength</em>]]);</pre>
<h3 id="Parameters">Parameters</h3>
<dl>
  <dt>
    <code>data</code></dt>
  <dd>
    The data to write to the socket. Depending on the <code>binaryType</code> option pass to the {{domxref("TCPSocket.open","open()")}} method, it will be a string or an <a href="/en-US/docs/Web/JavaScript/Typed_arrays/ArrayBuffer" title="/en-US/docs/Web/JavaScript/Typed_arrays/ArrayBuffer">ArrayBuffer</a>.</dd>
  <dt>
    <code>byteOffset</code></dt>
  <dd>
    The offset within the data from which to begin writing. Only relevant for <a href="/en-US/docs/Web/JavaScript/Typed_arrays/ArrayBuffer" title="/en-US/docs/Web/JavaScript/Typed_arrays/ArrayBuffer">ArrayBuffer</a> data.</dd>
  <dt>
    <code>byteLength</code></dt>
  <dd>
    The number of bytes to write. Only relevant for <a href="/en-US/docs/Web/JavaScript/Typed_arrays/ArrayBuffer" title="/en-US/docs/Web/JavaScript/Typed_arrays/ArrayBuffer">ArrayBuffer</a> data.</dd>
</dl>
<h3 id="Returns">Returns</h3>
<p>It returns a boolean as a hint to the caller that they may either continue sending more data immediately, or may want to wait until the other side has read some of the data which has already been written to the socket before buffering more.</p>
<ul>
  <li>If send returns <code>true</code>, then less than 64k has been buffered and it's safe to immediately write more.</li>
  <li>If send returns <code>false</code>, then more than 64k has been buffered, and the caller may wish to wait until the {{event("drain")}} event has been triggered before buffering more data by more calls to <code>send</code>.</li>
</ul>
<h2 id="Example">Example</h2>
<pre class="brush: js">
<code class="language-js"><span class="token keyword">var</span> socket <span class="token operator">=</span> navigator<span class="token punctuation">.</span>mozTCPSocket.open("www.mozilla.org", 80)<span class="token punctuation">;</span></code>

function pushData() {
  var data;
  // Do stuff that retrieve data

  // Fill the buffer as much as you can
  while(data != null &amp;&amp; socket.send(data));
}

// Resume pushing data when the buffer is flushed
socket.ondrain = pushData;

// Start pushing data
pushData();
</pre>
<h2 id="Specification">Specification</h2>
<p>Not part of any specification yet; however, this API is discussed at W3C as part of the <a class="external" href="http://www.w3.org/2012/sysapps/" rel="external" title="http://www.w3.org/2012/sysapps/">System Applications Working Group</a> under the <a href="http://www.w3.org/2012/sysapps/raw-sockets/" title="http://www.w3.org/2012/sysapps/raw-sockets/">Raw Sockets</a> proposal.</p>
<h2 id="See_also">See also</h2>
<ul>
  <li>{{domxref("TCPSocket")}}</li>
  <li><a href="/en-US/docs/WebAPI/TCP_Socket" title="/en-US/docs/WebAPI/TCP_Socket">TCP Socket</a></li>
</ul>
Revert to this revision