Skip to content

Commit bfa80ea

Browse files
authored
feat: Add socket.setEncoding() method (#74)
1 parent 466f866 commit bfa80ea

File tree

2 files changed

+24
-2
lines changed

2 files changed

+24
-2
lines changed

README.md

Lines changed: 7 additions & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -179,18 +179,24 @@ const client = TcpSocket.createConnection({
179179
_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)._
180180

181181
## API
182-
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**.
182+
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**.
183183

184184
### TcpSocket
185185
* **Methods:**
186186
* **[`TcpSocket.createConnection(options[, callback])`](#createconnection)**
187187
* [`address()`](https://nodejs.org/api/net.html#net_socket_address)
188188
* [`destroy([error])`](https://nodejs.org/api/net.html#net_socket_destroy_error)
189189
* [`end([data][, encoding][, callback])`](https://nodejs.org/api/net.html#net_socket_end_data_encoding_callback)
190+
* [`setEncoding([encoding])`](https://nodejs.org/api/net.html#net_socket_setencoding_encoding)
190191
* [`setKeepAlive([enable][, initialDelay])`](https://nodejs.org/api/net.html#net_socket_setkeepalive_enable_initialdelay) - _`initialDelay` is ignored_
191192
* [`setNoDelay([noDelay])`](https://nodejs.org/api/net.html#net_socket_setnodelay_nodelay)
192193
* [`setTimeout(timeout[, callback])`](https://nodejs.org/api/net.html#net_socket_settimeout_timeout_callback)
193194
* [`write(data[, encoding][, callback])`](https://nodejs.org/api/net.html#net_socket_write_data_encoding_callback)
195+
* **Events:**
196+
* [`'close'`](https://nodejs.org/api/net.html#net_event_close_1)
197+
* [`'connect'`](https://nodejs.org/api/net.html#net_event_connect)
198+
* [`'data'`](https://nodejs.org/api/net.html#net_event_data)
199+
* [`'error'`](https://nodejs.org/api/net.html#net_event_error_1)
194200

195201
#### `createConnection()`
196202
`createConnection(options[, callback])` creates a TCP connection using the given [`options`](#createconnection-options).

src/TcpSocket.js

Lines changed: 17 additions & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -42,6 +42,7 @@ export default class TcpSocket extends EventEmitter {
4242
this._timeout = undefined;
4343
/** @type {number} */
4444
this._state = STATE.DISCONNECTED;
45+
this._encoding = undefined;
4546
this._registerEvents();
4647
if (address != undefined) this._setConnected(address);
4748
}
@@ -54,7 +55,8 @@ export default class TcpSocket extends EventEmitter {
5455
this._dataListener = this._eventEmitter.addListener('data', (evt) => {
5556
if (evt.id !== this._id) return;
5657
const bufferTest = Buffer.from(evt.data, 'base64');
57-
this.emit('data', bufferTest);
58+
const finalData = this._encoding ? bufferTest.toString(this._encoding) : bufferTest;
59+
this.emit('data', finalData);
5860
});
5961
this._errorListener = this._eventEmitter.addListener('error', (evt) => {
6062
if (evt.id !== this._id) return;
@@ -155,6 +157,20 @@ export default class TcpSocket extends EventEmitter {
155157
}
156158
}
157159

160+
/**
161+
* 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.
162+
* Setting an encoding causes the stream data to be returned as strings of the specified encoding rather than as Buffer objects.
163+
*
164+
* For instance, calling `socket.setEncoding('utf8')` will cause the output data to be interpreted as UTF-8 data, and passed as strings.
165+
* Calling `socket.setEncoding('hex')` will cause the data to be encoded in hexadecimal string format.
166+
*
167+
* @param {BufferEncoding} [encoding]
168+
*/
169+
setEncoding(encoding) {
170+
this._encoding = encoding;
171+
return this;
172+
}
173+
158174
/**
159175
* Enable/disable the use of Nagle's algorithm. When a TCP connection is created, it will have Nagle's algorithm enabled.
160176
*

0 commit comments

Comments
 (0)