Skip to content

Commit fd86dd4

Browse files
committed
finish docs
1 parent 4a30fdc commit fd86dd4

9 files changed

+304
-20
lines changed

SUMMARY.md

Lines changed: 15 additions & 15 deletions
Original file line numberDiff line numberDiff line change
@@ -32,24 +32,24 @@
3232
* [2.2.3 给外部发送消息](docs/rooms_and_namespaces/sending_message_from_the_outside-world.md)
3333

3434
* [2.3 从0.9迁移版本 migrating_from_0.9]()
35-
* [2.3.1 authentication_differences](docs/migrating_from_0.9/authentication_differences.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)
35+
* [2.3.1 身份验证差异](docs/migrating_from_0.9/authentication_differences.md )
36+
* [2.3.2 日志差异](docs/migrating_from_0.9/log_differences.md)
37+
* [2.3.3 快捷方式](docs/migrating_from_0.9/shorcuts.md)
38+
* [2.3.4 配置差异](docs/migrating_from_0.9/configuration_differences.md)
39+
* [2.3.5 解析器/协议差异](docs/migrating_from_0.9/parser_protocol_differences.md)
4040

41-
* [2.4 using_multiple_nodes]()
42-
* [2.4.1 apache_httpd_configuration](docs/using_multiple_nodes/apache_httpd_configuration.md)
43-
* [2.4.2 passing_events_between_nodes](docs/using_multiple_nodes/passing_events_between_nodes.md)
44-
* [2.4.3 haproxy_configuration](docs/using_multiple_nodes/haproxy_configuration.md)
45-
* [2.4.4 sticky_load_balancing](docs/using_multiple_nodes/sticky_load_balancing.md)
46-
* [2.4.5 nginx_configuration](docs/using_multiple_nodes/nginx_configuration.md)
47-
* [2.4.6 using_node.js_cluster](docs/using_multiple_nodes/using_node.js_cluster.md)
41+
* [2.4 多路节点使用]()
42+
* [2.4.1 Apache httpd配置](docs/using_multiple_nodes/apache_httpd_configuration.md)
43+
* [2.4.2 在节点之间传递事件](docs/using_multiple_nodes/passing_events_between_nodes.md)
44+
* [2.4.3 Haproxy配置](docs/using_multiple_nodes/haproxy_configuration.md)
45+
* [2.4.4 粘性负载均衡](docs/using_multiple_nodes/sticky_load_balancing.md)
46+
* [2.4.5 Nginx配置](docs/using_multiple_nodes/nginx_configuration.md)
47+
* [2.4.6 使用node.js集群](docs/using_multiple_nodes/using_node.js_cluster.md)
4848

49-
* [2.5 logging_and_debugging]()
50-
* [2.5.1 available_debugging_scopes](docs/logging_and_debugging/available_debugging_scopes.md)
49+
* [2.5 日志和调试]()
50+
* [2.5.1 可用的调试范围](docs/logging_and_debugging/available_debugging_scopes.md)
5151

52-
* [2.6 emit_cheatsheet](docs/emit_cheatsheet.md)
52+
* [2.6 emit 备忘单](docs/emit_cheatsheet.md)
5353

5454
* [2.7 internals_overview]()
5555
* [2.7.1 internals_overview_dependency_graph_under_the_hood](docs/internals_overview_dependency_graph_under_the_hood.md)

docs/emit_cheatsheet.md

