Skip to content

Commit 613b3c6

Browse files
committed
update 2019年7月19日18:22:36
1 parent 62f78ec commit 613b3c6

File tree

11 files changed

+415
-183
lines changed

11 files changed

+415
-183
lines changed

.gitignore

Lines changed: 3 additions & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -1 +1,3 @@
1-
_book
1+
_book
2+
.idea
3+
demos/server/node_modules

README.md

Lines changed: 183 additions & 174 deletions
Large diffs are not rendered by default.

SUMMARY.md

Lines changed: 6 additions & 4 deletions
Original file line numberDiff line numberDiff line change
@@ -1,5 +1,6 @@
11
# Table of contents
22
* [关于本socket.io中文文档](README.md)
3+
* [socke.IO website](https://socket.io/)
34

45
* [1 指南(Guide)]()
56
* [1-1 介绍(Introduction)](guide/introduction.md)
@@ -30,11 +31,12 @@
3031
* [2.2.2 房间](docs/rooms_and_namespaces/rooms.md)
3132
* [2.2.3 给外部发送消息](docs/rooms_and_namespaces/sending_message_from_the_outside-world.md)
3233

33-
* [2.3 migrating_from_0.9]()
34+
* [2.3 从0.9迁移版本 migrating_from_0.9]()
3435
* [2.3.1 authentication_differences](docs/migrating_from_0.9/authentication_differences.md )
35-
* [2.3.2 parser_protocol_differences](docs/migrating_from_0.9/parser_protocol_differences.md)
36-
* [2.3.3 log_differences](docs/migrating_from_0.9/log_differences.md)
37-
* [2.3.4 shorcuts](docs/migrating_from_0.9/shorcuts.md)
36+
* [2.3.2 log_differences](docs/migrating_from_0.9/log_differences.md)
37+
* [2.3.3 shorcuts](docs/migrating_from_0.9/shorcuts.md)
38+
* [2.3.4 configuration_differences](docs/migrating_from_0.9/configuration_differences.md)
39+
* [2.3.5 parser_protocol_differences](docs/migrating_from_0.9/parser_protocol_differences.md)
3840

3941
* [2.4 using_multiple_nodes]()
4042
* [2.4.1 apache_httpd_configuration](docs/using_multiple_nodes/apache_httpd_configuration.md)
Lines changed: 59 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,59 @@
1+
## 身份验证差异
2+
3+
### Socket.io 现使用中间器件
4+
5+
可以通过io.use()为socket.io服务器提供任意函数,该函数在创建socket时运行。查看此示例:
6+
7+
```js
8+
const srv=require('http').createServer();
9+
const io = require('socket.io')(srv)
10+
let run=0;
11+
12+
io.use((socket,next)=>{
13+
run++; //0->1
14+
next()
15+
});
16+
17+
io.use((socket,next)=>{
18+
run++;// 1->2
19+
next();
20+
})
21+
22+
const socket=require('socket.io-client')();
23+
socket.io('connect',()=>{
24+
// run 此时等于2
25+
})
26+
27+
```
28+
29+
### 现通过中间件进行身份验证更简单
30+
31+
旧的`io.set()``io.get()`已弃用,只能支持向后兼容。下面是一个将旧的授权示例转换为中间件样式的转换:
32+
33+
```js
34+
io.set('authorization',(handshakeData,callback)=>{
35+
//确保握手数据看起来不错
36+
callback(null,true)//出错,'authorized' boolean second
37+
})
38+
```
39+
新的:
40+
41+
```js
42+
io.use((socket,next)=>{
43+
const handshakeData= socket.request;
44+
// 确保握手数据看起来和以前一样好
45+
// 如果错误,则执行这个:
46+
// next(new Error('not authorized'));
47+
// 否则回调next
48+
next();
49+
})
50+
```
51+
52+
### 命名空间授权?
53+
54+
```js
55+
io.of('/namespace').use((socket,next)=>{
56+
const handshakeData= socket.request;
57+
next()
58+
})
59+
```
Lines changed: 33 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,33 @@
1+
## 配置差异
2+
3+
### io.set 不见了
4+
相反,在服务器初始化中进行如下配置:
5+
6+
```js
7+
const socket= require('socket.io')({
8+
// 这里是选项
9+
})
10+
```
11+
12+
日志级别等选项已不存在,`io.set('transports')`, `io.set('heartbeat interval')`, `io.set('heartbeat timeout'`, 和 `io.set('resource')` 仍然支持向后兼容性。
13+
14+
15+
### 设置资源路径
16+
17+
上一个`资源`选项等效于新`路径`选项,但在开始时需要一个`/`。例如,以下配置:
18+
19+
```js
20+
const socket= io.connect('localhost:3000',{
21+
resource:"path/to/socket.io";
22+
})
23+
```
24+
25+
变成:
26+
27+
28+
```js
29+
const socket= io.connect('localhost:3000',{
30+
path:'/path/to/socket.io';
31+
})
32+
33+
```
Lines changed: 18 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,18 @@
1+
## 日志差异
2+
3+
### 现在支持基于调试的日志记录
4+
5+
要打印所有调试日志,请将环境变量DEBUG设置为。ie:`DEBUG=node index.js`
6+
7+
只打印socket.io相关日志:`DEBUG=socket.io:*node index.js`
8+
9+
只从socket对象打印日志:`DEBUG=socket.io:socket node index.js`
10+
11+
希望这种模式在这一点上是有意义的。socket.io/lib中的文件名与其调试名等效。
12+
13+
调试也在浏览器中工作;日志被保存到本地存储。
14+
15+
使用:打开开发控制台,键入`localstorage.debug='socket.io:*'`(或任何调试级别),然后刷新页面。在运行`localstorage.debug=''`之前,所有内容都会被记录。
16+
17+
请参阅此处的调试文档了解[更多信息](https://www.npmjs.org/package/debug)
18+

docs/migrating_from_0.9/shorcuts.md

Lines changed: 37 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,37 @@
1+
## 快捷方式
2+
3+
一般来说,常见事物有一些新的捷径。旧版本应该仍然有效,但是快捷方式很好。
4+
5+
### 向默认命名空间中的所有客户端广播
6+
7+
以前:
8+
9+
```js
10+
io.sockets.emit('eventname',"eventdata");
11+
```
12+
现在:
13+
14+
```js
15+
io.emit('eventname','eventdata')
16+
```
17+
18+
整洁。
19+
20+
请注意,在这两种情况下,这些消息都会到达连接到默认“/”命名空间的所有客户端,但不会到达其他命名空间中的客户端。
21+
22+
### 启动服务端
23+
24+
以前:
25+
26+
```js
27+
const io = require('socket.io');
28+
const socket=io.listen(80,{/*选项*/});
29+
30+
```
31+
32+
现在:
33+
34+
```js
35+
const io=require('socket.io');
36+
const socket=io({/*选项*/})
37+
````

docs/overview/broadcasting_messages.md

Lines changed: 3 additions & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -1,4 +1,5 @@
11
## 广播消息
2+
- 广播时不支持回调
23

34
要进行广播,只需添加广播标志即可发出和发送方法调用。 广播意味着向除了启动它的socket之外的所有人发送消息。
45

@@ -8,5 +9,6 @@
89
const io = require('socket.io')(80)
910
io.on('connection',(socket)=>{
1011
socket.broadcast.emit('user connected')
12+
socket.broadcast.to('socketId').emit('hi',"hi") //给指定人发送
1113
})
12-
```
14+
```

docs/overview/using_with_express.md

Lines changed: 1 addition & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -14,7 +14,7 @@ app.get('/',(req,res)=>{
1414

1515
io.on('connection',(socket)=>{
1616
socket.emit('news',{hello:'world'}
17-
soclet.ont('my other event',(data)=>{
17+
socket.on('my other event',(data)=>{
1818
console.log(data)
1919
})
2020
})

docs/rooms_and_namespaces/rooms.md

Lines changed: 45 additions & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -1 +1,45 @@
1-
## 房间
1+
## 房间
2+
3+
在每个名称空间中,还可以定义`sockets`可以`join(加入/连接)``level(离开)`的任意通道。
4+
5+
### 加入或离开
6+
7+
可以调用`join`来订阅给定通道的 `socket`
8+
9+
```js
10+
io.on('connection',socket=>{
11+
socket.join("some room")
12+
})
13+
```
14+
15+
然后在广播或者emit时,简单地使用 `to``in`(两者是相同):
16+
17+
```js
18+
io.to('some room').emit('some event');
19+
```
20+
21+
离开该频道,请以与`join`的相同的方式调用`level`:
22+
23+
```js
24+
io.level("some room")
25+
```
26+
27+
### 默认房间
28+
29+
Socket.io中的每`Socket`都由一个随机的、不可访问的、唯一的标识符`Socket#id`标识。为了方便起见,每个套接字都自动加入由该id标识的房间。
30+
31+
32+
这使得向其他socket 广播消息变得容易:
33+
34+
```js
35+
io.on('connection',(socket)=>{
36+
socket.io('say to someone',(id,msg)=>{
37+
socket.broadcast.to(id).emit("my message",msg)
38+
})
39+
})
40+
```
41+
42+
### 断开
43+
44+
断开连接后,sockets 会自动离开它们所属的所有通道,不需要进行特殊处理。
45+

0 commit comments

Comments
 (0)