Skip to content

Commit 9520054

Browse files
committed
refactor v2 js code
1 parent ccb3675 commit 9520054

20 files changed

+214
-688
lines changed

azure-devops/templates/socket.io-tpl.yml

+1-8
Original file line numberDiff line numberDiff line change
@@ -2,15 +2,8 @@ steps:
22
- pwsh: |
33
npm install pm2 -g
44
npm install
5+
pm2 start v2/index.js --name v2/index.js
56
pm2 start v4/index.js --name v4/index.js
6-
pm2 start v2/v2-ws.js
7-
pm2 start v2/v2-ws-token.js
8-
pm2 start v2/v2-http.js
9-
pm2 start v2/v2-http-token.js
10-
pm2 start v2/v2-ws-mp.js
11-
pm2 start v2/v2-ws-token-mp.js
12-
pm2 start v2/v2-http-mp.js
13-
pm2 start v2/v2-http-token-mp.js
147
displayName: Run socket.io server
158
workingDirectory: $(Agent.BuildDirectory)/s/tests/socket.io
169
- pwsh: $(Agent.BuildDirectory)/s/azure-devops/check-port.ps1

tests/socket.io/package.json

+2-11
Original file line numberDiff line numberDiff line change
@@ -4,20 +4,11 @@
44
"description": "",
55
"main": "app.js",
66
"scripts": {
7+
"v2": "cd v2 && npm start",
78
"v4": "cd v4 && npm start",
8-
9-
"v2-ws": "cd v2 && npm run v2-ws",
10-
"v2-ws-token": "cd v2 && npm run v2-ws-token",
11-
"v2-http": "cd v2 && npm run v2-http",
12-
"v2-http-token": "cd v2 && npm run v2-http-token",
13-
14-
"v2-ws-mp": "cd v2 && npm run v2-ws-mp",
15-
"v2-ws-token-mp": "cd v2 && npm run v2-ws-token-mp",
16-
"v2-http-mp": "cd v2 && npm run v2-http-mp",
17-
"v2-http-token-mp": "cd v2 && npm run v2-http-token-mp",
189

1910
"install":"cd v4 && npm install && cd ../v2 && npm install",
20-
"start": "concurrently \"npm run v4\" \"npm run v2-ws\" \"npm run v2-ws-token\" \"npm run v2-http\" \"npm run v2-http-token\" \"npm run v2-ws-mp\" \"npm run v2-ws-token-mp\" \"npm run v2-http-mp\" \"npm run v2-http-token-mp\""
11+
"start": "concurrently \"npm run v4\" \"npm run v2\""
2112
},
2213
"author": "https://github.com/doghappy",
2314
"license": "Apache",

tests/socket.io/v2/index.js

+107
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,107 @@
1+
const https = require('https');
2+
const http = require('http');
3+
const socket = require('socket.io');
4+
const fs = require('fs');
5+
const path = require('path');
6+
const template = require('./template');
7+
8+
const wsServers = [
9+
{
10+
name: 'v2-ws',
11+
port: 11200,
12+
server: () => http.createServer(),
13+
options: {
14+
pingInterval: 5000,
15+
pingTimeout: 10000,
16+
},
17+
onCreated: template.registerEvents
18+
},
19+
{
20+
name: 'v2-ws-token',
21+
port: 11201,
22+
server: () => http.createServer(),
23+
options: {},
24+
onCreated: template.useAuthMiddlewares
25+
},
26+
{
27+
name: 'v2-ws-mp',
28+
port: 11202,
29+
server: () => http.createServer(),
30+
options: {
31+
parser: require('socket.io-msgpack-parser'),
32+
pingInterval: 5000,
33+
pingTimeout: 10000,
34+
},
35+
onCreated: template.registerEvents
36+
},
37+
{
38+
name: 'v2-ws-token-mp',
39+
port: 11203,
40+
server: () => http.createServer(),
41+
options: {
42+
parser: require('socket.io-msgpack-parser')
43+
},
44+
onCreated: template.useAuthMiddlewares
45+
}
46+
];
47+
48+
const httpServers = [
49+
{
50+
name: 'v2-http',
51+
port: 11210,
52+
server: () => http.createServer(),
53+
options: {
54+
transports: ["polling"],
55+
pingInterval: 5000,
56+
pingTimeout: 10000,
57+
},
58+
onCreated: template.registerEvents
59+
},
60+
{
61+
name: 'v2-http-token',
62+
port: 11211,
63+
server: () => http.createServer(),
64+
options: {
65+
transports: ["polling"]
66+
},
67+
onCreated: template.useAuthMiddlewares
68+
},
69+
{
70+
name: 'v2-http-mp',
71+
port: 11212,
72+
server: () => http.createServer(),
73+
options: {
74+
parser: require('socket.io-msgpack-parser'),
75+
transports: ["polling"],
76+
pingInterval: 5000,
77+
pingTimeout: 10000,
78+
},
79+
onCreated: template.registerEvents
80+
},
81+
{
82+
name: 'v2-http-token-mp',
83+
port: 11213,
84+
server: () => http.createServer(),
85+
options: {
86+
parser: require('socket.io-msgpack-parser'),
87+
transports: ["polling"]
88+
},
89+
onCreated: template.useAuthMiddlewares
90+
}
91+
];
92+
93+
let servers = [
94+
...wsServers,
95+
...httpServers
96+
];
97+
98+
if (process.env.PORT && process.env.NAME){
99+
const server = servers.find(s => s.name === process.env.NAME);
100+
server.port = process.env.PORT;
101+
servers = [server];
102+
}
103+
104+
for (const server of servers) {
105+
console.log(`Starting server '${server.name}' on port ${server.port}...`);
106+
template.start(server.name, server.port, server.server(), server.options, server.onCreated);
107+
}

