1.0.5 • Published 4 years ago

egg-wsocket v1.0.5

Weekly downloads
1
License
MIT
Repository
github
Last release
4 years ago

egg-wsocket

NPM version build status Test coverage David deps Known Vulnerabilities npm download

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.

License

MIT

1.0.5

4 years ago

1.0.2

4 years ago

1.0.4

4 years ago

1.0.3

4 years ago

1.0.1

4 years ago

1.0.0

4 years ago