UDPSocket

Methods Summarized

Type
Name
Summary

void

Closes the socket and cleans up the resources.

Get a packet from receive buffer, read buffer until empty (null is returned).

Send a packet, over this socket, no need to start it if you don't want to listen to incoming packages.

Send a packet, over this socket, no need to start it if you don't want to listen to incoming packages.

Starts the socket to listen for incoming packets, no need to call this method if you only want to send packets.

void

Put a test packet in the receive buffer to test your method call and getReceivedPacket.

Methods Detailed

close()

Closes the socket and cleans up the resources.

Returns: void

getReceivedPacket()

Get a packet from receive buffer, read buffer until empty (null is returned).

Returns: JSPacket the next JSPacket from the receive buffer, or null if the buffer is empty.

Sample

var packet = null
while( ( packet = socket.getReceivedPacket() ) != null)
{
	var text = packet.readUTF()
	var count = packet.readInt()
}

sendPacket(destIpOrHostname, packet)

Send a packet, over this socket, no need to start it if you don't want to listen to incoming packages. This will send the packet to the same port as the socket itself is bound to.

Parameters

  • String destIpOrHostname the ip of the destination or the hostname

  • JSPacket packet the JSPacket to send

Returns: Boolean true if the packet was successfully sent; otherwise, false.

Sample

var socket = plugins.udp.createSocket(4321);
var packet = plugins.udp.createNewPacket();
packet.writeUTF('hello world!')
socket.sendPacket('10.0.0.1',packet)
socket.close();

sendPacket(destIpOrHostname, packet, port)

Send a packet, over this socket, no need to start it if you don't want to listen to incoming packages. This will send the packet to the give port on the destination.

Parameters

  • String destIpOrHostname the ip of the destination or the hostname

  • JSPacket packet the JSPacket to send

  • Number port the port on which to send the packet

Returns: Boolean true if the packet was successfully sent to the specified port; otherwise, false.

Sample

var socket = plugins.udp.createSocket(4321);
var packet = plugins.udp.createNewPacket()
packet.writeUTF('hello world!')
socket.sendPacket('10.0.0.1',packet, 9999)
socket.close();

start(packageReceivedCallback)

Starts the socket to listen for incoming packets, no need to call this method if you only want to send packets. the given function will be called when a packet is received, it will get as a parameter UPPSocket instance itself.

Parameters

  • Function packageReceivedCallback the callback function that will be called when a package is received, it will get as a parameter UPPSocket instance itself.

Returns: UDPSocket The UDPSocket instance, allowing method chaining after starting the listener.

Sample

var socket = plugins.udp.createSocket(4321).start(callbackFunction);
function callbackFunction() {
  var string = socket.getReceivedPacket().readUTF();
  application.output(string);
}

testPacket(packet)

Put a test packet in the receive buffer to test your method call and getReceivedPacket.

Parameters

Returns: void

Sample

var packet = plugins.udp.createNewPacket()
packet.writeUTF('hello world!')
plugins.udp.testPacket(packet)

Last updated

Was this helpful?