Skip to content

Commit

Permalink
chore: Add an example with long data
Browse files Browse the repository at this point in the history
  • Loading branch information
Rapsssito committed Apr 18, 2022
1 parent e23f3d9 commit 57dc527
Show file tree
Hide file tree
Showing 3 changed files with 34 additions and 4 deletions.
5 changes: 3 additions & 2 deletions examples/tcpsockets/App.js
Original file line number Diff line number Diff line change
Expand Up @@ -24,6 +24,7 @@ class App extends React.Component {
* @param {string | Error} msg
*/
updateChatter(msg) {
console.log(msg);
this.setState({
// @ts-ignore
chatter: this.state.chatter.concat([msg]),
Expand All @@ -35,7 +36,7 @@ class App extends React.Component {
this.updateChatter('Client connected to server on ' + JSON.stringify(socket.address()));

socket.on('data', (data) => {
this.updateChatter('Server client received: ' + data);
this.updateChatter('Server client received: ' + (data.length < 500 ? data : data.length + ' bytes'));
});

socket.on('error', (error) => {
Expand Down Expand Up @@ -65,7 +66,7 @@ class App extends React.Component {
});

client.on('data', (data) => {
this.updateChatter('Client received: ' + data);
this.updateChatter('Client received: ' + (data.length < 500 ? data : data.length + ' bytes'));
});

client.on('error', (error) => {
Expand Down
29 changes: 29 additions & 0 deletions examples/tcpsockets/examples/long-data.js
Original file line number Diff line number Diff line change
@@ -0,0 +1,29 @@
const net = require('net');

const server = new net.Server();
const client = new net.Socket();

const hugeData = 'x'.repeat(5*1024*1024)

function init() {
server.listen({ port: 0, host: '127.0.0.1', reuseAddress: true }, () => {
const port = server.address()?.port;
if (!port) throw new Error('Server port not found');
client.connect(
{
port: port,
host: '127.0.0.1',
localAddress: '127.0.0.1',
reuseAddress: true,
// localPort: 20000,
// interface: "wifi",
// tls: true
},
() => {
client.end(hugeData, 'utf8');
}
);
});
}

module.exports = { init, server, client };
4 changes: 2 additions & 2 deletions examples/tcpsockets/examples/main.js
Original file line number Diff line number Diff line change
Expand Up @@ -5,7 +5,7 @@ server.on('connection', (socket) => {
console.log('Client connected to server on ' + JSON.stringify(socket.address()));

socket.on('data', (data) => {
console.log('Server client received: ' + data);
console.log('Server client received: ' + (data.length < 500 ? data : data.length + ' bytes'));
});

socket.on('error', (error) => {
Expand Down Expand Up @@ -34,7 +34,7 @@ client.on('drain', () => {
});

client.on('data', (data) => {
console.log('Client received: ' + data);
console.log('Client received: ' + (data.length < 500 ? data : data.length + ' bytes'));
});

client.on('error', (error) => {
Expand Down

0 comments on commit 57dc527

Please sign in to comment.