egg-wsocket
v1.0.5
Published
Egg websocket plugin
Downloads
1
Maintainers
Readme
egg-wsocket
Install
$ npm i egg-wsocket --save
Usage
// {app_root}/config/plugin.js
exports.wsocket = {
enable: true,
package: 'egg-wsocket',
};
Configuration
// {app_root}/config/config.default.js
exports.wsocket = {
};
Router
// {app_root}/app/router.js
app.wsocket.route('/ws', app.controller.home.ws);
Controller
// {app_root}/controller/home.js
import { Controller } from 'egg';
export default class HomeController extends Controller {
async ws() {
const { ctx } = this;
if (!ctx.wsocket) {
throw new Error('this function can only be use in egg-wsocket router');
}
console.log('client connected');
ctx.wsocket
.on('message', (msg) => {
console.log('receive', msg);
})
.on('close', (code, reason) => {
console.log('websocket closed', code, reason);
});
}
}
see config/config.default.js for more detail.
Example
Questions & Suggestions
Please open an issue here.