tests/socket.io/v2/package.json

+1-8
Original file line numberDiff line numberDiff line change
@@ -4,14 +4,7 @@
44
"description": "",
55
"main": "app.js",
66
"scripts": {
7-
"v2-ws": "node v2-ws.js",
8-
"v2-ws-token": "node v2-ws-token.js",
9-
"v2-http": "node v2-http.js",
10-
"v2-http-token": "node v2-http-token.js",
11-
"v2-ws-mp": "node v2-ws-mp.js",
12-
"v2-ws-token-mp": "node v2-ws-token-mp.js",
13-
"v2-http-mp": "node v2-http-mp.js",
14-
"v2-http-token-mp": "node v2-http-token-mp.js"
7+
"start": "node index"
158
},
169
"author": "https://github.com/doghappy",
1710
"license": "Apache",

tests/socket.io/v2/template.js

+103
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,103 @@
1+
function start(name, port, server, options, onCreated) {
2+
const socket = require('socket.io');
3+
const io = socket(server, options);
4+
onCreated(io);
5+
server.listen(port, () => {
6+
console.log(`Started server '${name}' on port ${port}`);
7+
});
8+
}
9+
10+
function useAuthMiddlewares(io){
11+
useIOAuthMiddleware(io);
12+
useNspAuthMiddleware(io);
13+
}
14+
15+
function useIOAuthMiddleware(io){
16+
io.use((socket, next) => {
17+
if (socket.handshake.query.token === "abc") {
18+
next();
19+
} else {
20+
next(new Error("Authentication error"));
21+
}
22+
});
23+
}
24+
25+
function useNspAuthMiddleware(io){
26+
const nsp = io.of("/nsp");
27+
nsp.use((socket, next) => {
28+
if (socket.handshake.query.token === "abc") {
29+
next();
30+
} else {
31+
next(new Error("Authentication error"));
32+
}
33+
});
34+
}
35+
36+
function registerEvents(io) {
37+
registerIOEvents(io);
38+
registerNspEvents(io);
39+
}
40+
41+
function registerIOEvents(io) {
42+
io.on('connection', socket => {
43+
socket.on('1:emit', data => {
44+
socket.emit('1:emit', data);
45+
});
46+
socket.on('2:emit', (d1, d2) => {
47+
socket.emit('2:emit', d1, d2);
48+
});
49+
socket.on('1:ack', (data, cb) => {
50+
cb(data);
51+
});
52+
socket.on('get_auth', cb => {
53+
cb(socket.handshake.auth);
54+
});
55+
socket.on('get_header', (key, cb) => {
56+
cb(socket.handshake.headers[key]);
57+
});
58+
socket.on('disconnect', close => {
59+
socket.disconnect(close);
60+
});
61+
socket.on('client will be sending data to server', () => {
62+
socket.emit('client sending data to server', (arg) => {
63+
socket.emit("server received data", arg);
64+
});
65+
});
66+
});
67+
}
68+
69+
function registerNspEvents(io) {
70+
const nsp = io.of("/nsp");
71+
nsp.on("connection", socket => {
72+
socket.on('1:emit', data => {
73+
socket.emit('1:emit', data);
74+
});
75+
socket.on('2:emit', (d1, d2) => {
76+
socket.emit('2:emit', d1, d2);
77+
});
78+
socket.on('1:ack', (data, cb) => {
79+
cb(data);
80+
});
81+
socket.on('get_auth', cb => {
82+
cb(socket.handshake.auth);
83+
});
84+
socket.on('get_header', (key, cb) => {
85+
cb(socket.handshake.headers[key]);
86+
});
87+
socket.on('disconnect', close => {
88+
socket.disconnect(close);
89+
});
90+
socket.on('client will be sending data to server', () => {
91+
socket.emit('client sending data to server', (arg) => {
92+
socket.emit("server received data", arg);
93+
});
94+
});
95+
});
96+
}
97+
98+
module.exports.start = start;
99+
module.exports.useAuthMiddlewares = useAuthMiddlewares;
100+
module.exports.useIOAuthMiddleware = useIOAuthMiddleware;
101+
module.exports.useNspAuthMiddleware = useNspAuthMiddleware;
102+
module.exports.registerEvents = registerEvents;
103+
module.exports.registerIOEvents = registerIOEvents;

tests/socket.io/v2/v2-http-mp.js

-75
This file was deleted.

tests/socket.io/v2/v2-http-token-mp.js

-38
This file was deleted.

0 commit comments

Comments
 (0)