From fc94a6ab5555c598b6c8a46aa2eb9d965fefcfc4 Mon Sep 17 00:00:00 2001 From: ngot Date: Fri, 10 Feb 2017 19:19:58 +0800 Subject: [PATCH] init --- .autod.conf.js | 26 +++++++ .eslintignore | 2 + .eslintrc | 3 + .gitignore | 9 +++ .travis.yml | 11 +++ LICENSE | 21 +++++ README.md | 61 +++++++++++++++ README.zh_CN.md | 76 +++++++++++++++++++ app.js | 18 +++++ appveyor.yml | 15 ++++ config/config.default.js | 13 ++++ package.json | 62 +++++++++++++++ .../apps/websocket-test/app/router.js | 7 ++ .../fixtures/apps/websocket-test/package.json | 4 + test/websocket.test.js | 24 ++++++ 15 files changed, 352 insertions(+) create mode 100644 .autod.conf.js create mode 100644 .eslintignore create mode 100644 .eslintrc create mode 100644 .gitignore create mode 100644 .travis.yml create mode 100644 LICENSE create mode 100644 README.md create mode 100644 README.zh_CN.md create mode 100644 app.js create mode 100644 appveyor.yml create mode 100644 config/config.default.js create mode 100644 package.json create mode 100644 test/fixtures/apps/websocket-test/app/router.js create mode 100644 test/fixtures/apps/websocket-test/package.json create mode 100644 test/websocket.test.js diff --git a/.autod.conf.js b/.autod.conf.js new file mode 100644 index 0000000..0b4e25f --- /dev/null +++ b/.autod.conf.js @@ -0,0 +1,26 @@ +'use strict'; + +module.exports = { + write: true, + prefix: '^', + test: [ + 'test', + 'benchmark', + ], + devdep: [ + 'egg', + 'egg-ci', + 'egg-bin', + 'autod', + 'eslint', + 'eslint-config-egg', + 'supertest', + 'webstorm-disable-index', + ], + exclude: [ + './test/fixtures', + './docs', + './coverage', + ], + registry: 'https://r.cnpmjs.org', +}; diff --git a/.eslintignore b/.eslintignore new file mode 100644 index 0000000..a24e501 --- /dev/null +++ b/.eslintignore @@ -0,0 +1,2 @@ +test/fixtures +coverage diff --git a/.eslintrc b/.eslintrc new file mode 100644 index 0000000..c799fe5 --- /dev/null +++ b/.eslintrc @@ -0,0 +1,3 @@ +{ + "extends": "eslint-config-egg" +} diff --git a/.gitignore b/.gitignore new file mode 100644 index 0000000..a528ca9 --- /dev/null +++ b/.gitignore @@ -0,0 +1,9 @@ +logs/ +npm-debug.log +node_modules/ +coverage/ +.idea/ +.vscode/ +run/ +.DS_Store +*.swp diff --git a/.travis.yml b/.travis.yml new file mode 100644 index 0000000..5e7a967 --- /dev/null +++ b/.travis.yml @@ -0,0 +1,11 @@ +sudo: false +language: node_js +node_js: + - '6' + - '7' +install: + - npm i npminstall && npminstall +script: + - npm run ci +after_script: + - npminstall codecov && codecov diff --git a/LICENSE b/LICENSE new file mode 100644 index 0000000..8363a1e --- /dev/null +++ b/LICENSE @@ -0,0 +1,21 @@ +The MIT License (MIT) + +Copyright (c) Alibaba Group Holding Limited and other contributors. + +Permission is hereby granted, free of charge, to any person obtaining a copy +of this software and associated documentation files (the "Software"), to deal +in the Software without restriction, including without limitation the rights +to use, copy, modify, merge, publish, distribute, sublicense, and/or sell +copies of the Software, and to permit persons to whom the Software is +furnished to do so, subject to the following conditions: + +The above copyright notice and this permission notice shall be included in all +copies or substantial portions of the Software. + +THE SOFTWARE IS PROVIDED "AS IS", WITHOUT WARRANTY OF ANY KIND, EXPRESS OR +IMPLIED, INCLUDING BUT NOT LIMITED TO THE WARRANTIES OF MERCHANTABILITY, +FITNESS FOR A PARTICULAR PURPOSE AND NONINFRINGEMENT. IN NO EVENT SHALL THE +AUTHORS OR COPYRIGHT HOLDERS BE LIABLE FOR ANY CLAIM, DAMAGES OR OTHER +LIABILITY, WHETHER IN AN ACTION OF CONTRACT, TORT OR OTHERWISE, ARISING FROM, +OUT OF OR IN CONNECTION WITH THE SOFTWARE OR THE USE OR OTHER DEALINGS IN THE +SOFTWARE. diff --git a/README.md b/README.md new file mode 100644 index 0000000..e15e982 --- /dev/null +++ b/README.md @@ -0,0 +1,61 @@ +# egg-socket.io + +[![NPM version][npm-image]][npm-url] +[![build status][travis-image]][travis-url] +[![Test coverage][codecov-image]][codecov-url] +[![David deps][david-image]][david-url] +[![Known Vulnerabilities][snyk-image]][snyk-url] +[![npm download][download-image]][download-url] + +[npm-image]: https://img.shields.io/npm/v/egg-socket.io.svg?style=flat-square +[npm-url]: https://npmjs.org/package/egg-socket.io +[travis-image]: https://img.shields.io/travis/eggjs/egg-socket.io.svg?style=flat-square +[travis-url]: https://travis-ci.org/eggjs/egg-socket.io +[codecov-image]: https://img.shields.io/codecov/c/github/eggjs/egg-socket.io.svg?style=flat-square +[codecov-url]: https://codecov.io/github/eggjs/egg-socket.io?branch=master +[david-image]: https://img.shields.io/david/eggjs/egg-socket.io.svg?style=flat-square +[david-url]: https://david-dm.org/eggjs/egg-socket.io +[snyk-image]: https://snyk.io/test/npm/egg-socket.io/badge.svg?style=flat-square +[snyk-url]: https://snyk.io/test/npm/egg-socket.io +[download-image]: https://img.shields.io/npm/dm/egg-socket.io.svg?style=flat-square +[download-url]: https://npmjs.org/package/egg-socket.io + +Still Work in Process. + +## Install + +```bash +$ npm i egg-socket.io --save +``` + +## Usage + +```js +// {app_root}/config/plugin.js +exports.websocket = { + enable: true, + package: 'egg-socket.io', +}; +``` + +## Configuration + +```js +// {app_root}/config/config.default.js +exports.websocket = { +}; +``` + +see [config/config.default.js](config/config.default.js) for more detail. + +## Example + + + +## Questions & Suggestions + +Please open an issue [here](https://github.com/eggjs/egg/issues). + +## License + +[MIT](LICENSE) diff --git a/README.zh_CN.md b/README.zh_CN.md new file mode 100644 index 0000000..dbd2328 --- /dev/null +++ b/README.zh_CN.md @@ -0,0 +1,76 @@ +# egg-socket.io + +[![NPM version][npm-image]][npm-url] +[![build status][travis-image]][travis-url] +[![Test coverage][codecov-image]][codecov-url] +[![David deps][david-image]][david-url] +[![Known Vulnerabilities][snyk-image]][snyk-url] +[![npm download][download-image]][download-url] + +[npm-image]: https://img.shields.io/npm/v/egg-socket.io.svg?style=flat-square +[npm-url]: https://npmjs.org/package/egg-socket.io +[travis-image]: https://img.shields.io/travis/eggjs/egg-socket.io.svg?style=flat-square +[travis-url]: https://travis-ci.org/eggjs/egg-socket.io +[codecov-image]: https://img.shields.io/codecov/c/github/eggjs/egg-socket.io.svg?style=flat-square +[codecov-url]: https://codecov.io/github/eggjs/egg-socket.io?branch=master +[david-image]: https://img.shields.io/david/eggjs/egg-socket.io.svg?style=flat-square +[david-url]: https://david-dm.org/eggjs/egg-socket.io +[snyk-image]: https://snyk.io/test/npm/egg-socket.io/badge.svg?style=flat-square +[snyk-url]: https://snyk.io/test/npm/egg-socket.io +[download-image]: https://img.shields.io/npm/dm/egg-socket.io.svg?style=flat-square +[download-url]: https://npmjs.org/package/egg-socket.io + + + +## 依赖说明 + +### 依赖的 egg 版本 + +egg-socket.io 版本 | egg 1.x +--- | --- +1.x | 😁 +0.x | ❌ + +### 依赖的插件 + + +## 开启插件 + +```js +// config/plugin.js +exports['socket.io'] = { + enable: true, + package: 'egg-socket.io', +}; +``` + +## 使用场景 + +- Why and What: 描述为什么会有这个插件,它主要在完成一件什么事情。 +尽可能描述详细。 +- How: 描述这个插件是怎样使用的,具体的示例代码,甚至提供一个完整的示例,并给出链接。 + +## 详细配置 + +请到 [config/config.default.js](config/config.default.js) 查看详细配置项说明。 + +## 单元测试 + + + +## 提问交流 + +请到 [egg issues](https://github.com/eggjs/egg/issues) 异步交流。 + +## License + +[MIT](LICENSE) diff --git a/app.js b/app.js new file mode 100644 index 0000000..6d0ee99 --- /dev/null +++ b/app.js @@ -0,0 +1,18 @@ +'use strict'; + +const sio = require('socket.io'); +const sio_redis = require('socket.io-redis'); + +module.exports = app => { + const done = app.readyCallback('egg-websocket'); + + app.on('server', server => { + const io = sio(server); + io.adapter(sio_redis({ + host: app.config['socket.io'].redis.host, + port: app.config['socket.io'].redis.port, + })); + app['socket.io'] = io; + done(); + }); +}; diff --git a/appveyor.yml b/appveyor.yml new file mode 100644 index 0000000..ec7400e --- /dev/null +++ b/appveyor.yml @@ -0,0 +1,15 @@ +environment: + matrix: + - nodejs_version: '6' + - nodejs_version: '7' + +install: + - ps: Install-Product node $env:nodejs_version + - npm i npminstall && node_modules\.bin\npminstall + +test_script: + - node --version + - npm --version + - npm run ci + +build: off diff --git a/config/config.default.js b/config/config.default.js new file mode 100644 index 0000000..79577bb --- /dev/null +++ b/config/config.default.js @@ -0,0 +1,13 @@ +'use strict'; + +/** + * websocket default config + * @member Config#websocket + * @property {String} SOME_KEY - some description + */ +exports['socket.io'] = { + redis: { + host: '127.0.0.1', + port: 6379, + }, +}; diff --git a/package.json b/package.json new file mode 100644 index 0000000..5c8fbe2 --- /dev/null +++ b/package.json @@ -0,0 +1,62 @@ +{ + "name": "egg-socket.io", + "version": "0.0.1", + "description": "egg plugin for socket.io", + "eggPlugin": { + "name": "socket.io" + }, + "keywords": [ + "egg", + "eggPlugin", + "egg-plugin", + "socket.io", + "websocket" + ], + "dependencies": { + "socket.io": "^1.7.2", + "socket.io-redis": "^3.1.0" + }, + "devDependencies": { + "autod": "^2.7.1", + "egg": "^0.7.0", + "egg-bin": "^1.10.0", + "egg-ci": "^1.1.0", + "egg-mock": "^2.0.0", + "eslint": "^3.13.1", + "eslint-config-egg": "^3.2.0", + "supertest": "^2.0.1", + "webstorm-disable-index": "^1.1.2" + }, + "engines": { + "node": ">=6.0.0" + }, + "scripts": { + "test": "npm run lint -- --fix && npm run test-local", + "test-local": "egg-bin test", + "cov": "egg-bin cov", + "lint": "eslint .", + "ci": "npm run lint && npm run cov", + "autod": "autod" + }, + "files": [ + "index.js", + "app.js", + "agent.js", + "config", + "app", + "lib" + ], + "ci": { + "version": "6, 7" + }, + "repository": { + "type": "git", + "url": "git+https://github.com/eggjs/egg-socket.io.git" + }, + "bugs": { + "url": "https://github.com/eggjs/egg/issues" + }, + "homepage": "https://github.com/eggjs/egg-socket.io#readme", + "author": "ngot ", + "license": "MIT" +} diff --git a/test/fixtures/apps/websocket-test/app/router.js b/test/fixtures/apps/websocket-test/app/router.js new file mode 100644 index 0000000..291ccf9 --- /dev/null +++ b/test/fixtures/apps/websocket-test/app/router.js @@ -0,0 +1,7 @@ +'use strict'; + +module.exports = app => { + app.get('/', function* () { + this.body = 'hi, ' + app.plugins['socket.io'].name; + }); +}; diff --git a/test/fixtures/apps/websocket-test/package.json b/test/fixtures/apps/websocket-test/package.json new file mode 100644 index 0000000..2f8b3d2 --- /dev/null +++ b/test/fixtures/apps/websocket-test/package.json @@ -0,0 +1,4 @@ +{ + "name": "socket.io-test", + "version": "0.0.1" +} \ No newline at end of file diff --git a/test/websocket.test.js b/test/websocket.test.js new file mode 100644 index 0000000..b384f3d --- /dev/null +++ b/test/websocket.test.js @@ -0,0 +1,24 @@ +'use strict'; + +const request = require('supertest'); +const mm = require('egg-mock'); + +describe('test/socketio.test.js', () => { + let app; + before(() => { + app = mm.app({ + baseDir: 'apps/socketio-test', + }); + return app.ready(); + }); + + after(() => app.close()); + afterEach(mm.restore); + + it('should GET /', () => { + return request(app.callback()) + .get('/') + .expect('hi, websocket') + .expect(200); + }); +});