0.1.2 • Published 5 years ago

egg-tcp v0.1.2

Weekly downloads
3
License
MIT
Repository
github
Last release
5 years ago

egg-tcp

NPM version

Install

$ npm i egg-tcp --save

or

$ yarn add egg-tcp

Usage

// {app_root}/config/plugin.js
exports.tcp = {
    enable: true,
    package: 'egg-tcp',
};

Configuration

// {app_root}/config/config.default.js
exports.tcp = {
    port: 5001,
    host: '127.0.0.1'
};

Example

// {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.ts
app.tcp.handle('index.feed')

Questions & Suggestions

Please open an issue here.

Thanks

egg-udp

License

MIT