-
Couldn't load subscription status.
- Fork 11
Network API
Accessed through: $$.net
=============
Provides raw TCP protocol connection socket.
Create new socket object in the closed state.
var socket = new $$.net.TCPSocket();Open socket and connect to TCP server using ip address and port.
| Argument | Type | Description |
|---|---|---|
| ip | IP4Address | string | Server IP address to connect to. |
| port | number | TCP server port. |
socket.open('127.0.0.1', 8080);
socket.open(new IP4Address(127, 0, 0, 1), 8080);Push data buffer into socket transmit queue. This does not copy data, buffer will be sent directly to network interface, data modifications made after send() call may affect transmitted data.
| Argument | Type | Description |
|---|---|---|
| buffer | Uint8Array | Buffer to send. |
| return | bool | Hint to the caller that transmit queue is full. |
socket.send(new Uint8Array([1, 2, 3]));Send stream ended notification (FIN packet), but keep receiving new data.
socket.halfclose();Close the socket, stop transmitting and receiving new data.
socket.close();Handler for socket opened (connection) event.
socket.onopen = function() {
console.log('connected');
};
socket.open('127.0.0.1', 8080);Handler for received data events.
| Callback argument | Type | Description |
|---|---|---|
| buffer | Uint8Array | Received buffer. |
socket.ondata = function(buffer) {
console.log('new data', buffer);
};Handler for halfclose() event from the other end (received FIN packet).
socket.onend = function() {
console.log('received all data');
};Handler for socket closed event.
socket.onclose = function() {
console.log('socket closed');
};Provides TCP server socket.
Create new server socket object in the closed state.
var socket = new $$.net.TCPServerSocket();Open socket and start listening for new connections.
| Argument | Type | Description |
|---|---|---|
| port | number | TCP port to listen to. |
socket.listen(8080);Close socket and stop listening.
socket.close();Incoming connection event handler.
| Callback argument | Type | Description |
|---|---|---|
| socket | TCPSocket | Connected socket. |
socket.onconnect = function(socket) {
console.log('new connection to the server');
};Provides raw UDP datagram protocol socket.
Create new datagram socket object.
var socket = new $$.net.UDPSocket();Bind socket to UDP port and start receiving data.
| Argument | Type | Description |
|---|---|---|
| port | number | UDP port to bind to. |
socket.bind(5555);Send data to specified ip using port.
| Argument | Type | Description |
|---|---|---|
| ip | IP4Address | string | Server IP address to send buffer to. |
| port | number | UDP port. |
| buffer | Uint8Array | Buffer to send. |
socket.send('127.0.0.1', 4000, new Uint8Array([1, 2, 3]));Handler for received datagrams.
| Callback argument | Type | Description |
|---|---|---|
| ip | IP4Address | Sender IP address. |
| port | number | Sender port. |
| buffer | Uint8Array | Received buffer. |
socket.onmessage = function(ip, port, buffer) {
console.log('received message from', ip.toString(), port);
};Represents IPv4 address. Useful to avoid unnecessary address string conversions and string parsing in different parts of the network stack.
Create new IPv4 address object.
| Argument | Type | Description |
|---|---|---|
| a, b, c, d | number | Address octets. |
var addr = new $$.net.IP4Address(192, 168, 1, 1);Convert address to string.
new IP4Address(127, 0, 0, 1).toString() // '127.0.0.1'Parse IPv4 address from string.
| Argument | Type | Description |
|---|---|---|
| str | string | IP address string. |
IP4Address.parse('127.0.0.1') // new IP4Address(127, 0, 0, 1)Returns ANY address object 0.0.0.0
IP4Address.ANY // new IP4Address(0, 0, 0, 0)Returns BROADCAST address object 255.255.255.255
IP4Address.BROADCAST // new IP4Address(255, 255, 255, 255)// TODO: Translate to Russian