Skip to content
This repository has been archived by the owner on Jul 3, 2020. It is now read-only.

runtime.net

facekapow edited this page Sep 4, 2016 · 1 revision

Networking

Accessed through: runtime.net

=============

net.TCPSocket

Provides raw TCP protocol connection socket.

constructor()

Create new socket object in the closed state.

var socket = new runtime.net.TCPSocket();

open(ip, port)

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);

send(buffer)

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]));

halfclose()

Send stream ended notification (FIN packet), but keep receiving new data.

socket.halfclose();

close()

Close the socket, stop transmitting and receiving new data.

socket.close();

onopen = function()

Handler for socket opened (connection) event.

socket.onopen = function() {
  console.log('connected');
};
socket.open('127.0.0.1', 8080);

ondata = function(buffer)

Handler for received data events.

Callback argument Type Description
buffer Uint8Array Received buffer.
socket.ondata = function(buffer) {
  console.log('new data', buffer);
};

onend = function()

Handler for halfclose() event from the other end (received FIN packet).

socket.onend = function() {
  console.log('received all data');
};

onclose = function()

Handler for socket closed event.

socket.onclose = function() {
  console.log('socket closed');
};

net.TCPServerSocket

Provides TCP server socket.

constructor()

Create new server socket object in the closed state.

var socket = new runtime.net.TCPServerSocket();

listen(port)

Open socket and start listening for new connections.

Argument Type Description
port number TCP port to listen to.
socket.listen(8080);

close()

Close socket and stop listening.

socket.close();

onconnect = function(socket)

Incoming connection event handler.

Callback argument Type Description
socket TCPSocket Connected socket.
socket.onconnect = function(socket) {
  console.log('new connection to the server');
};

net.UDPSocket

Provides raw UDP datagram protocol socket.

constructor()

Create new datagram socket object.

var socket = new runtime.net.UDPSocket();

bind(port)

Bind socket to UDP port and start receiving data.

Argument Type Description
port number UDP port to bind to.
socket.bind(5555);

send(ip, port, buffer)

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]));

onmessage = function(ip, port, buffer)

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);
};

net.IP4Address

Represents IPv4 address. Useful to avoid unnecessary address string conversions and string parsing in different parts of the network stack.

constructor(a, b, c, d)

Create new IPv4 address object.

Argument Type Description
a, b, c, d number Address octets.
var addr = new runtime.net.IP4Address(192, 168, 1, 1);

toString()

Convert address to string.

new IP4Address(127, 0, 0, 1).toString()  // '127.0.0.1'

IP4Address.parse(str)

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)

IP4Address.ANY

Returns ANY address object 0.0.0.0

IP4Address.ANY  // new IP4Address(0, 0, 0, 0)

IP4Address.BROADCAST

Returns BROADCAST address object 255.255.255.255

IP4Address.BROADCAST  // new IP4Address(255, 255, 255, 255)