User Tools

Site Tools


en:code:data:tcp

Tcp

This object allows you to establish a connection to a TCP server.

Functions

Tcp

Constructor function.

Syntax

var myObject = new Tcp(events);

Arguments

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

Example

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

Methods

connect

Opens a connection to a server.

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

  • (Tcp) this Tcp

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 server.

Syntax

myObject.write(data);

Arguments

  1. data - (mixed) the data to write

Returns

  • (Tcp) this Tcp

Notes

This method is asynchronous, this means that it will return before the data has been written. To know when the data has been sent use the onBytesWritten event or the waitForBytesWritten method.

writeText

Writes some text to the server.

Syntax

myObject.writeText(text);

Arguments

  1. text - (string) the text to write

Returns

  • (Tcp) this Tcp

Notes

This method is asynchronous, this means that it will return before the data has been written. To know when the text has been sent use the onBytesWritten event or the waitForBytesWritten method.

disconnect

Closes a connection with a server.

Syntax

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

Returns

  • (Tcp) this Tcp

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 server.

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 server.

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

  • (Tcp) this Tcp

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

  • (Tcp) this Tcp

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

  • (Tcp) this Tcp

Exceptions

  • (ReadyReadError) waiting for ready read failed

waitForBytesWritten

Freezes the execution until all data has been written or waitTime time elapsed.

Syntax

myObject.waitForBytesWritten(waitTime);
myObject.waitForBytesWritten();

Arguments

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

Returns

  • (Tcp) this Tcp

Exceptions

  • (BytesWrittenError) waiting for bytes written 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
};

onBytesWritten

Called when data sent to the server has been written.

Syntax

myObject.onBytesWritten = function(bytes) {};

Arguments

  1. bytes - (integer) the number of bytes that have been written

Example

myObject.onBytesWritten = function(bytes)
{
	//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

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, Tcp.ReadOnly | Tcp.Unbuffered);
en/code/data/tcp.txt · Last modified: 2016/10/09 11:10 (external edit)