TCPSocket
Version and platform compatibility
- This feature was introduced in version 21.1.1 of the JourneyApps Container for iOS, Android and Desktop, and requires version 4.78.0 or greater of the JourneyApps Runtime.
The
TCPSocket
API allows applications to directly connect with external devices over TCP sockets.A
TCPSocket.info()
function is available to check if the user's device has access to the TCP sockets. It returns an object:Field | Type | Description |
---|---|---|
supported | boolean | Indicates if TCPSocket is supported on the current device. Not supported on Web |
To connect to a TCP socket, the host and port of the socket is required.
To connect, use
var tcpSocket = TCPSocket.connect({host: host, port: port, timeout: timeout}, readHandler, closeHandler)
.Parameter | Description |
---|---|
host | String hostname of socket, e.g. '10.0.0.1'. |
port | Number of the port of the socket, e.g. 8080. |
timeout | Number value in milliseconds. This value overrides the timeout period, which closes the TCP socket connection after network inactivity. Its default value is 10 seconds. Note: This parameter was introduced in version 4.86.6 of the JourneyApps Runtime. |
readHandler | Optional callback that gets called when data is present on the socket. |
closeHander | Optional callback that gets called when an error occurs or socket closes. The closeHandler will have an optional error parameter that will be a string, or null /undefined when there isn't an error. |
JavaScript
TypeScript
var socketHost = '192.168.1.1';
var socketPort = 8080;
var readHandler = function(data) {
// data received from TCP socket
console.log(data);
}
var closeHandler = function(error) {
if (error) {
console.log("Error", error);
}
}
// use a try .. catch, since an exception may be thrown.
try {
var tcpSocket = TCPSocket.connect({host: socketHost, port: socketPort}, readHandler, closeHandler);
} catch (error) {
console.log(error);
}
var socketHost = '192.168.1.1';
var socketPort = 8080;
var readHandler = function(data) {
// data received from TCP socket
console.log(data);
}
var closeHandler = function(error) {
if (error) {
console.log("Error", error);
}
}
// use a try .. catch, since an exception may be thrown.
try {
var tcpSocket = await TCPSocket.connect({host: socketHost, port: socketPort}, readHandler, closeHandler);
} catch (error) {
console.log(error);
}
There are two ways to read data from the socket.
The
readHandler
from the example above uses a callback with data in Uint8Array when there is data available.A ReadableStream object is also available. To use the ReadableStream, the following can be used:
JavaScript
TypeScript
var stream = mySocket.stream;
var streamReader = stream.getReader();
function startRead() {
setTimeout(function() {
streamReader.read().then(function(dataRead) {
if (dataRead.done) {
console.log("Stream finished");
} else {
// use data:
console.log("Data", dataRead.value);
// trigger another read:
startRead();
}
// in the case where view variables are changed,
// e.g. updating text on the view,
// we need to call the journey.forceDigest() function
// due to this code running inside a setTimeout:
}).then(journey.forceDigest);
}, 1000);
}
// call the startRead function once:
startRead();
var stream = mySocket.stream;
var streamReader = stream.getReader();
function startRead() {
setTimeout(async function() {
var dataRead = await streamReader.read();
if (dataRead.done) {
console.log("Stream finished");
} else {
// use data:
console.log("Data", dataRead.value);
// in the case where view variables are changed,
// e.g. updating text on the view,
// we need to call the journey.forceDigest() function
// due to this code running inside a setTimeout:
journey.forceDigest();
// trigger another read:
startRead();
}
}, 1000);
}
// call the startRead function once:
startRead();
Data must be continually read when using the ReadableStream. In the example above, the data is read one second after previous reading.
ReadableStream methods can be used, including piping through other streams, e.g. TextDecoderStream:
JavaScript
TypeScript
var stream = mySocket.stream.pipeThrough(new TextDecoderStream());
stream.read().then(function(data) {
if (!data.done) {
// result is String
console.log(data.value);
}
});
var stream = mySocket.stream.pipeThrough(new TextDecoderStream());
var data = await stream.read();
if (!data.done) {
// result is String
console.log(data.value);
}
The
TCPSocket.connect
function returns a TCPSocket instance. This instance is used to write data to the socket in binary (Uint8Array) format.JavaScript
TypeScript
var tcpSocket = TCPSocket.connect({host: socketHost, port: socketPort}, readHandler, closeHandler);
// writing binary data ("Hello" in hexadecimal)
var binaryData = new Uint8Array([0x48, 0x65, 0x6c, 0x6c, 0x6f]);
tcpSocket.write(binaryData);
var tcpSocket = await TCPSocket.connect({host: socketHost, port: socketPort}, readHandler, closeHandler);
// writing binary data ("Hello" in hexadecimal)
var binaryData = new Uint8Array([0x48, 0x65, 0x6c, 0x6c, 0x6f]);
await tcpSocket.write(binaryData);
To close a connection, call
close()
on the connection object, e.g. tcpSocket.close()
.To reset all the sockets, call
TCPSocket.reset()
. This closes all open sockets.There is also an
isOpen
property on each connection object, e.g. tcpSocket.isOpen
that returns a boolean to check if the socket is open. Note that it may take some time after a socket has disconnected for the socket itself to register that it has closed.Last modified 4mo ago