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); | ||
| + | </ | ||
| + | |||
| + | === Arguments === | ||
| + | - events - (object) the events that can be called | ||
| + | * onConnected - ([[# | ||
| + | * onDisconnected - ([[# | ||
| + | * onReadyRead - ([[# | ||
| + | * onBytesWritten - ([[# | ||
| + | * onError - ([[# | ||
| + | |||
| + | === Example === | ||
| + | <code javascript> | ||
| + | var myObject = new Tcp(); | ||
| + | </ | ||
| + | <code javascript> | ||
| + | var myObject = new Tcp({ | ||
| + | onConnected: | ||
| + | { | ||
| + | Console.print(" | ||
| + | }, | ||
| + | onDisconnected: | ||
| + | { | ||
| + | Console.print(" | ||
| + | } | ||
| + | }); | ||
| + | </ | ||
| + | |||
| + | ===== Methods ===== | ||
| + | |||
| + | ==== connect ==== | ||
| + | Opens a connection to a server. | ||
| + | |||
| + | === Syntax === | ||
| + | <code javascript> | ||
| + | myObject.connect(hostname, | ||
| + | </ | ||
| + | <code javascript> | ||
| + | myObject.connect(hostname, | ||
| + | </ | ||
| + | |||
| + | === 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, | ||
| + | To know when a connection is made use the [[# | ||
| + | |||
| + | ==== write ==== | ||
| + | Writes some data to the server. | ||
| + | |||
| + | === Syntax === | ||
| + | <code javascript> | ||
| + | myObject.write(data); | ||
| + | </ | ||
| + | |||
| + | === Arguments === | ||
| + | - data - (mixed) the data to write | ||
| + | |||
| + | === Returns === | ||
| + | * (Tcp) this Tcp | ||
| + | |||
| + | === Notes === | ||
| + | This method is asynchronous, | ||
| + | To know when the data has been sent use the [[# | ||
| + | |||
| + | ==== writeText ==== | ||
| + | Writes some text to the server. | ||
| + | |||
| + | === Syntax === | ||
| + | <code javascript> | ||
| + | myObject.writeText(text); | ||
| + | </ | ||
| + | |||
| + | === Arguments === | ||
| + | - text - (string) the text to write | ||
| + | |||
| + | === Returns === | ||
| + | * (Tcp) this Tcp | ||
| + | |||
| + | === Notes === | ||
| + | This method is asynchronous, | ||
| + | To know when the text has been sent use the [[# | ||
| + | |||
| + | ==== disconnect ==== | ||
| + | Closes a connection with a server. | ||
| + | |||
| + | === Syntax === | ||
| + | <code javascript> | ||
| + | myObject.disconnect(); | ||
| + | </ | ||
| + | <code javascript> | ||
| + | myObject.disconnect(); | ||
| + | </ | ||
| + | |||
| + | === Returns === | ||
| + | * (Tcp) this Tcp | ||
| + | |||
| + | === Notes === | ||
| + | This method is asynchronous, | ||
| + | To know when the connection is closed use the [[# | ||
| + | |||
| + | ==== read ==== | ||
| + | Returns the data sent by the server. | ||
| + | |||
| + | === Syntax === | ||
| + | <code javascript> | ||
| + | myObject.read(); | ||
| + | </ | ||
| + | |||
| + | === Returns === | ||
| + | * (mixed) the data | ||
| + | |||
| + | === Notes === | ||
| + | To know when data is available use the [[# | ||
| + | |||
| + | ==== readText ==== | ||
| + | Returns the text sent by the server. | ||
| + | |||
| + | === Syntax === | ||
| + | <code javascript> | ||
| + | myObject.readText(encoding); | ||
| + | </ | ||
| + | <code javascript> | ||
| + | myObject.readText(); | ||
| + | </ | ||
| + | |||
| + | === Arguments === | ||
| + | - encoding - ([[en: | ||
| + | |||
| + | === Returns === | ||
| + | * (string) the text | ||
| + | |||
| + | === Notes === | ||
| + | To know when text is available use the [[# | ||
| + | |||
| + | ==== waitForConnected ==== | ||
| + | Freezes the execution until a connection has been established or **waitTime** time elapsed. | ||
| + | |||
| + | === Syntax === | ||
| + | <code javascript> | ||
| + | myObject.waitForConnected(waitTime); | ||
| + | </ | ||
| + | <code javascript> | ||
| + | myObject.waitForConnected(); | ||
| + | </ | ||
| + | |||
| + | === Arguments === | ||
| + | - waitTime - (integer) the time to wait (milliseconds, | ||
| + | |||
| + | === 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 javascript> | ||
| + | myObject.waitForDisconnected(); | ||
| + | </ | ||
| + | |||
| + | === Arguments === | ||
| + | - waitTime - (integer) the time to wait (milliseconds, | ||
| + | |||
| + | === 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 javascript> | ||
| + | myObject.waitForReadyRead(); | ||
| + | </ | ||
| + | |||
| + | === Arguments === | ||
| + | - waitTime - (integer) the time to wait (milliseconds, | ||
| + | |||
| + | === 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 javascript> | ||
| + | myObject.waitForBytesWritten(); | ||
| + | </ | ||
| + | |||
| + | === Arguments === | ||
| + | - waitTime - (integer) the time to wait (milliseconds, | ||
| + | |||
| + | === 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() {}; | ||
| + | </ | ||
| + | |||
| + | === Example === | ||
| + | <code javascript> | ||
| + | myObject.onConnected = function() | ||
| + | { | ||
| + | //Event action | ||
| + | }; | ||
| + | </ | ||
| + | |||
| + | ==== onDisconnected ==== | ||
| + | Called when a connection is closed. | ||
| + | |||
| + | === Syntax === | ||
| + | <code javascript> | ||
| + | myObject.onDisconnected = function() {}; | ||
| + | </ | ||
| + | |||
| + | === Example === | ||
| + | <code javascript> | ||
| + | myObject.onDisconnected = function() | ||
| + | { | ||
| + | //Event action | ||
| + | }; | ||
| + | </ | ||
| + | |||
| + | ==== onReadyRead ==== | ||
| + | Called when data is available and can be read using the [[# | ||
| + | |||
| + | === Syntax === | ||
| + | <code javascript> | ||
| + | myObject.onReadyRead = function() {}; | ||
| + | </ | ||
| + | |||
| + | === Example === | ||
| + | <code javascript> | ||
| + | myObject.onReadyRead = function() | ||
| + | { | ||
| + | //Event action | ||
| + | }; | ||
| + | </ | ||
| + | |||
| + | ==== onBytesWritten ==== | ||
| + | Called when data sent to the server has been written. | ||
| + | |||
| + | === Syntax === | ||
| + | <code javascript> | ||
| + | myObject.onBytesWritten = function(bytes) {}; | ||
| + | </ | ||
| + | |||
| + | === Arguments === | ||
| + | - bytes - (integer) the number of bytes that have been written | ||
| + | |||
| + | === Example === | ||
| + | <code javascript> | ||
| + | myObject.onBytesWritten = function(bytes) | ||
| + | { | ||
| + | //Event action | ||
| + | }; | ||
| + | </ | ||
| + | |||
| + | ==== onError ==== | ||
| + | Called when an error has occurred. | ||
| + | |||
| + | === Syntax === | ||
| + | <code javascript> | ||
| + | myObject.onError = function(errorMessage) {}; | ||
| + | </ | ||
| + | |||
| + | === Arguments === | ||
| + | - errorMessage - (string) text describing the error | ||
| + | |||
| + | === Example === | ||
| + | <code javascript> | ||
| + | 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, | ||
| + | Example: | ||
| + | <code javascript> | ||
| + | myObject.connect(" | ||
| + | </ | ||
