$ npm i egg-tcp --save
or
$ yarn add egg-tcp
// {app_root}/config/plugin.js
exports.tcp = {
enable: true,
package: 'egg-tcp',
};
// {app_root}/config/config.default.js
exports.tcp = {
port: 5001,
host: '127.0.0.1'
};
// {app_root}/app/tcp/controller/index.js
module.exports = app => {
return {
async feed(socket) {
console.log('A new connection has been established.');
// Now that a TCP connection has been established, the server can send data to
// the client by writing to its socket.
socket.write('Hello, client.');
// The server can also receive data from the client by reading from its socket.
socket.on('data', function (chunk) {
console.log(`Data received from client: ${chunk.toString()}.`);
});
// When the client requests to end the TCP connection with the server, the server
// ends the connection.
socket.on('end', function () {
console.log('Closing connection with the client');
});
// Don't forget to catch error, for your own sake.
socket.on('error', function (err) {
console.log(`Error: ${err}`);
});
}
};
};
// {app_root}/app/router.js
app.tcp.handle('index.feed')
Please open an issue here.