Skip to content

Commit

Permalink
fix: egg socket io missing session middleware (#835)
Browse files Browse the repository at this point in the history
  • Loading branch information
czy88840616 authored Feb 2, 2021
1 parent 21c78c2 commit 6e605a1
Show file tree
Hide file tree
Showing 11 changed files with 86 additions and 1 deletion.
2 changes: 1 addition & 1 deletion packages/typegoose/package.json
Original file line number Diff line number Diff line change
Expand Up @@ -18,7 +18,7 @@
],
"license": "MIT",
"devDependencies": {
"@midwayjs/cli": "^1.2.38",
"@midwayjs/cli": "^1.2.36",
"@midwayjs/koa": "^2.7.2",
"@midwayjs/mock": "^2.7.2",
"@typegoose/typegoose": "^7.4.7",
Expand Down
6 changes: 6 additions & 0 deletions packages/web/app.js
Original file line number Diff line number Diff line change
Expand Up @@ -18,6 +18,12 @@ class AppBootHook {
}

async didLoad() {
if (this.app.loader['useEggSocketIO']) {
// socketio 下会提前加入 session 中间件,这里删除,防止重复加载
if (this.app.middleware.length && this.app.middleware[this.app.middleware.length - 1]._name === 'session') {
this.app.middleware.pop();
}
}
const middlewareNames = this.coreMiddleware.concat(this.appMiddleware);
// 等 midway 加载完成后,再去 use 中间件
for (const name of middlewareNames) {
Expand Down
1 change: 1 addition & 0 deletions packages/web/package.json
Original file line number Diff line number Diff line change
Expand Up @@ -31,6 +31,7 @@
"@midwayjs/mock": "^2.7.2",
"dayjs": "^1.10.4",
"egg-view-nunjucks": "^2.2.0",
"egg-socket.io": "^4.1.6",
"fake-egg": "1.0.0",
"fs-extra": "^8.0.1",
"pedding": "^1.1.0",
Expand Down
14 changes: 14 additions & 0 deletions packages/web/src/base.ts
Original file line number Diff line number Diff line change
Expand Up @@ -37,6 +37,7 @@ export const createAppWorkerLoader = () => {
app: any;
framework;
bootstrap;
useEggSocketIO = false;

getEggPaths() {
if (!this.appDir) {
Expand Down Expand Up @@ -130,6 +131,19 @@ export const createAppWorkerLoader = () => {
await this.framework.loadLifeCycles();
});
}

loadMiddleware() {
super.loadMiddleware();
if (this.plugins['io']) {
this.useEggSocketIO = true;
const sessionMiddleware = this.app.middlewares['session'](
this.app.config['session'],
this.app
);
sessionMiddleware._name = 'session';
this.app.use(sessionMiddleware);
}
}
}

return EggAppWorkerLoader as any;
Expand Down
Original file line number Diff line number Diff line change
@@ -0,0 +1,3 @@
{
"name": "ali-demo"
}
Original file line number Diff line number Diff line change
@@ -0,0 +1,26 @@
import { EggAppConfig, EggAppInfo, PowerPartial } from 'egg';

export type DefaultConfig = PowerPartial<EggAppConfig>;

export default (appInfo: EggAppInfo) => {
const config = {} as DefaultConfig;

// use for cookie sign key, should change to your own and keep security
config.keys = appInfo.name + '_1611657289809_5801';

// add your config here
config.middleware = [];

// socket.io配置
config.io = {
init: {}, // passed to engine.io
namespace: {
'/ws': {
connectionMiddleware: [],
packetMiddleware: [],
},
},
};

return config;
};
Empty file.
Original file line number Diff line number Diff line change
@@ -0,0 +1,12 @@
import { EggPlugin } from 'egg';

// 启用socketio
const io = {
enable: true,
package: 'egg-socket.io',
};

export default {
static: false, // default is true
io,
} as EggPlugin;
Original file line number Diff line number Diff line change
@@ -0,0 +1,5 @@
import { Configuration } from '@midwayjs/decorator';

@Configuration()
export class ContainerConfiguration {
}
Original file line number Diff line number Diff line change
@@ -0,0 +1,10 @@
import { Controller, Get, Provide } from '@midwayjs/decorator';

@Provide()
@Controller('/')
export class HomeController {
@Get('/')
async home() {
return 'Hello Midwayjs!';
}
}
8 changes: 8 additions & 0 deletions packages/web/test/issue.test.ts
Original file line number Diff line number Diff line change
Expand Up @@ -67,5 +67,13 @@ describe('/test/issue.test.ts', () => {
expect(result.text).toEqual('hello world');
await closeApp(app);
});

it('test #825 issue load socket io plugin error in egg.js', async () => {
const app = await creatApp('issue/base-app-socketio');
let result = await createHttpRequest(app).get('/');
expect(result.status).toEqual(200);
expect(result.text).toEqual('Hello Midwayjs!');
await closeApp(app);
});
});

0 comments on commit 6e605a1

Please sign in to comment.