The Connection object is used by JavaScript functions that implement a protocol for the JavaScript TCP Server and JavaScript TCP Client communication points. This object provides access to the underlying transport connection.

Methods

Method

Description

readByte()

Reads a byte from the connection.

readBlock()

Reads a block of bytes (up to 1024) from the connection and returns them as an array of bytes.

readBlock(size)

Reads a block of bytes (up to the size specified) from the connection and returns them as an array of bytes.

readString()

Reads a block of bytes (up to 1024) from the connection and returns them as a string using the platform's default character encoding system.

readString(size)

Reads a block of bytes (up to the size specified) from the connection and returns them as a string using the platform's default character encoding system.

readString(encoding)

Reads a block of bytes (up to 1024) from the connection and returns them as a string using the specified character encoding system.

readString(encoding, size)

Reads a block of bytes (up to the size specified) from the connection and returns them as a string using the specified character encoding system.

writeHex(hex)

Takes a hex-encoded binary block, decodes it and writes it to the connection.

write(byte)

Writes a single byte to the connection.

write(string)

Writes the string to the connection using the default character encoding system.

write(string, encoding)

Writes the string to the connection using the specified character encoding system.

flush()

Flushes the connection, forcing any buffered output bytes to be written immediately.

Example

In the following example, a JavaScript TCP Server and Client pair are used to implement a protocol. This simple protocol prepends "BEGIN" to the beginning of the message, and appends "END" to the end. Suppose the server is an output communication point, the client is an input communication point, and they are both bound to the same port. The send script that has been configured for the output communication point is shown below. It reads the message from the ROMessage object, and writes it to the Connection conn.

// Add header
conn.write("BEGIN");

// Write message
conn.write(message.text);

// Add footer
conn.write("END");

conn.flush();

The input communication point's receive script is shown next. It reads from the Connection object conn, checking that it first receives the string "BEGIN", and that it finally receives the string "END". The message, read from the connection, is stored in the Message object result.

This receive script is returned regardless of whether the message was received successfully or not. However, no return keyword is required, simply true or false.

// Check message header
var header = conn.readString(5);
if (!header.equals("BEGIN")) {
    throw new java.io.IOException("Cannot receive message, incorrect header");
}

// Find message body and footer
var remainingText = conn.readString();
var bodyLength = remainingText.length - 3;
result.text = remainingText.substring(0, bodyLength);
var footer = remainingText.substring(bodyLength);

// Check message footer
if (!footer.equals("END")) {
    throw new java.io.IOException("Warning, message did not have correct footer");
}

// Return true as message has been received successfully
true;