Lines changed: 62 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,62 @@
1+
## emit备忘单
2+
3+
```js
4+
io.on('connect',onConnect)
5+
6+
function onConnect(){
7+
// 发消息到客户端
8+
socket.emit('hello',"can you hear me?",1,2,"abc");
9+
10+
// 发送到是所有客户端除了发送者
11+
socket.broadcast.emit("boradcast","hello friends!");
12+
13+
// 发送到 “game” room中除发件人以外的所有客户端
14+
socket.to("game").emit("nice game","let't play a game");
15+
16+
// 发送到“game1”和/或“game2”房间中的所有客户端,发件人除外
17+
socket.to('game1').to('game2').emit("nice game","let's play a game (too)");
18+
19+
// 发送给“游戏”室中的所有客户,包括发件人
20+
io.in('game').emit('big-announcement',"the game will start soon");
21+
22+
// 发送到命名空间“mynamespace”中的所有客户端,包括发件人
23+
io.of('myNamespace').emit('bigger-announcement',"the tournament will start soon");
24+
25+
// 发送到特定命名空间中的特定房间,包括发件人
26+
io.of('myNamespace').to('root').emit('event',"message");
27+
28+
// 警告:`socket.to(socket.id).emit()` ,不会工作,因为它会发送给房间里的每个人
29+
// 命名 `socket.id` ,但为发件人。请改用经典的“socket.emit()”。
30+
31+
// 带确认发送
32+
socket.emit('question',"do you think so?",(answer)=>{
33+
34+
})
35+
36+
// 不压缩发送
37+
socket.compress(false).emit('uncomressed',"that's rough");
38+
39+
// 指定要发送的数据是否具有二进制数据
40+
socket.binary(false).emit('what',"I hava no binaries!");
41+
42+
// 发送到此节点上的所有客户端(使用多个节点时
43+
io.local.emit('hi',"my lovely babies");
44+
45+
// 发送到所有连接的客户端
46+
io.emit('an event send to all connected clients';)
47+
48+
}
49+
50+
```
51+
52+
**注意**:以下事件是保留的,应用程序不应将其用作事件名称:
53+
54+
- `erorr`
55+
- `connect`
56+
- `disconnect`
57+
- `disconnecting`
58+
- `newListener`
59+
- `removeListener`
60+
- `ping`
61+
- `pong`
62+

docs/faq.md

Lines changed: 7 additions & 4 deletions
Original file line numberDiff line numberDiff line change
@@ -1,12 +1,15 @@
1-
## 我可以在活动中使用通配符吗?
1+
## FAQ
2+
3+
### 我可以在活动中使用通配符吗?
24
不是直接在`Socket.IO`?可以看看 `Hao-kang Den`这个插件,提供了一个 Socket'.io中间件来处理通配符。
35

