diff --git a/README.md b/README.md index cbf8ce1..34def81 100644 --- a/README.md +++ b/README.md @@ -179,7 +179,7 @@ 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:** @@ -187,10 +187,16 @@ Here are listed all methods implemented in `react-native-tcp-socket`, their func * [`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). diff --git a/src/TcpSocket.js b/src/TcpSocket.js index aa0a206..75c0599 100644 --- a/src/TcpSocket.js +++ b/src/TcpSocket.js @@ -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); } @@ -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; @@ -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. *