fix the server part

This commit is contained in:
jane 2021-05-25 20:32:56 -04:00
parent 0f45c368e7
commit 2ef6f3962b
2 changed files with 31 additions and 16 deletions

View file

@ -1,7 +1,7 @@
import * as server from './server.js';
console.log(server)
server.default(
server.recv(
(res) => {
console.log(`callback: ${res}`);
},

View file

@ -1,21 +1,36 @@
import * as net from 'net';
const hostname = '0.0.0.0';
const port = 29997;
const port = 29999;
export default function recv(callback, errorCallback) {
export function recv(callback, errorCallback) {
let server = new net.Server();
server.listen(port, hostname, () => {
server.on('connect', ()=>{
console.log('connection recieved')
})
server.on('data', (data) => {
callback(data)
})
server.on('connection', (con) => {
console.log('connection recieved: ' +
con.remoteAddress + ":" + con.remotePort);
con.on('data', (data) => {
callback(data);
});
con.on('close', () => {
console.log('recieved close for ' +
con.remoteAddress + ":" + con.remotePort);
con.destroy();
});
server.getConnections((err, cons) => {
if (err) {
console.error(err);
}
else {
console.log(`connections: ${cons}`);
}
})
})
server.on('error', (e) => {
server.close();
errorCallback(e);
});
});
}