socketio封装

This commit is contained in:
robin
2022-05-31 06:52:00 +08:00
parent 96bfa3f2d3
commit c764e7097d
9 changed files with 77 additions and 4 deletions
+3
View File
@@ -0,0 +1,3 @@
module.exports = (eventName, callback)=>{
console.log('123321',eventName, callback);
}
+2 -2
View File
@@ -6,8 +6,8 @@ const config = require("./config")
const EventEmitter = require('events'); const EventEmitter = require('events');
module.exports = async (app) => { module.exports = async (app) => {
app.event = new EventEmitter() app.event = new EventEmitter()
const evList = await app.load(config.path) const list = await app.load(config.path)
evList.forEach(item => { list.forEach(item => {
Object.keys(item.res).forEach(key => { Object.keys(item.res).forEach(key => {
//载入所有事件 //载入所有事件
app.event.on(key, item.res[key]) app.event.on(key, item.res[key])
+3
View File
@@ -0,0 +1,3 @@
module.exports = {
path:"middleware/*.js"
}
+15
View File
@@ -0,0 +1,15 @@
/**
* 加载中间件
* 加载app/middleware文件夹下的中间件
*/
const config = require("./config")
module.exports = async (app) => {
let list = await app.load(config.path)
list = app.xe.orderBy(list, "res.sort")
list = list.filter(item => item.res.use)
list.forEach(item => {
app.use(async (ctx, next) => {
return await item.res.fun(ctx, next)
})
})
}
+3
View File
@@ -0,0 +1,3 @@
module.exports = {
path:"app/*/io/*.js",
}
+42
View File
@@ -0,0 +1,42 @@
/**
* socket.io
* 自动绑定io文件夹内js文件
* 参考文档:
* 1.https://socket.io/docs/v4/server-api
* 2.https://juejin.cn/post/6844903810050031630
*/
const config = require("./config")
const path = require('path')
module.exports = async (app) => {
const server = require('http').createServer(app.callback());
const io = require('socket.io')(server);
let list = await app.load(config.path)
app.io = io
//默认命名空间
io.on('connection', socket => {
socket.emit('open', "连接成功!");//通知客户端已连接
console.log('通知客户端已连接');
//监听disconnect事件
socket.on('disconnect', (eventName, callback) => {
console.log('断开')
})
for (let el of list) {
//空间名称
// const namespace = path.basename(path.resolve(el.parse.dir, '..'))
//事件名称
const onname = el.parse.name
socket.on(onname, el.res)
}
});
//覆盖启动方法
app.startServer = () => {
// 监听端口
app.startServer = server.listen(3000, () => {
console.log('listening on *:3000');
});
}
}
+6 -1
View File
@@ -1,6 +1,7 @@
const Koa = require("koa") const Koa = require("koa")
const glob = require("glob") const glob = require("glob")
const path = require('path') const path = require('path')
const xe = require("xe-utils")
/** /**
* bamboo 核心 * bamboo 核心
@@ -10,12 +11,12 @@ module.exports = class Bamboo extends Koa {
/*初始化业务目录*/ /*初始化业务目录*/
constructor(extend_directory, root) { constructor(extend_directory, root) {
super(); super();
this.xe = xe
this.root = this.isPath() || root; this.root = this.isPath() || root;
this.extend_directory = extend_directory || `extend/*/index.js` this.extend_directory = extend_directory || `extend/*/index.js`
console.log(`当前根目录${this.root}`); console.log(`当前根目录${this.root}`);
console.log(`当前插件目录${this.extend_directory}`); console.log(`当前插件目录${this.extend_directory}`);
this.loadEvent() this.loadEvent()
this.startServer()
} }
/*加载扩展库目录*/ /*加载扩展库目录*/
@@ -24,6 +25,7 @@ module.exports = class Bamboo extends Koa {
for (let listElement of list) { for (let listElement of list) {
await listElement.res(this) await listElement.res(this)
} }
this.startServer()
} }
/*当前根目录*/ /*当前根目录*/
@@ -55,8 +57,11 @@ module.exports = class Bamboo extends Koa {
}) })
} }
/*启动服务*/ /*启动服务*/
startServer(prod) { startServer(prod) {
this.listen(prod || 3000) this.listen(prod || 3000)
console.log('启动服务:3000');
} }
} }
+2 -1
View File
@@ -10,6 +10,7 @@
"license": "ISC", "license": "ISC",
"dependencies": { "dependencies": {
"glob": "^8.0.3", "glob": "^8.0.3",
"koa": "^2.13.4" "koa": "^2.13.4",
"xe-utils": "^3.5.4"
} }
} }
+1
View File
@@ -63,6 +63,7 @@
"require-directory": "^2.1.1", "require-directory": "^2.1.1",
"sequelize": "^6.18.0", "sequelize": "^6.18.0",
"shelljs": "^0.8.5", "shelljs": "^0.8.5",
"socket.io": "^4.5.1",
"sqlite3": "^5.0.2", "sqlite3": "^5.0.2",
"webpack": "^4.41.5", "webpack": "^4.41.5",
"webpack-cli": "3.3.10", "webpack-cli": "3.3.10",