Giter Site home page Giter Site logo

egg-udp's Introduction

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

egg-udp's People

Contributors

ruur avatar cnkoala avatar

Stargazers

一剑 avatar yunqi avatar  avatar Naeemo avatar ZCDC_Ren avatar

Watchers

James Cloos avatar Naeemo avatar

Forkers

cnkoala yamilemon

egg-udp's Issues

无法连接,启动报错

完全按照文档书写,但是程序启动后会报错,uDP没有连接成功
{ Error: write ENOTSUP
at ChildProcess.target._send (internal/child_process.js:741:20)
at ChildProcess.target.send (internal/child_process.js:625:19)
at sendHelper (internal/cluster/utils.js:25:15)
at send (internal/cluster/master.js:356:10)
at handle.add (internal/cluster/master.js:328:5)
at SharedHandle.add (internal/cluster/shared_handle.js:29:3)
at queryServer (internal/cluster/master.js:317:10)
at Worker.onmessage (internal/cluster/master.js:253:5)
at ChildProcess.onInternalMessage (internal/cluster/utils.js:42:8)
at ChildProcess.emit (events.js:187:15) errno: 'ENOTSUP', code: 'ENOTSUP', syscall: 'write' }
不知道是什么原因造成的,另外我不适用本插件,自己写udp连接,程序启动后调用方法启动udp连接也会报这个错误。

Recommend Projects

  • React photo React

    A declarative, efficient, and flexible JavaScript library for building user interfaces.

  • Vue.js photo Vue.js

    🖖 Vue.js is a progressive, incrementally-adoptable JavaScript framework for building UI on the web.

  • Typescript photo Typescript

    TypeScript is a superset of JavaScript that compiles to clean JavaScript output.

  • TensorFlow photo TensorFlow

    An Open Source Machine Learning Framework for Everyone

  • Django photo Django

    The Web framework for perfectionists with deadlines.

  • D3 photo D3

    Bring data to life with SVG, Canvas and HTML. 📊📈🎉

Recommend Topics

  • javascript

    JavaScript (JS) is a lightweight interpreted programming language with first-class functions.

  • web

    Some thing interesting about web. New door for the world.

  • server

    A server is a program made to process requests and deliver data to clients.

  • Machine learning

    Machine learning is a way of modeling and interpreting data that allows a piece of software to respond intelligently.

  • Game

    Some thing interesting about game, make everyone happy.

Recommend Org

  • Facebook photo Facebook

    We are working to build community through open source technology. NB: members must have two-factor auth.

  • Microsoft photo Microsoft

    Open source projects and samples from Microsoft.

  • Google photo Google

    Google ❤️ Open Source for everyone.

  • D3 photo D3

    Data-Driven Documents codes.