1.1.0 • Published 3 years ago

egg-udp v1.1.0

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

egg-udp

NPM version build status Known Vulnerabilities npm download

udp plugin for egg.

Install

$ npm i egg-udp --save

Usage

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

Configuration

// {app_root}/config/config.default.js
exports.udp = {
  port: 5000,
};

see config/config.default.js for more detail.

Example

Router example

// {app_root}/app/router.ts(or router.js)
app.udp.handle('proxy.handle');

Controller example

  • This is the example for javascript.

    // {app_root}/app/udp/controller/proxy.js
    module.exports = app => {
      return {
        async handle(udp) {
          udp.on('error', (err) => {
            console.log(`udp error:\n${err.stack}`);
          });
    
          udp.on('message', (msg, rinfo) => {
            console.log(`udp server got: ${msg.toString()} from ${rinfo.address}:${rinfo.port}`);
          });
    
          udp.on('listening', () => {
            const address = udp.address();
            console.log(`udp listening ${address.address}:${address.port}`);
          });
        },
      };
    };
  • This is the example for typescript.

    // {app_root}/app/udp/controller/proxy.ts
    import {  Application } from 'egg';
    import { Socket } from 'dgram';
    
    interface Rinfo {
      address: string,
      family: string,
      port: number,
      size: number,
    }
    
    export default (app: Application) => {
      return {
        async handle(udp: Socket) {
          udp.on('error', (err) => {
            console.log(`udp error:\n${err.stack}`);
          });
    
          udp.on('message', (msg, rinfo: Rinfo) => {
            console.log(`udp server got: ${msg.toString()} from ${rinfo.address}:${rinfo.port}`);
          });
    
          udp.on('listening', () => {
            const address = udp.address();
            console.log(`udp listening ${address.address}:${address.port}`);
          });
        },
      };
    };

Test

// Test udp server by Linux
$ echo "2019-05-20T15:30:57 testlog" | nc -u -w1 127.0.0.1 5000
// Check it in server log
udp server got: 2019-05-20T15:30:57 testlog from 127.0.0.1:5000

License

MIT

1.1.0

3 years ago

1.0.9

4 years ago

1.0.8

5 years ago

1.0.7

5 years ago

1.0.5

5 years ago

1.0.4

5 years ago

1.0.3

5 years ago

1.0.2

5 years ago

1.0.1

5 years ago

1.0.0

5 years ago