Skip to content
This repository has been archived by the owner on Aug 6, 2020. It is now read-only.

Naeemo/egg-tcp

Repository files navigation

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

Questions & Suggestions

Please open an issue here.

Thanks

egg-udp

License

MIT

About

Simple tcp plugin for eggjs

Resources

License

Stars

Watchers

Forks

Releases

No releases published

Packages

No packages published