oox 0.0.6 → 0.1.0

This diff represents the content of publicly available package versions that have been released to one of the supported registries. The information contained in this diff is provided for informational purposes only and reflects changes between package versions as they appear in their respective public registries.
@@ -1,222 +1,222 @@
1
-
2
- const { getIPAddress } = require ( '../util' )
3
-
4
- const http = require ( 'http' )
5
-
6
- const SocketIO = require ( 'socket.io' )
7
-
8
- const Socket = require ( './socket.class' )
9
-
10
- const Global = require ( '../global' )
11
-
12
-
13
-
14
- module.exports = class SocketIOServer {
15
-
16
-
17
-
18
- /**
19
- * service name
20
- */
21
- name = ''
22
-
23
-
24
-
25
- /**
26
- * service host ip
27
- */
28
- host = getIPAddress ( 4 ) [ 0 ]
29
-
30
-
31
-
32
- /**
33
- * listen port
34
- */
35
- port = 0
36
-
37
-
38
-
39
- /**
40
- * socket.io service path
41
- */
42
- path = '/socket.io'
43
-
44
-
45
-
46
- /**
47
- * means this.server created by myself<SocketIOServer>
48
- */
49
- #isSelfServer = false
50
-
51
-
52
-
53
- /**
54
- * @type {http.Server}
55
- */
56
- server = null
57
-
58
-
59
-
60
- /**
61
- * @type {SocketIO.Server}
62
- */
63
- socketServer = null
64
-
65
-
66
-
67
- constructor ( name ) {
68
-
69
- this.name = name
70
-
71
- Global.socketIOServers.push ( this )
72
- }
73
-
74
-
75
-
76
- async serve ( ) {
77
-
78
- await this.stop ( )
79
-
80
- const port = this.port
81
-
82
- const isRndPort = 'number' !== typeof port || port === 0
83
-
84
- const isSelfServer = this.#isSelfServer = this.server ? true : false
85
-
86
- const server = this.server = isSelfServer ? this.server :
87
- http.createServer ( ( request, response ) => response.end ( 'No HTTP Gateway' ) )
88
-
89
- if ( !server.listening ) server.listen ( isRndPort ? 0 : port )
90
-
91
- this.port = server.address ( ).port
92
-
93
- this.createSocketIOServer ( )
94
- }
95
-
96
-
97
-
98
- async stop ( ) {
99
-
100
- if ( this.socketServer )
101
- await new Promise ( ( resolve, reject ) =>
102
- this.socketServer.close ( error => error ? reject ( error ) : resolve ( ) ) )
103
-
104
- if ( this.#isSelfServer )
105
- await new Promise ( ( resolve, reject ) => {
106
-
107
- this.server.close ( function ( error ) {
108
-
109
- if ( error ) reject ( error )
110
- else resolve ( )
111
- } )
112
- } )
113
- }
114
-
115
-
116
-
117
- genOptions ( ) {
118
-
119
- return {
120
- /**
121
- * name of the path to capture
122
- * @default "/socket.io"
123
- */
124
- path: this.path,
125
- /**
126
- * how many ms before a client without namespace is closed
127
- * @default 45000
128
- */
129
- connectTimeout: 5000,
130
- /**
131
- * how many ms without a pong packet to consider the connection closed
132
- * @default 5000
133
- */
134
- pingTimeout: 2000,
135
- /**
136
- * how many ms before sending a new ping packet
137
- * @default 25000
138
- */
139
- pingInterval: 10000,
140
- /**
141
- * how many bytes or characters a message can be, before closing the session (to avoid DoS).
142
- * @default 1e5 (100 KB)
143
- */
144
- maxHttpBufferSize: 1e5
145
- }
146
- }
147
-
148
-
149
-
150
- createSocketIOServer ( ) {
151
-
152
- const socketServer = this.socketServer = new SocketIO.Server ( this.server, this.genOptions ( ) )
153
-
154
- socketServer.on ( 'connection', async socket => {
155
-
156
- try {
157
-
158
- await this.onSocketConnection ( socket )
159
- } catch ( error ) {
160
-
161
- socket.send ( error.message ).disconnect ( true )
162
- }
163
- } )
164
- }
165
-
166
-
167
-
168
- /**
169
- * 服务端Socket连接事件
170
- * @param {Socket} socket
171
- */
172
- onSocketConnection ( socket ) {
173
-
174
- const headers = socket.handshake.headers
175
-
176
- const callerId = headers [ 'x-caller-id' ] || socket.id
177
-
178
- // 已经存在相同的连接
179
- if ( Global.sockets.has ( callerId ) ) throw new Error ( 'Connection Exists' )
180
-
181
- // client ip or caller service ip
182
- const ip = headers [ 'x-real-ip' ] || headers [ 'x-ip' ] || socket.handshake.address
183
-
184
- // service name
185
- const caller = headers [ 'x-caller' ] || 'anonymous'
186
-
187
- socket.data = { connected: true, host: ip, name: caller, id: callerId, owner: this.name }
188
-
189
- // 保存 callerId 与 socket 对应关系
190
- Global.sockets.set ( callerId, socket )
191
-
192
- socket.on ( 'disconnect', reason => this.onSocketDisconnect ( socket, new Error ( `SocketIO name[${caller}] id[${callerId}] ${reason}` ) ) )
193
-
194
- socket.emit ( 'oox_connected', { name: this.name } )
195
-
196
- this.onConnection ( socket )
197
- }
198
-
199
-
200
-
201
- onConnection ( socket ) { }
202
-
203
-
204
-
205
- /**
206
- * 服务端Socket断开事件
207
- * @param {Socket} socket
208
- * @param {Error} reason
209
- */
210
- onSocketDisconnect ( socket, reason ) {
211
-
212
- socket.data.connected = false
213
-
214
- Global.sockets.delete ( socket.data.id )
215
-
216
- this.onDisconnect ( socket, reason )
217
- }
218
-
219
-
220
-
221
- onDisconnect ( socket, reason ) { }
1
+
2
+ const { getIPAddress } = require ( '../util' )
3
+
4
+ const http = require ( 'http' )
5
+
6
+ const SocketIO = require ( 'socket.io' )
7
+
8
+ const Socket = require ( './socket.class' )
9
+
10
+ const Global = require ( '../global' )
11
+
12
+
13
+
14
+ module.exports = class SocketIOServer {
15
+
16
+
17
+
18
+ /**
19
+ * service name
20
+ */
21
+ name = ''
22
+
23
+
24
+
25
+ /**
26
+ * service host ip
27
+ */
28
+ host = getIPAddress ( 4 ) [ 0 ]
29
+
30
+
31
+
32
+ /**
33
+ * listen port
34
+ */
35
+ port = 0
36
+
37
+
38
+
39
+ /**
40
+ * socket.io service path
41
+ */
42
+ path = '/socket.io'
43
+
44
+
45
+
46
+ /**
47
+ * means this.server created by myself<SocketIOServer>
48
+ */
49
+ #isSelfServer = false
50
+
51
+
52
+
53
+ /**
54
+ * @type {http.Server}
55
+ */
56
+ server = null
57
+
58
+
59
+
60
+ /**
61
+ * @type {SocketIO.Server}
62
+ */
63
+ socketServer = null
64
+
65
+
66
+
67
+ constructor ( name ) {
68
+
69
+ this.name = name
70
+
71
+ Global.socketIOServers.push ( this )
72
+ }
73
+
74
+
75
+
76
+ async serve ( ) {
77
+
78
+ await this.stop ( )
79
+
80
+ const port = this.port
81
+
82
+ const isRndPort = 'number' !== typeof port || port === 0
83
+
84
+ const isSelfServer = this.#isSelfServer = this.server ? true : false
85
+
86
+ const server = this.server = isSelfServer ? this.server :
87
+ http.createServer ( ( request, response ) => response.end ( 'No HTTP Gateway' ) )
88
+
89
+ if ( !server.listening ) server.listen ( isRndPort ? 0 : port )
90
+
91
+ this.port = server.address ( ).port
92
+
93
+ this.createSocketIOServer ( )
94
+ }
95
+
96
+
97
+
98
+ async stop ( ) {
99
+
100
+ if ( this.socketServer )
101
+ await new Promise ( ( resolve, reject ) =>
102
+ this.socketServer.close ( error => error ? reject ( error ) : resolve ( ) ) )
103
+
104
+ if ( this.#isSelfServer )
105
+ await new Promise ( ( resolve, reject ) => {
106
+
107
+ this.server.close ( function ( error ) {
108
+
109
+ if ( error ) reject ( error )
110
+ else resolve ( )
111
+ } )
112
+ } )
113
+ }
114
+
115
+
116
+
117
+ genOptions ( ) {
118
+
119
+ return {
120
+ /**
121
+ * name of the path to capture
122
+ * @default "/socket.io"
123
+ */
124
+ path: this.path,
125
+ /**
126
+ * how many ms before a client without namespace is closed
127
+ * @default 45000
128
+ */
129
+ connectTimeout: 5000,
130
+ /**
131
+ * how many ms without a pong packet to consider the connection closed
132
+ * @default 5000
133
+ */
134
+ pingTimeout: 2000,
135
+ /**
136
+ * how many ms before sending a new ping packet
137
+ * @default 25000
138
+ */
139
+ pingInterval: 10000,
140
+ /**
141
+ * how many bytes or characters a message can be, before closing the session (to avoid DoS).
142
+ * @default 1e5 (100 KB)
143
+ */
144
+ maxHttpBufferSize: 1e5
145
+ }
146
+ }
147
+
148
+
149
+
150
+ createSocketIOServer ( ) {
151
+
152
+ const socketServer = this.socketServer = new SocketIO.Server ( this.server, this.genOptions ( ) )
153
+
154
+ socketServer.on ( 'connection', async socket => {
155
+
156
+ try {
157
+
158
+ await this.onSocketConnection ( socket )
159
+ } catch ( error ) {
160
+
161
+ socket.send ( error.message ).disconnect ( true )
162
+ }
163
+ } )
164
+ }
165
+
166
+
167
+
168
+ /**
169
+ * 服务端Socket连接事件
170
+ * @param {Socket} socket
171
+ */
172
+ onSocketConnection ( socket ) {
173
+
174
+ const headers = socket.handshake.headers
175
+
176
+ const callerId = headers [ 'x-caller-id' ] || socket.id
177
+
178
+ // 已经存在相同的连接
179
+ if ( Global.sockets.has ( callerId ) ) throw new Error ( 'Connection Exists' )
180
+
181
+ // client ip or caller service ip
182
+ const ip = headers [ 'x-real-ip' ] || headers [ 'x-ip' ] || socket.handshake.address
183
+
184
+ // service name
185
+ const caller = headers [ 'x-caller' ] || 'anonymous'
186
+
187
+ socket.data = { connected: true, host: ip, name: caller, id: callerId, owner: this.name }
188
+
189
+ // 保存 callerId 与 socket 对应关系
190
+ Global.sockets.set ( callerId, socket )
191
+
192
+ socket.on ( 'disconnect', reason => this.onSocketDisconnect ( socket, new Error ( `SocketIO name[${caller}] id[${callerId}] ${reason}` ) ) )
193
+
194
+ socket.emit ( 'oox_connected', { name: this.name } )
195
+
196
+ this.onConnection ( socket )
197
+ }
198
+
199
+
200
+
201
+ onConnection ( socket ) { }
202
+
203
+
204
+
205
+ /**
206
+ * 服务端Socket断开事件
207
+ * @param {Socket} socket
208
+ * @param {Error} reason
209
+ */
210
+ onSocketDisconnect ( socket, reason ) {
211
+
212
+ socket.data.connected = false
213
+
214
+ Global.sockets.delete ( socket.data.id )
215
+
216
+ this.onDisconnect ( socket, reason )
217
+ }
218
+
219
+
220
+
221
+ onDisconnect ( socket, reason ) { }
222
222
  }
@@ -1,23 +1,23 @@
1
-
2
- const SocketIO = require ( 'socket.io' )
3
-
4
- module.exports = class Socket extends SocketIO.Socket {
5
-
6
-
7
-
8
- static Data = {
9
- connected: false,
10
- host: '',
11
- name: '',
12
- id: '',
13
- owner: ''
14
- }
15
-
16
-
17
-
18
- /**
19
- * Socket 扩展连接信息
20
- * @type {Socket.Data}
21
- */
22
- data = Object.assign ( { }, Socket.Data )
1
+
2
+ const SocketIO = require ( 'socket.io' )
3
+
4
+ module.exports = class Socket extends SocketIO.Socket {
5
+
6
+
7
+
8
+ static Data = {
9
+ connected: false,
10
+ host: '',
11
+ name: '',
12
+ id: '',
13
+ owner: ''
14
+ }
15
+
16
+
17
+
18
+ /**
19
+ * Socket 扩展连接信息
20
+ * @type {Socket.Data}
21
+ */
22
+ data = Object.assign ( { }, Socket.Data )
23
23
  }