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'; import * as server from './server.js';
console.log(server) console.log(server)
server.default( server.recv(
(res) => { (res) => {
console.log(`callback: ${res}`); console.log(`callback: ${res}`);
}, },

View file

@ -1,21 +1,36 @@
import * as net from 'net'; import * as net from 'net';
const hostname = '0.0.0.0'; 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(); let server = new net.Server();
server.listen(port, hostname, () => { server.listen(port, hostname, () => {
server.on('connect', ()=>{ server.on('connection', (con) => {
console.log('connection recieved') console.log('connection recieved: ' +
}) con.remoteAddress + ":" + con.remotePort);
server.on('data', (data) => { con.on('data', (data) => {
callback(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.on('error', (e) => {
server.close(); server.close();
errorCallback(e); errorCallback(e);
}); });
});
} }