User Tools

Site Tools


en:code:data:udp

Udp

This object allows you to establish a connection to an UDP peer.

Functions

Udp

Constructor function.

Syntax

var myObject = new Udp(events);

Arguments

  1. events - (object) the events that can be called
    • onConnected - (onConnected) called when the object is connected to the peer
    • onDisconnected - (onDisconnected) called when the object is disconnected from the peer
    • onReadyRead - (onReadyRead) called when the object is ready to read data sent from the peer
    • onError - (onError) called when an error has occured

Example

var myObject = new Udp();
var myObject = new Udp({
	onConnected: function()
	{
		Console.print("Connected!");
	},
	onDisconnected: function()
	{
		Console.print("Disconnected!");
	}
});

Methods

connect

Opens a connection to a peer.

Syntax

myObject.connect(hostname, port, openMode);
myObject.connect(hostname, port);

Arguments

  1. hostname - (string) the hostname to connect to (ip address or dns)
  2. port - (integer) the port number
  3. openMode - (OpenMode) the open mode (default: read & write)

Returns

  • (Udp) this Udp

Notes

This method is asynchronous, this means that it will return before a connection is established. To know when a connection is made use the onConnected event or the waitForConnected method.

write

Writes some data to the peer.

Syntax

myObject.write(data);

Arguments

  1. data - (mixed) the data to write

Returns

  • (Udp) this Udp

Notes

This method is asynchronous, this means that it will return before the data has been written. Due to the unreliable nature of UDP you have no means to know if the data has arrived.

writeText

Writes some text to the peer.

Syntax

myObject.writeText(text);

Arguments

  1. text - (string) the text to write

Returns

  • (Udp) this Udp

Notes

This method is asynchronous, this means that it will return before the data has been written. Due to the unreliable nature of UDP you have no means to know if the data has arrived.

disconnect

Closes a connection with a peer.

Syntax

myObject.disconnect();
myObject.disconnect();

Returns

  • (Udp) this Udp

Notes

This method is asynchronous, this means that it will return before the connection is closed. To know when the connection is closed use the onDisconnected event or the waitForDisconnected method.

read

Returns the data sent by the peer.

Syntax

myObject.read();

Returns

  • (mixed) the data

Notes

To know when data is available use the onReadyRead event or the waitForReadyRead method.

readText

Returns the text sent by the peer.

Syntax

myObject.readText(encoding);
myObject.readText();

Arguments

  1. encoding - (Encoding) the encoding to use

Returns

  • (string) the text

Notes

To know when text is available use the onReadyRead event or the waitForReadyRead method.

waitForConnected

Freezes the execution until a connection has been established or waitTime time elapsed.

Syntax

myObject.waitForConnected(waitTime);
myObject.waitForConnected();

Arguments

  1. waitTime - (integer) the time to wait (milliseconds, default: 30000)

Returns

  • (Udp) this Udp

Exceptions

  • (ConnectionError) cannot establish a connection to the host

waitForDisconnected

Freezes the execution until the connection has been closed or waitTime time elapsed.

Syntax

myObject.waitForDisconnected(waitTime);
myObject.waitForDisconnected();

Arguments

  1. waitTime - (integer) the time to wait (milliseconds, default: 30000)

Returns

  • (Udp) this Udp

Exceptions

  • (DisconnectionError) waiting for disconnection failed

waitForReadyRead

Freezes the execution until data is available or waitTime time elapsed.

Syntax

myObject.waitForReadyRead(waitTime);
myObject.waitForReadyRead();

Arguments

  1. waitTime - (integer) the time to wait (milliseconds, default: 30000)

Returns

  • (Udp) this Udp

Exceptions

  • (ReadyReadError) waiting for ready read failed

Events

onConnected

Called when a connection is established.

Syntax

myObject.onConnected = function() {};

Example

myObject.onConnected = function()
{
	//Event action
};

onDisconnected

Called when a connection is closed.

Syntax

myObject.onDisconnected = function() {};

Example

myObject.onDisconnected = function()
{
	//Event action
};

onReadyRead

Called when data is available and can be read using the read method.

Syntax

myObject.onReadyRead = function() {};

Example

myObject.onReadyRead = function()
{
	//Event action
};

onError

Called when an error has occurred.

Syntax

myObject.onError = function(errorMessage) {};

Arguments

  1. errorMessage - (string) text describing the error

Example

myObject.errorMessage = function(errorMessage)
{
	//Event action
};

Enumerations

OpenMode

Udp open mode.

Values

  1. ReadOnly: socked opened for reading only
  2. WriteOnly: socked opened for writing only
  3. ReadWrite: socked opened for reading and writing
  4. Unbuffered: socked opened in unbuffered mode

Notes

This is a flag enumeration, that means that you can use multiple values using the | operator. Example:

myObject.connect("127.0.0.1", 80, Udp.ReadOnly | Udp.Unbuffered);
en/code/data/udp.txt · Last modified: 2021/02/13 11:23 by 127.0.0.1