[RFC] egg-socket.io
See original GitHub issueDirectory 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
- Write your connection middleware
app/io/middleware/auth.js
module.exports = app => {
return function* (next) {
this.socket.emit('res', 'connected!');
yield* next;
// execute when disconnect.
console.log('disconnection!');
};
};
- then config this middleware to make it works.
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
- Write your packet middleware
app/io/middleware/filter.js
module.exports = app => {
return function* (next) {
this.socket.emit('res', 'packet received!');
console.log('packet:', this.packet);
yield* next;
};
};
- then config this middleware to make it works.
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);
};
Issue Analytics
- State:
- Created 7 years ago
- Comments:68 (59 by maintainers)
Top Results From Across the Web
Socket IO与NIO(二) - 掘金
是一个简单的面向数据报的传输层协议,正式规范为RFC 768。 ... vue-socket.io以及egg-socket.io简单示例WebSocket 是HTML5 开始提供的一种在单个TCP ...
Read more >Egg 期刊06 - VSCode 调试&& Alinode 接入 - 知乎专栏
对此,@天猪提出了一个RFC 提案,据说类库名有亮点,欢迎围观。 ... 文档地址:egg-socket.io 文档 。 感谢@苏依 同学的贡献。 Θ egg-schedule 优化.
Read more >Emit An Event To Socket Io In Jmeter - ADocLib
Writing a Socket.IO sampler in JMeter I was recently working on a project with a node.js server and talking to its client through...
Read more >socket.io连接https - CSDN博客
学习中,后台是express+nodejs,用到socket.io 做聊天功能。 ... 你就知道一些啦,当然有个最好的参考 文档就是HTTP相关的RFC文档,认真读一下肯定解决 ...
Read more >So what's up | Only Me and Me - WordPress.com
... .dennis.mars.tvs.francis.manga.silent.egg.socket.mhz.caps.literary. ... .sl.confidential.automobile.rfc.statutory.athens.northeast.isp.
Read more >Top Related Medium Post
No results found
Top Related StackOverflow Question
No results found
Troubleshoot Live Code
Lightrun enables developers to add logs, metrics and snapshots to live code - no restarts or redeploys required.
Start FreeTop Related Reddit Thread
No results found
Top Related Hackernoon Post
No results found
Top Related Tweet
No results found
Top Related Dev.to Post
No results found
Top Related Hashnode Post
No results found
Top GitHub Comments
如果不混部,为什么要强行在 egg 里面支持 socket.io,我们设计的这一套 web 开发相关的规范本来就美为 socket.io 考虑,等于是要再造一个基于 socket.io 的上层框架,我们本身也没有类似的强需求,不应该做到 egg 里面。
我的看法是我们主要解决的第二类问题。
很好啊。 可以在所有的npm包里都打上egg-前缀,多么美妙的事情啊。哈哈。