-
Notifications
You must be signed in to change notification settings - Fork 0
Expand file tree
/
Copy pathindex.js
More file actions
92 lines (86 loc) · 2.47 KB
/
Copy pathindex.js
File metadata and controls
92 lines (86 loc) · 2.47 KB
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
61
62
63
64
65
66
67
68
69
70
71
72
73
74
75
76
77
78
79
80
81
82
83
84
85
86
87
88
89
90
91
92
const SIO = require('socket.io');
class SocketIO {
constructor() {
this.sio = new SIO.Server({
allowEIO3: true, // whether to enable compatibility with Socket.IO v2 clientst
pingInterval: 10000,
pingTimeout: 5000,
cors: {
origin: [
'http://localhost',
'https://localhost',
'http://localhost:3000',
'https://localhost:3000',
'http://localhost:3001',
'https://localhost:3001',
'http://localhost:8000',
'https://localhost:8000',
],
methods: ['GET', 'POST', 'PUT'],
allowedHeaders: ['X-Custom-Header'],
credentials: true, //是否带有Cookie,认证信息
},
});
this.sio.compress(true);
this.sio.on('error', (...args) => {
console.log(args);
});
}
// 监听独立服务
listen(port) {
if (!port) {
return false;
}
this.sio.listen(port);
}
/**
* 1、绑定WEB Server
* 2、创建连接
*/
bindWeb(server) {
this.sio.bind(server);
this._buildWeb();
}
_buildWeb() {
this.sio
.on('connection', (socket) => {
console.log(
'----web-server--socket-connected---socket.id',
socket.id
);
})
.on('data', (data) => {
console.log('----web-server--socket-data', data);
});
this.sio
.of('namespace2')
.on('connection', (socket) => {
console.log(
'----web-server--namespace2-connected---socket.id',
socket.id
);
})
.on('data', (data) => {
console.log('----web-server--namespace2-data', data);
});
}
/**
* 1、绑定API Server
* 2、创建连接
*/
bindApi(server) {
this.sio.bind(server);
this._buildApi();
}
/**
* 实现连接功能
*/
_buildApi() {
this.sio.on('connection', (p1, p2) => {
console.log('----api-server--socket-connected---');
console.log('----api-server---p1---', p1);
console.log('----api-server---p2---', p2);
});
}
}
module.exports = SocketIO;