Skip to content
Merged
Show file tree
Hide file tree
Changes from all commits
Commits
File filter

Filter by extension

Filter by extension

Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
8 changes: 7 additions & 1 deletion README.md
Original file line number Diff line number Diff line change
Expand Up @@ -179,18 +179,24 @@ const client = TcpSocket.createConnection({
_Note: In order to use self-signed certificates make sure to [update your metro.config.js configuration](#self-signed-ssl-only-available-for-react-native--060)._

## API
Here are listed all methods implemented in `react-native-tcp-socket`, their functionalities are equivalent to those provided by Node's [net](https://nodejs.org/api/net.html) (more info on [#41](https://github.com/Rapsssito/react-native-tcp-socket/issues/41)). However, the **methods whose interface differs from Node are shown in bold**.
Here are listed all methods implemented in `react-native-tcp-socket`, their functionalities are equivalent to those provided by Node's [net](https://nodejs.org/api/net.html) (more info on [#41](https://github.com/Rapsssito/react-native-tcp-socket/issues/41)). However, the **methods whose interface differs from Node are marked in bold**.

### TcpSocket
* **Methods:**
* **[`TcpSocket.createConnection(options[, callback])`](#createconnection)**
* [`address()`](https://nodejs.org/api/net.html#net_socket_address)
* [`destroy([error])`](https://nodejs.org/api/net.html#net_socket_destroy_error)
* [`end([data][, encoding][, callback])`](https://nodejs.org/api/net.html#net_socket_end_data_encoding_callback)
* [`setEncoding([encoding])`](https://nodejs.org/api/net.html#net_socket_setencoding_encoding)
* [`setKeepAlive([enable][, initialDelay])`](https://nodejs.org/api/net.html#net_socket_setkeepalive_enable_initialdelay) - _`initialDelay` is ignored_
* [`setNoDelay([noDelay])`](https://nodejs.org/api/net.html#net_socket_setnodelay_nodelay)
* [`setTimeout(timeout[, callback])`](https://nodejs.org/api/net.html#net_socket_settimeout_timeout_callback)
* [`write(data[, encoding][, callback])`](https://nodejs.org/api/net.html#net_socket_write_data_encoding_callback)
* **Events:**
* [`'close'`](https://nodejs.org/api/net.html#net_event_close_1)
* [`'connect'`](https://nodejs.org/api/net.html#net_event_connect)
* [`'data'`](https://nodejs.org/api/net.html#net_event_data)
* [`'error'`](https://nodejs.org/api/net.html#net_event_error_1)

#### `createConnection()`
`createConnection(options[, callback])` creates a TCP connection using the given [`options`](#createconnection-options).
Expand Down
18 changes: 17 additions & 1 deletion src/TcpSocket.js
Original file line number Diff line number Diff line change
Expand Up @@ -42,6 +42,7 @@ export default class TcpSocket extends EventEmitter {
this._timeout = undefined;
/** @type {number} */
this._state = STATE.DISCONNECTED;
this._encoding = undefined;
this._registerEvents();
if (address != undefined) this._setConnected(address);
}
Expand All @@ -54,7 +55,8 @@ export default class TcpSocket extends EventEmitter {
this._dataListener = this._eventEmitter.addListener('data', (evt) => {
if (evt.id !== this._id) return;
const bufferTest = Buffer.from(evt.data, 'base64');
this.emit('data', bufferTest);
const finalData = this._encoding ? bufferTest.toString(this._encoding) : bufferTest;
this.emit('data', finalData);
});
this._errorListener = this._eventEmitter.addListener('error', (evt) => {
if (evt.id !== this._id) return;
Expand Down Expand Up @@ -155,6 +157,20 @@ export default class TcpSocket extends EventEmitter {
}
}

/**
* Set the encoding for the socket as a Readable Stream. By default, no encoding is assigned and stream data will be returned as `Buffer` objects.
* Setting an encoding causes the stream data to be returned as strings of the specified encoding rather than as Buffer objects.
*
* For instance, calling `socket.setEncoding('utf8')` will cause the output data to be interpreted as UTF-8 data, and passed as strings.
* Calling `socket.setEncoding('hex')` will cause the data to be encoded in hexadecimal string format.
*
* @param {BufferEncoding} [encoding]
*/
setEncoding(encoding) {
this._encoding = encoding;
return this;
}

/**
* Enable/disable the use of Nagle's algorithm. When a TCP connection is created, it will have Nagle's algorithm enabled.
*
Expand Down