To enable Web applications to maintain bidirectional
communications with client-side processes, this specification
introduces the WebSocketServer
interface.
Implementations of the APIs defined in this specification MUST implement them in a manner consistent with the ECMAScript Bindings defined in the Web IDL specification [[!WEBIDL]], as this specification uses that specification and terminology.
This specification relies on several other underlying specifications.
Many fundamental concepts from HTML5 are used by this specification. [[!HTML5]]
The IDL blocks in this specification use the semantics of the WebIDL specification. [[!WEBIDL]]
The WebSocketServer interface is based on the client WebSocket one [[!WEBSOCKETS-API]]
The multiple incomming connection on the Web socket server are managed by the port mechanism defined in the HTML5 Web Messaging specification [[!POSTMSG]]
The EventHandler interface represents a callback used for event handlers as defined in [[!HTML5]].
The concepts queue a task and fire a simple event are defined in [[!HTML5]].
The terms event handler and event handler event types are defined in [[!HTML5]].
The task source for all tasks queued in this specification is the WebSocket task source defined in [[!WEBSOCKETS-API]].
The term DOM is used to refer to the API set made available to
scripts in Web applications, and does not necessarily imply the
existence of an actual Document
object or of any other
Node
objects as defined in the DOM Core
specifications. [[!DOM-LEVEL-3-CORE]]
An IDL attribute is said to be getting when its value is being retrieved (e.g. by author script), and is said to be setting when a new value is assigned to it.
This API must be only exposed to trusted content.
The WebSocketServer interface defines attributes and methods for Web Socket Server communication
var webSocketServer = new WebSocketServer(urlToListen, serverID); webSocketServer.onconnect = function handleClient(event) { var port = event.port[0]; // security related informations var origin = port.origin; // check same origin or CORS access var cookies = port.cookies; // initial HTTP cookies var authorization = port.authorization; // initial HTTP Authorization header var remoteURI = port.remoteURI; // ex: "tcp:194.43.12.5:80" // handle client messages port.onmessage = function handleMessage(msgEvent) { // send a message to a specific client port.postMessage(message); } }; // send basic message to all connected clients webSocketServer.send(message); webSocketServer.onmessage = function (msgEvent) { // global handling of client message var origin = msgEvent.origin; // check same origin or CORS access var cookies = msgEvent.cookies; // initial HTTP cookies var authorization = msgEvent.authorization; // initial HTTP Authorization header var remoteURI = msgEvent.remoteURI; // ex: "tcp:194.43.12.5:80" };
TBD: Coming from the WebSocket interface. Used on the Server Interface to specified the listened URI
The readyState
attribute represents the state of the connection. It can have the
constant defined values
The bufferedAmount
attribute must return the number of bytes of
application data (UTF-8 text and binary data) that have been queued using send() but
that, as of the last time the event loop started executing a task, had not yet been
transmitted to the network. (This thus includes any text sent during the execution of
the current task, regardless of whether the user agent is able to transmit text
asynchronously with script execution.) This does not include framing overhead incurred
by the protocol, or buffering done by the operating system or network hardware.
If the connection is closed, this attribute's value will only increase with each call
to the send()
method (the number does not reset to zero once the
connection closes).
Event handler that corresponds to the open
event, which is
fired when the socket is open and ready to receive connection attempts from clients. If
the creation of the server socket fails the error event will be fired instead.
Event handler that corresponds to the connect
event, which is
fired repeatedly and asynchronously after the WebSocketServer
object has been created, every time an incoming connection attempt on the specified URI
has been accepted.
Event handler that corresponds to the error
event, which is
fired when there is an error on the server socket. The data attribute of the event passed
to the onerror handler will have a description of the kind of error.
TBD: Coming from the WebSocket interface. To be specified if required
TBD: Coming from the WebSocket interface. To be specified if required
TBD: Coming from the WebSocket interface. To be specified if required
Closes the Web socket. A closed Web socket can not be used any more.
TBD: Coming from the WebSocket interface. Expected to be used to handle messages from whatever connection. May be source of security issues.
When a WebSocket object is created, its binaryType IDL attribute must be set to the string "blob". On getting, it must return the last value it was set to. On setting, if the new value is either the string "blob" or the string "arraybuffer", then set the IDL attribute to this new value. Otherwise, throw a SyntaxError exception.
As WebSocketServer
can be connected to multiple
client, this property should also be at the port
level and
depend on each connection related port.onmessage
event
Sends data on the given Web socket to all the connected clients.
The WebSocket protocol specification defines Ping and Pong frames that can be used for keep-alive, heart-beats, network status probing, latency instrumentation, and so forth. These are not currently exposed in the API.
TBD: Section coming from The Web Sockets API [[!WEBSOCKETS-API]]. To be specified if required
The steps to parse a WebSocket URL's components from a string url are as follows. These steps return either a host, a port, a resource name, and a secure flag, or they fail.
TBD: Section coming from The Web Sockets API [[!WEBSOCKETS-API]]. To be specified if required
TO BE DONE
connect
Event InterfaceCookies from the HTTP connection which initiated the Web Socket connection
May be used to bind the message to an existing session
Content of the Authorization header of the HTTP connection which initiated the Web Socket connection
May be used to authenticate the user and provide related permissions
message
Event InterfaceCookies from the HTTP connection which initiated the Web Socket connection
May be used to detect from which connection the message come from and to bind the message to an existing session
Content of the Authorization header of the HTTP connection which initiated the Web Socket connection
May be used to authenticate the user and provide related permissions
TBD: Section coming from The Web Sockets API [[!WEBSOCKETS-API]]. To be specified if required
Many thanks to Robin Berjon for making our lives so much easier with his cool tool.