Egg: 这个框架不适合udp开发吗

Created on 1 Aug 2019  ·  6Comments  ·  Source: eggjs/egg

What happens?

A clear and concise description of what the bug is.

Mini Showcase Repository(REQUIRED)

Provide a mini GitHub repository which can reproduce the issue.
Use npm init egg --type=simple bug then upload to your GitHub

How To Reproduce

Steps to reproduce the behavior:
1.
2.

Expected behavior
1.
2.

Context

  • Node Version:
  • Egg Version:
  • Plugin Name:
  • Plugin Version:
  • Platform:

Most helpful comment

@ruur @atian25 有个“取巧”的办法。koa源代码 application类里有个handleRequest方法。egg application类也重写了他

// koa监听到http请求就会调用该方法,调用中间件等
handleRequest(ctx, fnMiddleware) {
    const res = ctx.res;
    res.statusCode = 404;
    const onerror = err => ctx.onerror(err);
    const handleResponse = () => respond(ctx);
    onFinished(res, onerror);
    return fnMiddleware(ctx).then(handleResponse).catch(onerror);
  }

监听udp 对每条数据/请求 封装个req 大致代码:

        const fn = compose(this.app.middleware);
        const ctx = this.app.createAnonymousContext();
        const req = {
          url: udp.msg,
          _parsedUrl: url.parse(udp.msg),
          headers: {},
          method: 'POST',
          socket: {
            remoteAddress: '127.0.0.1',
          },
        };
        ctx.request.req = req;
        this.app.handleRequest(ctx, fn);
      }

能实现类似http请求... @atian25 req/res对齐就是上面这样吗

All 6 comments

@grutty-sf egg-udp 刚写了个

@grutty-sf egg-udp 刚写了个
我的想法是把udp封装成request 创建个context 这样可以用中间件形式

@grutty-sf 这样的话改动就有点大了 本身的结构是针对的http协议 我这个plugin都是避开了使用原有的一些注入 个人认为 要实现你这个 你得把框架实现的那套整体重写吧

Koa 本身是基于 Node 的 http.Server 的。

如果你要支持其他协议,就需要在前面去把 context 包括 req/res 对齐。

@ruur @atian25 有个“取巧”的办法。koa源代码 application类里有个handleRequest方法。egg application类也重写了他

// koa监听到http请求就会调用该方法,调用中间件等
handleRequest(ctx, fnMiddleware) {
    const res = ctx.res;
    res.statusCode = 404;
    const onerror = err => ctx.onerror(err);
    const handleResponse = () => respond(ctx);
    onFinished(res, onerror);
    return fnMiddleware(ctx).then(handleResponse).catch(onerror);
  }

监听udp 对每条数据/请求 封装个req 大致代码:

        const fn = compose(this.app.middleware);
        const ctx = this.app.createAnonymousContext();
        const req = {
          url: udp.msg,
          _parsedUrl: url.parse(udp.msg),
          headers: {},
          method: 'POST',
          socket: {
            remoteAddress: '127.0.0.1',
          },
        };
        ctx.request.req = req;
        this.app.handleRequest(ctx, fn);
      }

能实现类似http请求... @atian25 req/res对齐就是上面这样吗

我上面说的大致意思就是这样,但还需要加上 app/extend/request.js / app/extend/response.js / app/extend/context.js 去把这三者的一些 API 对齐了。

Was this page helpful?
0 / 5 - 0 ratings

Related issues

popomore picture popomore  ·  38Comments

killagu picture killagu  ·  48Comments

itsky365 picture itsky365  ·  62Comments

atian25 picture atian25  ·  68Comments

kkys4bfgp75be9p picture kkys4bfgp75be9p  ·  36Comments