4-
## Apache Cordova中使用 `Socket.IO`
6+
### Apache Cordova中使用 `Socket.IO`
57
看看这里[this tutorial](https://Socket.IO/socket-io-with-apache-cordova/)
6-
## iOS中使用 `Socket.IO`
8+
9+
### iOS中使用 `Socket.IO`
710
看看这里[SIOSocket.](https://github.com/MegaBits/SIOSocket)
811

9-
## Android 中使用 `Socket.IO`
12+
### Android 中使用 `Socket.IO`
1013
看看这里[socket.io-client.java.](https://github.com/nkzawa/socket.io-client.java)
1114

1215
————————————————————————————————
Lines changed: 71 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,71 @@
1+
## 依赖关系图
2+
3+
socket.io codebase跨多个存储库拆分:
4+
5+
- https://github.com/socketio/socket.io
6+
- https://github.com/socketio/socket.io-client
7+
- https://github.com/socketio/socket.io-parser
8+
- https://github.com/socketio/socket.io-adapter
9+
- https://github.com/socketio/socket.io-redis
10+
- https://github.com/socketio/engine.io
11+
- https://github.com/socketio/engine.io-client
12+
- https://github.com/socketio/engine.io-parser
13+
14+
下图显示了每个项目之间的关系:
15+
16+
![依赖图](/images/dependencies.jpg)
17+
18+
每个项目都有自己的一套功能:
19+
20+
### engine.io-parser
21+
22+
是用于engine.io协议编码的javascript解析器,由 [engine.io-client](https://github.com/socketio/engine.io-client)[engine.io](https://github.com/socketio/engine.io) 共享。
23+
24+
协议的规范可以在这里找到:https://github.com/socketio/engine.io-protocol
25+
26+
### engine.io
27+
28+
engine.io是socket.io基于传输的跨浏览器/跨设备双向通信层的实现。
29+
30+
它的主要特点是能够在飞行中交换传输。连接(由 [engine.io-client](https://github.com/socketio/engine.io-client) 对应方启动)从xhr轮询开始,但如果可能,可以切换到websocket。
31+
32+
它使用 [engine.io-parser](https://github.com/socketio/engine.io-parser) 对数据包进行编码/解码。
33+
34+
### engine.io-client
35+
36+
这是[engine.io](https://github.com/socketio/engine.io)的客户机,是基于传输的[socket.io](https://github.com/socketio/socket.io)跨浏览器/跨设备双向通信层的实现。
37+
38+
它在浏览器(包括HTML5 [WebWorker](https://developer.mozilla.org/en-US/docs/Web/API/Web_Workers_API) )和node.js中运行。
39+
40+
它使用 [engine.io-parser](https://github.com/socketio/engine.io-parser) 对数据包进行编码/解码。
41+
42+
43+
### socket.io-adapter
44+
45+
这是默认的socket.io内存适配器类。
46+
47+
此模块不适用于最终用户,但可以用作从您可能想要构建的其他适配器(如[socket.io-redis](https://github.com/socketio/socket.io-redis))继承的接口。
48+
49+
### socket.io-redis
50+
51+
这是使用redis 发布/订阅 机制在多个节点之间广播消息的适配器。
52+
53+
### socket.io-parser
54+
用符合[socket.io-protocol](https://socket.io/docs/internals/) 第3版的javascript编写的socket.io编码器和解码器。由[socket.io](https://github.com/socketio/socket.io)[socket.io-client](https://github.com/socketio/socket.io-client)使用。
55+
56+
57+
### socket.io
58+
59+
socket.io给engine.io“raw”api带来了一些语法上的优势。它还引入了两个新的概念,即`root``namespace`,这引入了通信通道之间的关注分离。请参阅[相关文档](https://socket.io/docs/rooms-and-namespaces/)
60+
61+
默认情况下,它在`/socket.io/socket.io.js`处公开客户端的浏览器构建。
62+
63+
### socket.io-client
64+
65+
这是[socket.io](https://github.com/socketio/socket.io)的客户端。它依赖于[engine.io-client](https://github.com/socketio/engine.io-client),它管理传输交换和断开检测。
66+
67+
它会自动处理重新连接,以防基础连接被切断。
68+
69+
它使用[socket.io-parser](https://github.com/socketio/socket.io-parser)对数据包进行编码/解码。
70+
71+
Lines changed: 86 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,86 @@
1+
## 引擎底层
2+
3+
### Connection连接
4+
5+
```js
6+
7+
const client =io('https://myhost.com')
8+
9+
```
10+
11+
执行以下步骤:
12+
13+
- 在客户端,创建一个`engine.io-client`实例
14+
- `engine.io-client`实例尝试建立`轮询`传输
15+
16+
```txt
17+
GET https://myhost.com/socket.io/?EIO=3&transport=polling&t=ML4jUwU&b64=1
18+
19+
如下:
20+
21+
"EIO=3" # Engine.io协议的当前版本
22+
"transport=polling" # 正在建立的传输
23+
"t=ML4jUwU&b64=1" # 用于缓存总线的哈希时间戳
24+
25+
```
26+
27+
- engine.io服务器响应为:
28+
29+
```json
30+
{
31+
"type": "open",
32+
"data": {
33+
"sid": "36Yib8-rSutGQYLfAAAD", // 唯一会话ID
34+
"upgrades": ["websocket"], // 可能的运输升级列表
35+
"pingInterval": 25000, // 心跳机制的第一个参数
36+
"pingTimeout": 5000 // 心跳机制的第二个参数
37+
}
38+
}
39+
40+
```
41+
42+
- 内容由`engine.io-parser`编码为:
43+
44+
45+
```txt
46+
'96:0{"sid":"hLOEJXN07AE0GQCNAAAB","upgrades":["websocket"],"pingInterval":25000,"pingTimeout":5000}2:40'
47+
48+
详细:
49+
50+
"96" # 第一条消息的长度
51+
":" # 长度和内容之间的分隔符
52+
"0" # "打开的"消息类型
53+
'{"sid":"hLOEJXN07AE0GQCNAAAB","upgrades":["websocket"],"pingInterval":25000,"pingTimeout":5000}' # JSON编码的握手数据
54+
"2" # 第二条消息的长度
55+
":" # 长度和内容之间的分隔符
56+
"4" # 消息类型
57+
"0" # socket.io协议中的"打开的"消息类型
58+
59+
```
60+
61+
- 然后由客户端的`engine.io-parser`对内容进行解码。
62+
-`engine.io-client`级别发出`open`事件
63+
-`socket.io-client`级别发出`connect`事件
64+
65+
66+
### 升级
67+
68+
一旦刷新了现有传输(XHR轮询)的所有缓冲区,就可以通过发送探测来测试升级。
69+
70+
71+
```txt
72+
GET wss://myhost.com/socket.io/?EIO=3&transport=websocket&sid=36Yib8-rSutGQYLfAAAD
73+
74+
具体:
75+
76+
"EIO=3" # 同样,当前版本的engine.io协议
77+
"transport=websocket" # 正在调查的新传输
78+
"sid=36Yib8-rSutGQYLfAAAD" # 唯一的会话id
79+
80+
```
81+
82+
- 客户端在websocket帧中发送“ping”包,由`engine.io-parser`编码为`2probe`,其中`2`是“ping”消息类型。
83+
- 服务器响应一个“pong”包,编码为`3probe`,其中`3`是“pong”消息类型。
84+
- 在接收到“pong”包时,升级被认为是完成的,并且所有以下消息都通过新的传输。
85+
86+
Lines changed: 45 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,45 @@
1+
## 日志和调试
2+
3+
socket.io现在完全由TJ Holowaychuk称之为[debug](https://github.com/visionmedia/debug)的一个极简但功能强大的实用程序进行了测
4+
试。
5+
6+
在1.0之前,socket.io服务器默认将所有内容注销到控制台。这对许多用户来说是非常冗长的(尽管对其他用户非常有用),所以现在默认情况下我们完全保持沉默。
7+
8+
基本思想是socket.io使用的每个模块提供不同的调试范围,使您能够深入了解内部。默认情况下,所有输出都被抑制,您可以通过提供`DEBUG `env变量(node.js)或`localstorage.debug`属性(browsers)来选择查看消息。
9+
10+
您可以在我们的主页上看到它的实际应用:
11+
12+
### 可用的调试范围
13+
14+
查看可用信息的最佳方法是使用*
15+
16+
```js
17+
18+
DEBUG=* node yourfile.js
19+
20+
```
21+
22+
或者在浏览器中:
23+
24+
```js
25+
26+
localStorage.debug = '*';
27+
28+
```
29+
30+
然后根据你感兴趣的范围进行过滤。您可以在`*`前面加上范围前缀,如果有多个范围,则用逗号分隔。例如,要仅查看node.js上socket.io客户机的调试语句,请尝试以下操作:
31+
32+
```js
33+
34+
DEBUG=socket.io:client* node yourfile.js
35+
36+
```
37+
38+
要查看来自engine和socket.io的所有调试消息:
39+
40+
41+
```js
42+
43+
DEBUG=engine,socket.io* node yourfile.js
44+
45+
```

docs/using_multiple_nodes/passing_events_between_nodes.md

Lines changed: 14 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -11,3 +11,17 @@ const redis = require('socket.io-redis');
1111
io.adapter(redis({host:"localhost",port:6379}));
1212

1313
```
14+
15+
如下调用:
16+
17+
```js
18+
io.emit('hi',"all sockets")
19+
```
20+
21+
将通过redis的[发布/订阅机制](https://redis.io/topics/pubsub)广播到每个节点。
22+
23+
24+
**注意**:使用Redis适配器时仍然需要粘性会话。
25+
26+
如果您想通过`non-socket.io`进程将消息传递给它,您应该考虑[“从外部世界发送消息”](https://socket.io/docs/rooms-and-namespaces/#Sending-messages-from-the-outside-world)
27+

docs/using_multiple_nodes/using_node.js_cluster.md

Lines changed: 4 additions & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -4,4 +4,7 @@
44

55
Fedor Indutny创建了一个名为[Sticky Session](https://github.com/indutny/sticky-session)的模块,该模块确保文件描述符(即:连接)基于`remoteAddress`(即:IP)进行路由。请注意,这可能会导致路由不平衡,这取决于散列方法。
66

7-
您还可以根据集群工作进程ID为集群中的每个工作进程分配不同的端口,并使用上面可以找到的配置来平衡负载。
7+
您还可以根据集群工作进程ID为集群中的每个工作进程分配不同的端口,并使用上面可以找到的配置来平衡负载。
8+
9+
10+

images/dependencies.jpg

67.6 KB
Loading

0 commit comments

Comments
 (0)