Przeglądaj źródła

Gitbook Auto Published

Willin Wang 8 lat temu
rodzic
commit
6336680e3a

+ 4 - 0
SUMMARY.md

@@ -7,6 +7,10 @@
   - [系统实践](design/system.md)
 - [项目](project/README.md)
   - [结构](project/structure.md)
+    - [HAPI](project/source/hapi.md)
+    - [Socket.IO](project/source/socketio.md)
+    - [UDP](project/source/udp.md)
+    - [Electron](project/source/electron.md)
   - JavaScript
     - [ES6/7](project/js/es6.md)
     - [Benchmark](project/js/benchmark.md)

+ 4 - 4
project/README.md

@@ -2,10 +2,10 @@
 
 人无远虑必有近忧。能在设计过程中解决掉的问题,绝对不要拖到实现过程中迭代。
 
+## 推荐配置
+
+参考: <https://github.com/w2fs/best-practice>
+
 ## 现有项目
 
 内部信息,登录Git访问: <https://code.aliyun.com/wulian/project>
-
-<script type="text/javascript">
-window.open('https://code.aliyun.com/wulian/project');
-</script>

+ 43 - 0
project/source/electron.md

@@ -0,0 +1,43 @@
+# Electron
+
+快速示例: <https://github.com/electron/electron-quick-start>
+
+## 打包工具
+
+electron-packager: <https://github.com/electron-userland/electron-packager>
+
+## Client
+
+### 目录结构
+
+```bash
+.
+├── app
+│   ├── app.js
+│   └── index.html
+├── main.js
+├── package.json
+└── src
+    ├── app.js
+    ├── components
+    │   ├── delete.js
+    │   ├── login.js
+    │   └── main.js
+    ├── index.less
+    ├── routes
+    │   └── index.js
+    ├── webpack.config.js
+    └── webpack.config.prod.js
+```
+
+### 运行脚本
+
+```json
+"scripts": {
+  "start": "./node_modules/.bin/webpack --config src/webpack.config.js && ./node_modules/.bin/electron main.js",
+  "test": "./node_modules/.bin/webpack --config src/webpack.config.js",
+  "pack-win": "./node_modules/.bin/electron-packager . --asar  --overwrite --platform=win32 --arch=ia32 --prune=true --out=out --version-string.CompanyName='GitHub, Inc.'  --ignore=node_modules",
+  "pack-mac": "./node_modules/.bin/electron-packager . --asar --overwrite --platform=darwin --arch=x64 --prune=true --out=out  --ignore=node_modules",
+  "pack-all": "./node_modules/.bin/electron-packager . --out=dist --prune --asar --overwrite --all"
+}
+```

+ 63 - 0
project/source/hapi.md

@@ -0,0 +1,63 @@
+# HAPI
+
+## Server
+
+```js
+import hapi from 'hapi';
+
+// Static File Server
+import inert from 'inert';
+// Render Views
+import vision from 'vision';
+
+const server = new hapi.Server();
+
+server.connection({
+  host: '127.0.0.1',
+  port: 4000,
+  router: {
+    stripTrailingSlash: true
+  }
+}, { timeout: { server: 5000, socket: 5000 } });
+
+// 根据需要注册插件
+server.register([inert, vision], () => {
+  server.start(() => {
+    console.log(`Server started at:  ${server.info.uri}`);
+  });
+});
+
+// Load Routes
+server.route(require('./routes'));
+
+// Error Response Handler
+server.ext('onPreResponse', (request, reply) => {
+  const response = request.response;
+  if (!response.isBoom) {
+    return reply.continue();
+  }
+  // return custom err result
+});
+
+// Add Templates Support with handlebars
+server.views({
+  path: `${__dirname}/lib/views`,
+  engines: { html: require('handlebars') },
+  partialsPath: `${__dirname}/lib/views/partials`,
+  isCached: false
+});
+
+module.exports = server;
+```
+
+## Plugins
+
+* 自动文档: <https://github.com/WulianCC/hapi-swagger>
+* 用户鉴权: <http://hapijs.com/api#serverauthapi>
+* 表单校验: <https://github.com/hapijs/joi>
+* HTTP错误: <https://github.com/hapijs/boom>
+* 渲染模板页面: <https://github.com/hapijs/vision>
+* 打印错误: <https://github.com/hapijs/good>
+* 静态文件引用: <https://github.com/hapijs/inert>
+
+其中,用户鉴权可参考: <https://code.aliyun.com/shgg/push/blob/master/lib/auth.js>

+ 35 - 0
project/source/socketio.md

@@ -0,0 +1,35 @@
+# Socket.IO
+
+<http://socket.io/>
+
+## Server
+
+应用场景:服务器间通信。
+
+```js
+const io = require('socket.io')().listen(6666);
+
+io.on('connection', (socket) => {
+  socket.on('client', async(data) => {
+    // Codes Here
+  });
+});
+
+exports.io = io;
+```
+
+## Client
+
+```js
+import io from 'socket.io-client';
+
+const socket = io('ws://127.0.0.1:6666/');
+
+// 上线汇报
+socket.emit('client', ()=>{
+  return 'Hello World'
+});
+socket.on('server', async(data) => {
+  // 处理服务器消息
+});
+```

+ 30 - 0
project/source/udp.md

@@ -0,0 +1,30 @@
+# UDP
+
+官方文档: <https://nodejs.org/api/dgram.html>
+
+中文翻译: <http://shouce.w3cfuns.com/nodejs/dgram.html>
+
+## Server
+
+应用场景: 心跳服务。
+
+```js
+import dgram from 'dgram';
+
+const server = dgram.createSocket('udp4');
+server.on('error', (err) => {
+  // 处理错误
+  server.close();
+  server.bind(6666);
+});
+
+server.on('message', async(msg, info) => {
+  // 处理消息
+});
+
+server.on('listening', () => {
+  const address = server.address();
+  console.log('Push Client Server listening at %s - %s', `${address.address}:${address.port}`, new Date());
+});
+server.bind(6666);
+```