Skip to content

Commit

Permalink
docs(socket.io): add webchat description (#2198)
Browse files Browse the repository at this point in the history
  • Loading branch information
atian25 authored and popomore committed Mar 12, 2018
1 parent 5cce879 commit c042366
Show file tree
Hide file tree
Showing 3 changed files with 52 additions and 32 deletions.
2 changes: 1 addition & 1 deletion docs/source/en/basics/router.md
Original file line number Diff line number Diff line change
Expand Up @@ -313,7 +313,7 @@ module.exports = app => {
app.router.get('s', '/search', app.middlewares.uppercase(), app.controller.search)
};

// curl http://localhost:7001/search2?name=egg
// curl http://localhost:7001/search?name=egg
```

### Too Many Routing Maps?
Expand Down
2 changes: 1 addition & 1 deletion docs/source/zh-cn/basics/router.md
Original file line number Diff line number Diff line change
Expand Up @@ -318,7 +318,7 @@ module.exports = app => {
app.router.get('s', '/search', app.middlewares.uppercase(), app.controller.search)
};

// curl http://localhost:7001/search2?name=egg
// curl http://localhost:7001/search?name=egg
```

### 太多路由映射?
Expand Down
80 changes: 50 additions & 30 deletions docs/source/zh-cn/tutorials/socketio.md
Original file line number Diff line number Diff line change
Expand Up @@ -7,7 +7,7 @@ WebSocket 的产生源于 Web 开发中日益增长的实时通信需求,对

框架提供了 [egg-socket.io] 插件,增加了以下开发规约:

- namespace: 通过配置的方式定义 namespace(命名空间)
- namespace: 通过配置的方式定义 namespace(命名空间)
- middleware: 对每一次 socket 连接的建立/断开、每一次消息/数据传递进行预处理
- controller: 响应 socket.io 的 event 事件
- router: 统一了 socket.io 的 event 与 框架路由的处理配置方式
Expand Down Expand Up @@ -44,7 +44,7 @@ exports.io = {
'/example': {
connectionMiddleware: [],
packetMiddleware: [],
},
},
},
};
```
Expand All @@ -58,7 +58,7 @@ exports.io = {
```js
// {app_root}/config/config.${env}.js
exports.io = {
init: { wsEngine: 'uws' }, // default: us
init: { wsEngine: 'uws' }, // default: us
};
```

Expand All @@ -78,7 +78,7 @@ exports.io = {
};
```

> 开启 `redis` 后,程序在启动时会尝试连接到 redis 服务器
> 开启 `redis` 后,程序在启动时会尝试连接到 redis 服务器
> 此处 `redis` 仅用于存储连接实例信息,参见 [#server.adapter](https://socket.io/docs/server-api/#server-adapter-value)
**注意:**
Expand All @@ -96,7 +96,7 @@ exports.io = {
{
"scripts": {
"dev": "egg-bin dev --sticky",
"start": "egg-scripts start --sticky"
"start": "egg-scripts start --sticky"
}
}
```
Expand Down Expand Up @@ -245,7 +245,7 @@ class DefaultController extends Controller {

module.exports = DefaultController;

// or async functions
// or async functions

exports.ping = async function() {
const message = this.args[0];
Expand All @@ -262,18 +262,18 @@ exports.ping = async function() {

module.exports = app => {
const { router, controller, io } = app;

// default
router.get('/', controller.home.index);

// socket.io
io.of('/').route('server', io.controller.home.server);
};
```

**注意:**

nsp 有如下的系统事件:
nsp 有如下的系统事件:

- `disconnecting` doing the disconnect
- `disconnect` connection has disconnected.
Expand Down Expand Up @@ -340,13 +340,13 @@ const log = console.log;
window.onload = function () {
// init
const socket = io('/', {

// 实际使用中可以在这里传递参数
query: {
room: 'demo',
room: 'demo',
userId: `client_${Math.random()}`,
},

transports: ['websocket']
});

Expand All @@ -364,16 +364,16 @@ window.onload = function () {
socket.on(id, msg => {
log('#receive,', msg);
});

// 系统事件
socket.on('disconnect', msg => {
socket.on('disconnect', msg => {
log('#disconnect', msg);
});

socket.on('disconnecting', () => {
log('#disconnecting');
});

socket.on('error', () => {
log('#error');
});
Expand All @@ -384,6 +384,26 @@ window.onload = function () {
};
```

#### 微信小程序

微信小程序提供的 API 为 WebSocket ,而 socket.io 是 Websocket 的上层封装,故我们无法直接用小程序的 API 连接,可以使用类似 [wxapp-socket-io](https://github.com/wxsocketio/wxapp-socket-io) 的库来适配。

示例代码如下:

```js
// 小程序端示例代码
import io from 'vendor/wxapp-socket-io.js';

const socket = io('ws://127.0.0.1:7001');
socket.on('connect', function () {
socket.emit('chat', 'hello world!');
});
socket.on('res', msg => {
console.log('res from server: %s!', msg);
});
```


### server

以下是 demo 的部分代码并解释了各个方法的作用
Expand All @@ -399,7 +419,7 @@ exports.io = {
packetMiddleware: [ ], // 针对消息的处理暂时不实现
},
},

// cluster 模式下,通过 redis 实现数据共享
redis: {
host: '127.0.0.1',
Expand Down Expand Up @@ -472,10 +492,10 @@ module.exports = () => {

const tick = (id, msg) => {
logger.debug('#tick', id, msg);

// 踢出用户前发送消息
socket.emit(id, helper.parseMsg('deny', msg));

// 调用 adapter 方法踢出用户,客户端触发 disconnect 事件
nsp.adapter.remoteDisconnect(id, true, err => {
logger.error(err);
Expand All @@ -498,10 +518,10 @@ module.exports = () => {

// 当用户加入时
nsp.adapter.clients(rooms, (err, clients) => {

// 追加当前 socket 信息到clients
clients[id] = query;

// 加入房间
socket.join(room);

Expand Down Expand Up @@ -531,7 +551,7 @@ module.exports = () => {
});

logger.debug('#online_leave', _clients);

// 更新在线用户列表
nsp.to(room).emit('online', {
clients: _clients,
Expand All @@ -548,7 +568,7 @@ module.exports = () => {

#### controller

p2p 通信,通过 exchange 进行数据交换
P2P 通信,通过 exchange 进行数据交换

```js
// {app_root}/app/io/controller/nsp.js
Expand Down Expand Up @@ -583,7 +603,7 @@ module.exports = NspController;
module.exports = app => {
const { router, controller, io } = app;
router.get('/', controller.home.index);

// socket.io
io.of('/').route('exchange', io.controller.nsp.exchange);
};
Expand All @@ -592,20 +612,20 @@ module.exports = app => {
开两个 tab 页面,并调出控制台:

```js
socket.emit('exchange', {
target: '/webrtc#Dkn3UXSu8_jHvKBmAAHW',
payload: {
msg : 'test'
}
socket.emit('exchange', {
target: '/webrtc#Dkn3UXSu8_jHvKBmAAHW',
payload: {
msg : 'test',
},
});
```

![](https://raw.githubusercontent.com/eggjs/egg/master/docs/assets/socketio-console.png)

## 参考链接

- [socket.io](https://socket.io)
- [egg-socket.io](https://github.com/eggjs/egg-socket.io)
- [socket.io]
- [egg-socket.io]
- [egg-socket.io example](https://github.com/eggjs/egg-socket.io/tree/master/example)

[socket.io]: https://socket.io
Expand Down

0 comments on commit c042366

Please sign in to comment.