Use this API to send and receive data over the network using TCP or UDP.
This specification defines conformance criteria that apply to a single product: the user agent that implements the interfaces that it contains.
Implementations that use ECMAScript to implement 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.
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]].
This API must be only exposed to trusted content.
The UDPSocket interface defines the attributes and methods for a UDP socket
// // This example shows a a simple implementation of UPnP-SSDP M-SEARCH discovery using a multicast UDPSocket // // Create a UDP socket var mySocket = new UDPSocket (); // Build an SSDP M-SEARCH multicast message var MSearch = "M-SEARCH * HTTP/1.1\r\n" + "ST: " + ServiceType + "\r\n" + "MAN: \"ssdp:discover\"\r\n" + "HOST: 239.255.255.250:1900\r\n" + "MX: 10\r\n\r\n"; var SSDPMulticastAddress = "239.255.255.250"; var SSDPMulticastPort = 1900; try { // Send SSDP M-SEARCH multicast message var moreBufferingOK = mySocket.sendUDP(MSearch, SSDPMulticastAddress, SSDPMulticastPort); console.log('M-SEARCH Sent!'); } catch(err) { // Sending failed console.error('Sending M-SEARCH failed: ' + err.name); } // Receive M-SEARCH responses mysocket.onreceivedudp = function (receivedUDPEvent) { console.log(“Peer address: ” + receivedUDPEvent.peerAddress + “ Peer port: “ + receivedUDPEvent.peerPort + " Received data" + receivedUDPEvent.data); };
// // This example shows a a simple implementation of reception of UPnP-SSDP NOTIFY multicast messages // var SSDPMulticastAddress = "239.255.255.250"; var SSDPMulticastPort = 1900; // Create a UDP socket and bind it to SSDP multicast address and port var mySocket = new UDPSocket ({"localAddress":SSDPMulticastAddress, "localPort":SSDPMulticastPort}); // Receive SSDP NOTFIY mysocket.onreceivedudp = function (receivedUDPEvent) { console.log(“Peer address: ” + receivedUDPEvent.peerAddress + “ Peer port: “ + receivedUDPEvent.peerPort + " Received data" + receivedUDPEvent.data); };
Sends data on the given UDP socket to the given address and port.
Pause reading incoming UDP data and invocations of the onreceivedudp
handler until resume is called.
Resume reading incoming UDPdata and invoking onreceivedudp
as usual.
When the UDPSocket
constructor is invoked, the User Agent MUST run the following steps:
localAddress
argument is absent or null then bind the socket to any available local IPv4/6 address.
Otherwise, if the requested local address is free or if it is in use and the options
.addressReuse
attribute is
true then bind the socket to the requested local address. Else, throw an InvalidAccessError
exception and abort these steps.
localPort
argument is absent or null bind then the socket to any available local port. Otherwise, if the requested
local port is free or if it is in use and the options
.addressReuse
attribute is true then bind the socket to the
requested local port. Else, throw an InvalidAccessError
exception and abort these steps.
UDPSocket
object and set the localAddress
and localPort
attributes according to the
steps above. Set the options
attribute according to the constructors options
argument if it was present,
else set the options
attribute to the default values.
UDPSocket
object to the application.
The sendUDP
method when invoked MUST run the following steps:
data
parameter to the address and port of the recipient indicated in the
peerAddress
and peerPort
parameters.
InvalidAccessError
and abort these steps.
InvalidStateError
and abort these steps.
InvalidAccessError
and abort these steps.
Upon a new UDP datagram being received, the user agent MUST:
ReceivedUDPEvent
.
data
of the ReceivedUDPEvent
to the contents of the data field of the received UDP datagram in the
datatype as defined by the UDPSocket constructor's options
binaryType
attribute.
peerAddress
of the ReceivedUDPEvent
to the source address of the received UDP datagram.
peerPort
of the ReceivedUDPEvent
to the source port of the received UDP datagram.
receivedudp
with the
newly created ReceivedUDPEvent
instance at the UDPSocket object.
In the process of sending UDP data, upon a detection that previously-buffered data has been written to the network and it is possible to buffer more data received from the application, the user agent MUST:
drainudp
at the UDPSocket object.
The following are the event handlers (and their corresponding event handler event types) that MUST be supported as attributes by the UDPSocket object.
event handler | event handler event type |
---|---|
onreceivedudp |
receivedudp |
ondrainudp |
drainudp |
The receivedudp
event SHALL implement the ReceivedUDPEvent interface.
The ReceivedUDPEvent interface represents events related to received UDP data.
UDPsocket create options.
Many thanks to Robin Berjon for making our lives so much easier with his cool tool.