eggjs / egg

🥚 Born to build better enterprise frameworks and apps with Node.js & Koa
https://eggjs.org
MIT License
18.88k stars 1.81k forks source link

[RFC] egg-socket.io #269

Closed atian25 closed 7 years ago

atian25 commented 7 years ago

Directory Structure

app
├── io
│   ├── controller
│   │   └── chat.js
│   └── middleware
│       ├── auth.js
│       ├── filter.js
├── router.js
config
 ├── config.default.js
 └── plugin.js

Configuration

// {app_root}/config/config.default.js
exports.io = {
  namespace: {
    '/': {
      connectionMiddleware: [],
      packetMiddleware: [],
    },
  },
  redis: {
    host: '127.0.0.1',
    port: 6379
  }
};

Middleware

middleware are functions which every connection or packet will be processed by.

Connection Middleware

config/config.default.js

exports.io = {
  namespace: {
    '/': {
      connectionMiddleware: ['auth'],
    },
  },
};

pay attention to the namespace, the config will only work for a specific namespace.

Packet Middleware

config/config.default.js

exports.io = {
  namespace: {
    '/': {
      packetMiddleware: ['filter'],
    },
  },
};

pay attention to the namespace, the config will only work for a specific namespace.

Controller

controller is designed to handle the emit event from the client.

example:

app/io/controller/chat.js

module.exports = app => {
  return function* () {
    const message = this.args[0];
    console.log(message);
    this.socket.emit('res', `Hi! I've got your message: ${message}`);
  };
};

next, config the router at app/router.js

module.exports = app => {
  // or app.io.of('/')
  app.io.route('chat', app.io.controllers.chat);
};
atian25 commented 7 years ago
exports.io = {
   middleware: {
      connection:  [ 'auth' ], // connection middleware
      packet: [ 'filter' ], // packet middleware
   }
};

这样怎么样?

另外, 这里会不会存在数组配置合并的问题?

dead-horse commented 7 years ago

这个只有应用会配,问题不大的

atian25 commented 7 years ago

cmiddleware 这种命名有点让人疑惑

dead-horse commented 7 years ago

connectionMiddlewarepacketMiddleware 吧,这种不要简写

atian25 commented 7 years ago

@ngot 把两者方案选型的考虑, 以及最终方案, 更新到顶楼吧.

ngot commented 7 years ago

嗯。我还在继续看,上下文,session那块,等完整了,更新到正文。

atian25 commented 7 years ago

@ngot 先更新一版吧,需要补全的地方,注明下。

等会我要发 egg-feed 了

luicfer commented 7 years ago

我觉得可以参考下sailsjs。那上面的websocket,我看着炒鸡爽

atian25 commented 7 years ago

@luicfer 提炼下写下提案?

ngot commented 7 years ago

我还在国外考驾照…下周发个版

ngot commented 7 years ago

https://github.com/eggjs/egg-socket.io/pull/1

atian25 commented 7 years ago

@ngot 提案也更新到顶楼吧

ngot commented 7 years ago

@atian25 done

ngot commented 7 years ago

发了 + egg-socket.io@1.0.0-beta.1 了。求试用建议@_@

atian25 commented 7 years ago

有没有写 examples/socket.io ? 这样试用方便点

ngot commented 7 years ago

@atian25 https://github.com/eggjs/egg-socket.io/tree/master/example 这里有

atian25 commented 7 years ago

好, 回头可以放到 https://github.com/eggjs/examples

ngot commented 7 years ago

已经发布 1.0.0