Skip to content

Commit 42d43d9

Browse files
committed
add: demos && fix typos
1 parent ff50cfc commit 42d43d9

File tree

8 files changed

+111
-6
lines changed

8 files changed

+111
-6
lines changed

.gitignore

+3-2
Original file line numberDiff line numberDiff line change
@@ -1,3 +1,4 @@
1+
node_modules
2+
demos/node_modules
13
_book
2-
.idea
3-
demos/server/node_modules
4+
.idea

README.md

+4-1
Original file line numberDiff line numberDiff line change
@@ -6,8 +6,11 @@ gitbook 见 :https://socket.gitbook.io/docs/
66

77
为了更加深入学习和使用socket.io,现在我把它的文档中文翻译下,也方便自己学习。
88

9+
10+
911
## TODO
10-
> 剩下API 尚未完成
12+
13+
- 更新新版的socket.io docs
1114

1215
## 目录
1316
* [关于本socket.io中文文档](README.md)

demos/_2.1.7-limit-namespace/app.js

+65
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,65 @@
1+
const app = require("express")();
2+
const bodyParser = require("body-parser");
3+
const server = require("http").Server(app);
4+
const io = require("socket.io")(server);
5+
6+
app.use(bodyParser.urlencoded({ extended: true }));
7+
app.use(bodyParser.json());
8+
server.listen(80, () => {
9+
console.log("server listen port = 80");
10+
});
11+
12+
let ssids = [];
13+
io.on("connection", (socket) => {
14+
console.log("===>");
15+
ssids.push(socket.id);
16+
socket.emit("news", { hello: "world" });
17+
18+
socket.on("my other event", (data) => {
19+
console.log(data);
20+
});
21+
22+
socket.on("news", (data) => {
23+
console.log(data);
24+
});
25+
26+
});
27+
28+
const chat = io
29+
.of('/chat')
30+
.on('connection',(socket)=>{
31+
console.log("chat===>","hello world")
32+
socket.emit('a message',{
33+
that:'only',
34+
'/chat':'will get'
35+
})
36+
chat.emit('a message',{
37+
everyone:'in',
38+
'/chat':'will get'
39+
})
40+
})
41+
42+
const chat = io
43+
.of('/news')
44+
.on('connection',(socket)=>{
45+
console.log("new===>","hello world")
46+
socket.emit('a message',{
47+
that:'only',
48+
'/chat':'will get'
49+
})
50+
chat.emit('a message',{
51+
everyone:'in',
52+
'/chat':'will get'
53+
})
54+
})
55+
56+
57+
58+
app.get("/", (req, res) => {
59+
const query = req.query;
60+
console.log(query);
61+
62+
io.sockets.emit("send-a", { target: "通缉A" });
63+
64+
res.sendFile(__dirname + "/index.html");
65+
});
+29
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,29 @@
1+
<!DOCTYPE html>
2+
<html lang="en">
3+
<head>
4+
<meta charset="UTF-8">
5+
<meta name="viewport" content="width=device-width, initial-scale=1.0">
6+
<meta http-equiv="X-UA-Compatible" content="ie=edge">
7+
<title>Document</title>
8+
<script src="http://127.0.0.1/socket.io/socket.io.js"></script>
9+
10+
</head>
11+
<body>
12+
13+
</body>
14+
<script>
15+
const chat = io.connect('http://localhost/chat')
16+
const news = io.connect('http://localhost/news')
17+
chat.on('connect',()=>{
18+
chat.emit('hi~')
19+
chat.on("a message",(data)=>{
20+
console.log(data)
21+
})
22+
23+
})
24+
25+
news.on('news',()=>{
26+
news.emit('wowo~')
27+
})
28+
</script>
29+
</html>

demos/_2.1.7-limit-namespace/{

Whitespace-only changes.

demos/package.json

+7
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,7 @@
1+
{
2+
"dependencies": {
3+
"body-parser": "^1.19.0",
4+
"express": "^4.17.1",
5+
"socket.io": "^2.3.0"
6+
}
7+
}

demos/server/index.html

+1-1
Original file line numberDiff line numberDiff line change
@@ -5,7 +5,7 @@
55
<meta name="viewport" content="width=device-width, initial-scale=1.0">
66
<meta http-equiv="X-UA-Compatible" content="ie=edge">
77
<title>Document</title>
8-
<script src="/socket.io/socket.io.js"></script>
8+
<script src="127.0.0.1/socket.io/socket.io.js"></script>
99
<script>
1010
const socket= io.connect('http://localhost')
1111
socket.on('connect',()=>{

docs/overview/restricting_yourself_to_a_namespace.md

+2-2
Original file line numberDiff line numberDiff line change
@@ -32,8 +32,8 @@ const chat = io
3232

3333
```html
3434
<script>
35-
const chat = io.connection('http://localhost/chat')
36-
const news = io.connection('http://localhost/news')
35+
const chat = io.connect('http://localhost/chat')
36+
const news = io.connect('http://localhost/news')
3737
chat.on('connect',()=>{
3838
chat.emit('hi~')
3939
})

0 commit comments

Comments
 (0)