This article is in need of a technical review.
Non-standard
This feature is not on a current W3C standards track, but it is supported on the Firefox OS platform. Although implementations may change in the future and it is not supported widely across browsers, it is suitable for use in code dedicated to Firefox OS apps.
This API is available on Firefox OS for privileged or certified applications only.
Summary
Provides access to a raw TCP socket API in JavaScript.
The main entry point for this API is the navigator.mozTCPSocket property which is a TCPSocket object.
Note: Only certified apps can accept incoming connections on a port below 1024.
API overview
interface TCPSocket{
readonly attribute DOMString host;
readonly attribute unsigned short port;
readonly attribute boolean ssl;
readonly attribute unsigned long bufferedAmount;
readonly attribute DOMString binaryType;
readonly attribute DOMString readyState;
TCPSocket open(DOMString host, unsigned short port, [object options]);
TCPServerSocket listen(unsigned short port, [object options, [unsigned short backlog]])
void upgradeToSecure();
void suspend();
void resume();
void close();
boolean send(in jsval data);
attribute onopen;
attribute ondrain;
attribute ondata;
attribute onerror;
attribute onclose;
};
Properties
-
TCPSocket.hostRead only - A string representing the host name of the server the socket is connected to.
-
TCPSocket.portRead only - A number representing the port the socket is connected to.
-
TCPSocket.sslRead only -
A boolean indicating whether the socket is encrypted with SSL (
true) or not (false). -
TCPSocket.bufferedAmountRead only - The number of bytes of not-yet-sent data in the socket buffered.
-
TCPSocket.binaryTypeRead only -
The type of data used. Possible value is
arraybuffer, orstring. -
TCPSocket.readyStateRead only -
The socket state. Possible value is
connecting,open,closing, orclosed.
Events handler
-
TCPSocket.onopen -
A handler for the
openevent. After this event, the socket is ready to send and receive data. -
TCPSocket.ondrain -
A handler for the
drainevent. This event is triggered each time the buffer of data is flushed. -
TCPSocket.onerror -
A handler for the
errorevent. -
TCPSocket.ondata -
A handler for the
dataevent. This event is triggered each time data has been received. -
TCPSocket.onclose -
A handler for the
closeevent.
Methods
-
TCPSocket.close() - Closes the connection.
-
TCPSocket.open(host, port [, options]) -
Returns a new
TCPSocketobject connected to the givenhostat the givenport. -
TCPSocket.listen(port [, options [, backlog]])Requires FirefoxOS 1.2 -
Returns a new
TCPServerSocketobject listening the givenport. -
TCPSocket.resume() -
Resumes the
dataevents. -
TCPSocket.send(data) - Buffers data to be sent across the network.
-
TCPSocket.suspend() -
Pauses the
dataevents. -
TCPSocket.upgradeToSecure()Requires FirefoxOS 1.2 - Enable secure on chanel.
Specification
Not part of any specification yet; however, this API is discussed at W3C as part of the System Applications Working Group under the TCP and UDP Socket API (formerly known as the Raw Sockets API) proposal.