User Tools

Site Tools


en:code:data:tcp

Differences

This shows you the differences between two versions of the page.


Previous revision
en:code:data:tcp [2021/02/13 11:23] (current) – external edit 127.0.0.1
Line 1: Line 1:
 +====== Tcp ======
 +This object allows you to establish a connection to a TCP server.
  
 +===== Functions =====
 +
 +==== Tcp ====
 +Constructor function.
 +
 +=== Syntax ===
 +<code javascript>
 +var myObject = new Tcp(events);
 +</code>
 +
 +=== Arguments ===
 +  - events - (object) the events that can be called
 +    * onConnected - ([[#onConnected|onConnected]]) called when the object is connected to the server
 +    * onDisconnected - ([[#onDisconnected|onDisconnected]]) called when the object is disconnected from the server
 +    * onReadyRead - ([[#onReadyRead|onReadyRead]]) called when the object is ready to read data sent from the server
 +    * onBytesWritten - ([[#onBytesWritten|onBytesWritten]]) called when the object has finished writing data to the server
 +    * onError - ([[#onError|onError]]) called when an error has occured
 +
 +=== Example ===
 +<code javascript>
 +var myObject = new Tcp();
 +</code>
 +<code javascript>
 +var myObject = new Tcp({
 + onConnected: function()
 + {
 + Console.print("Connected!");
 + },
 + onDisconnected: function()
 + {
 + Console.print("Disconnected!");
 + }
 +});
 +</code>
 +
 +===== Methods =====
 +
 +==== connect ====
 +Opens a connection to a server.
 +
 +=== Syntax ===
 +<code javascript>
 +myObject.connect(hostname, port, openMode);
 +</code>
 +<code javascript>
 +myObject.connect(hostname, port);
 +</code>
 +
 +=== 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|onConnected]] event or the [[#waitForConnected|waitForConnected]] method.
 +
 +==== write ====
 +Writes some data to the server.
 +
 +=== Syntax ===
 +<code javascript>
 +myObject.write(data);
 +</code>
 +
 +=== 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|onBytesWritten]] event or the [[#waitForBytesWritten|waitForBytesWritten]] method.
 +
 +==== writeText ====
 +Writes some text to the server.
 +
 +=== Syntax ===
 +<code javascript>
 +myObject.writeText(text);
 +</code>
 +
 +=== 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|onBytesWritten]] event or the [[#waitForBytesWritten|waitForBytesWritten]] method.
 +
 +==== disconnect ====
 +Closes a connection with a server.
 +
 +=== Syntax ===
 +<code javascript>
 +myObject.disconnect();
 +</code>
 +<code javascript>
 +myObject.disconnect();
 +</code>
 +
 +=== 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|onDisconnected]] event or the [[#waitForDisconnected|waitForDisconnected]] method.
 +
 +==== read ====
 +Returns the data sent by the server.
 +
 +=== Syntax ===
 +<code javascript>
 +myObject.read();
 +</code>
 +
 +=== Returns ===
 +  * (mixed) the data
 +
 +=== Notes ===
 +To know when data is available use the [[#onReadyRead|onReadyRead]] event or the [[#waitForReadyRead|waitForReadyRead]] method.
 +
 +==== readText ====
 +Returns the text sent by the server.
 +
 +=== Syntax ===
 +<code javascript>
 +myObject.readText(encoding);
 +</code>
 +<code javascript>
 +myObject.readText();
 +</code>
 +
 +=== Arguments ===
 +  - encoding - ([[en:code:core:global#encoding|Encoding]]) the encoding to use
 +
 +=== Returns ===
 +  * (string) the text
 +
 +=== Notes ===
 +To know when text is available use the [[#onReadyRead|onReadyRead]] event or the [[#waitForReadyRead|waitForReadyRead]] method.
 +
 +==== waitForConnected ====
 +Freezes the execution until a connection has been established or **waitTime** time elapsed.
 +
 +=== Syntax ===
 +<code javascript>
 +myObject.waitForConnected(waitTime);
 +</code>
 +<code javascript>
 +myObject.waitForConnected();
 +</code>
 +
 +=== 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 ===
 +<code javascript>
 +myObject.waitForDisconnected(waitTime);
 +</code>
 +<code javascript>
 +myObject.waitForDisconnected();
 +</code>
 +
 +=== 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 ===
 +<code javascript>
 +myObject.waitForReadyRead(waitTime);
 +</code>
 +<code javascript>
 +myObject.waitForReadyRead();
 +</code>
 +
 +=== 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 ===
 +<code javascript>
 +myObject.waitForBytesWritten(waitTime);
 +</code>
 +<code javascript>
 +myObject.waitForBytesWritten();
 +</code>
 +
 +=== 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 ===
 +<code javascript>
 +myObject.onConnected = function() {};
 +</code>
 +
 +=== Example ===
 +<code javascript>
 +myObject.onConnected = function()
 +{
 + //Event action
 +};
 +</code>
 +
 +==== onDisconnected ====
 +Called when a connection is closed.
 +
 +=== Syntax ===
 +<code javascript>
 +myObject.onDisconnected = function() {};
 +</code>
 +
 +=== Example ===
 +<code javascript>
 +myObject.onDisconnected = function()
 +{
 + //Event action
 +};
 +</code>
 +
 +==== onReadyRead ====
 +Called when data is available and can be read using the [[#read|read]] method.
 +
 +=== Syntax ===
 +<code javascript>
 +myObject.onReadyRead = function() {};
 +</code>
 +
 +=== Example ===
 +<code javascript>
 +myObject.onReadyRead = function()
 +{
 + //Event action
 +};
 +</code>
 +
 +==== onBytesWritten ====
 +Called when data sent to the server has been written.
 +
 +=== Syntax ===
 +<code javascript>
 +myObject.onBytesWritten = function(bytes) {};
 +</code>
 +
 +=== Arguments ===
 +  - bytes - (integer) the number of bytes that have been written
 +
 +=== Example ===
 +<code javascript>
 +myObject.onBytesWritten = function(bytes)
 +{
 + //Event action
 +};
 +</code>
 +
 +==== onError ====
 +Called when an error has occurred.
 +
 +=== Syntax ===
 +<code javascript>
 +myObject.onError = function(errorMessage) {};
 +</code>
 +
 +=== Arguments ===
 +  - errorMessage - (string) text describing the error
 +
 +=== Example ===
 +<code javascript>
 +myObject.errorMessage = function(errorMessage)
 +{
 + //Event action
 +};
 +</code>
 +
 +===== 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:
 +<code javascript>
 +myObject.connect("127.0.0.1", 80, Tcp.ReadOnly | Tcp.Unbuffered);
 +</code>