Table of Contents
Tcp
This object allows you to establish a connection to a TCP server.
Functions
Tcp
Constructor function.
Syntax
var myObject = new Tcp(events);
Arguments
- 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
- hostname - (string) the hostname to connect to (ip address or dns)
- port - (integer) the port number
- 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
- 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
- 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
- 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
- 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
- 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
- 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
- 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
- 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
- errorMessage - (string) text describing the error
Example
myObject.errorMessage = function(errorMessage) { //Event action };
Enumerations
OpenMode
Open mode.
Values
- ReadOnly: socked opened for reading only
- WriteOnly: socked opened for writing only
- ReadWrite: socked opened for reading and writing
- 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);