@cocreate/socket-server 1.1.12 → 1.2.1
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.
- package/CHANGELOG.md +30 -0
- package/package.json +3 -5
- package/src/index.js +69 -79
- package/index.js +0 -4
package/CHANGELOG.md
CHANGED
|
@@ -1,3 +1,33 @@
|
|
|
1
|
+
## [1.2.1](https://github.com/CoCreate-app/CoCreate-socket-server/compare/v1.2.0...v1.2.1) (2022-03-22)
|
|
2
|
+
|
|
3
|
+
|
|
4
|
+
### Bug Fixes
|
|
5
|
+
|
|
6
|
+
* removed testing console.logs ([48358eb](https://github.com/CoCreate-app/CoCreate-socket-server/commit/48358eb3939e60c9401defb774161cbd65b6e774))
|
|
7
|
+
|
|
8
|
+
# [1.2.0](https://github.com/CoCreate-app/CoCreate-socket-server/compare/v1.1.13...v1.2.0) (2022-03-06)
|
|
9
|
+
|
|
10
|
+
|
|
11
|
+
### Bug Fixes
|
|
12
|
+
|
|
13
|
+
* broadcast_sender if condition ([663da41](https://github.com/CoCreate-app/CoCreate-socket-server/commit/663da417cb127048a5bf7be92b3988ba88c9ad37))
|
|
14
|
+
* removed querystring dependancy ([340b131](https://github.com/CoCreate-app/CoCreate-socket-server/commit/340b13124fc3d222cdd57c0b20db0913a71eb807))
|
|
15
|
+
* update param roomInfo to socketInfo ([7d651d6](https://github.com/CoCreate-app/CoCreate-socket-server/commit/7d651d65b1299b18499559a747eba6b10395ef00))
|
|
16
|
+
* update param ws to socket for convetion purposes ([17e5989](https://github.com/CoCreate-app/CoCreate-socket-server/commit/17e598940acec73286465c5947a9b9b9d58b0c56))
|
|
17
|
+
|
|
18
|
+
|
|
19
|
+
### Features
|
|
20
|
+
|
|
21
|
+
* broadcast supports multiple rooms ([a8edf12](https://github.com/CoCreate-app/CoCreate-socket-server/commit/a8edf125e0b925e597bd54f7179e1c9aae3b39c3))
|
|
22
|
+
|
|
23
|
+
## [1.1.13](https://github.com/CoCreate-app/CoCreate-socket-server/compare/v1.1.12...v1.1.13) (2022-03-03)
|
|
24
|
+
|
|
25
|
+
|
|
26
|
+
### Bug Fixes
|
|
27
|
+
|
|
28
|
+
* removed emit changeDB ([cde4d9f](https://github.com/CoCreate-app/CoCreate-socket-server/commit/cde4d9f6f74d7cbf536eddb4a87436cc315e07fe))
|
|
29
|
+
* update pakage.json main to src/index ([4188d9c](https://github.com/CoCreate-app/CoCreate-socket-server/commit/4188d9c65ef461c26cc7ea3e12d8216f20183ea5))
|
|
30
|
+
|
|
1
31
|
## [1.1.12](https://github.com/CoCreate-app/CoCreate-socket-server/compare/v1.1.11...v1.1.12) (2022-02-28)
|
|
2
32
|
|
|
3
33
|
|
package/package.json
CHANGED
|
@@ -1,6 +1,6 @@
|
|
|
1
1
|
{
|
|
2
2
|
"name": "@cocreate/socket-server",
|
|
3
|
-
"version": "1.1
|
|
3
|
+
"version": "1.2.1",
|
|
4
4
|
"description": "CoCreate-socket-server",
|
|
5
5
|
"keywords": [
|
|
6
6
|
"cocreate-socket",
|
|
@@ -23,7 +23,7 @@
|
|
|
23
23
|
"publishConfig": {
|
|
24
24
|
"access": "public"
|
|
25
25
|
},
|
|
26
|
-
"main": "index
|
|
26
|
+
"main": "./src/index",
|
|
27
27
|
"scripts": {
|
|
28
28
|
"demo": "PORT=5000 node demo/server.js",
|
|
29
29
|
"test": "echo \"Error: no test specified\" && exit 1",
|
|
@@ -41,9 +41,7 @@
|
|
|
41
41
|
"homepage": "https://cocreate.app/docs/CoCreate-socket-server",
|
|
42
42
|
"dependencies": {
|
|
43
43
|
"@cocreate/docs": "^1.2.70",
|
|
44
|
-
"@cocreate/uuid": "^1.1.60"
|
|
45
|
-
"querystring": "^0.2.0",
|
|
46
|
-
"ws": "^7.3.0"
|
|
44
|
+
"@cocreate/uuid": "^1.1.60"
|
|
47
45
|
},
|
|
48
46
|
"devDependencies": {
|
|
49
47
|
"express": "^4.17.1",
|
package/src/index.js
CHANGED
|
@@ -1,5 +1,3 @@
|
|
|
1
|
-
|
|
2
|
-
const qs = require('querystring');
|
|
3
1
|
const WebSocket = require('ws');
|
|
4
2
|
const url = require("url");
|
|
5
3
|
const EventEmitter = require("events").EventEmitter;
|
|
@@ -32,55 +30,55 @@ class SocketServer extends EventEmitter{
|
|
|
32
30
|
handleUpgrade(req, socket, head) {
|
|
33
31
|
const self = this;
|
|
34
32
|
const pathname = url.parse(req.url).pathname;
|
|
35
|
-
const
|
|
36
|
-
if (
|
|
37
|
-
self.wss.handleUpgrade(req, socket, head, function(
|
|
38
|
-
self.onWebSocket(req,
|
|
33
|
+
const socketInfo = this.getKeyFromUrl(pathname)
|
|
34
|
+
if (socketInfo.type == this.prefix) {
|
|
35
|
+
self.wss.handleUpgrade(req, socket, head, function(socket) {
|
|
36
|
+
self.onWebSocket(req, socket, socketInfo);
|
|
39
37
|
})
|
|
40
38
|
return true;
|
|
41
39
|
}
|
|
42
40
|
return false;
|
|
43
41
|
}
|
|
44
42
|
|
|
45
|
-
onWebSocket(req,
|
|
43
|
+
onWebSocket(req, socket, socketInfo) {
|
|
46
44
|
const self = this;
|
|
47
45
|
|
|
48
|
-
this.addClient(
|
|
46
|
+
this.addClient(socket, socketInfo.key, socketInfo);
|
|
49
47
|
|
|
50
|
-
|
|
51
|
-
await self.onMessage(req,
|
|
48
|
+
socket.on('message', async (message) => {
|
|
49
|
+
await self.onMessage(req, socket, message, socketInfo);
|
|
52
50
|
})
|
|
53
51
|
|
|
54
|
-
|
|
55
|
-
self.removeClient(
|
|
52
|
+
socket.on('close', function () {
|
|
53
|
+
self.removeClient(socket, socketInfo.key, socketInfo)
|
|
56
54
|
})
|
|
57
55
|
|
|
58
|
-
|
|
59
|
-
self.removeClient(
|
|
56
|
+
socket.on("error", () => {
|
|
57
|
+
self.removeClient(socket, socketInfo.key, socketInfo)
|
|
60
58
|
});
|
|
61
59
|
|
|
62
|
-
this.send(
|
|
60
|
+
this.send(socket, 'connect', socketInfo.key);
|
|
63
61
|
|
|
64
62
|
}
|
|
65
63
|
|
|
66
|
-
removeClient(
|
|
64
|
+
removeClient(socket, key, socketInfo) {
|
|
67
65
|
let room_clients = this.clients.get(key)
|
|
68
|
-
const index = room_clients.indexOf(
|
|
66
|
+
const index = room_clients.indexOf(socket);
|
|
69
67
|
|
|
70
68
|
if (index > -1) {
|
|
71
69
|
room_clients.splice(index, 1);
|
|
72
70
|
}
|
|
73
71
|
|
|
74
72
|
if (room_clients.length == 0) {
|
|
75
|
-
this.emit('userStatus',
|
|
76
|
-
this.emit("removeMetrics", null, { org_id:
|
|
73
|
+
this.emit('userStatus', socket, {info: key.replace(`/${this.prefix}/`, ''), status: 'off'}, socketInfo);
|
|
74
|
+
this.emit("removeMetrics", null, { org_id: socketInfo.orgId });
|
|
77
75
|
// this.addAsyncMessage.delete(key);
|
|
78
76
|
} else {
|
|
79
77
|
let total_cnt = 0;
|
|
80
78
|
this.clients.forEach((c) => total_cnt += c.length)
|
|
81
79
|
|
|
82
80
|
this.emit("changeCountMetrics", null, {
|
|
83
|
-
org_id:
|
|
81
|
+
org_id: socketInfo.orgId,
|
|
84
82
|
total_cnt,
|
|
85
83
|
client_cnt: room_clients.length
|
|
86
84
|
});
|
|
@@ -88,24 +86,24 @@ class SocketServer extends EventEmitter{
|
|
|
88
86
|
|
|
89
87
|
}
|
|
90
88
|
|
|
91
|
-
addClient(
|
|
89
|
+
addClient(socket, key, socketInfo) {
|
|
92
90
|
let room_clients = this.clients.get(key);
|
|
93
91
|
if (room_clients) {
|
|
94
|
-
room_clients.push(
|
|
92
|
+
room_clients.push(socket);
|
|
95
93
|
} else {
|
|
96
|
-
room_clients = [
|
|
94
|
+
room_clients = [socket];
|
|
97
95
|
}
|
|
98
96
|
this.clients.set(key, room_clients);
|
|
99
|
-
this.addAsyncMessage(
|
|
97
|
+
this.addAsyncMessage(socketInfo.key)
|
|
100
98
|
|
|
101
|
-
this.emit('userStatus',
|
|
99
|
+
this.emit('userStatus', socket, {info: key.replace(`/${this.prefix}/`, ''), status: 'on'}, socketInfo);
|
|
102
100
|
|
|
103
101
|
//. add metrics
|
|
104
102
|
let total_cnt = 0;
|
|
105
103
|
this.clients.forEach((c) => total_cnt += c.length)
|
|
106
104
|
|
|
107
105
|
this.emit("createMetrics", null, {
|
|
108
|
-
org_id:
|
|
106
|
+
org_id: socketInfo.orgId,
|
|
109
107
|
client_cnt: room_clients.length,
|
|
110
108
|
total_cnt: total_cnt
|
|
111
109
|
});
|
|
@@ -131,19 +129,18 @@ class SocketServer extends EventEmitter{
|
|
|
131
129
|
return params
|
|
132
130
|
}
|
|
133
131
|
|
|
134
|
-
async onMessage(req,
|
|
132
|
+
async onMessage(req, socket, message, socketInfo) {
|
|
135
133
|
try {
|
|
136
|
-
this.recordTransfer('in', message,
|
|
134
|
+
this.recordTransfer('in', message, socketInfo.orgId)
|
|
137
135
|
|
|
138
136
|
// ToDo remove
|
|
139
137
|
if (message instanceof Buffer) {
|
|
140
|
-
this.emit('importFile2DB',
|
|
141
|
-
console.log('importFile2DB',
|
|
138
|
+
this.emit('importFile2DB', socket, message, socketInfo);
|
|
139
|
+
console.log('importFile2DB', socket, message, socketInfo);
|
|
142
140
|
return;
|
|
143
141
|
}
|
|
144
142
|
|
|
145
143
|
const requestData = JSON.parse(message)
|
|
146
|
-
let cloneRoomInfo = {...roomInfo};
|
|
147
144
|
|
|
148
145
|
if (requestData.module) {
|
|
149
146
|
let user_id = null;
|
|
@@ -155,25 +152,20 @@ class SocketServer extends EventEmitter{
|
|
|
155
152
|
if (this.permissionInstance) {
|
|
156
153
|
let passStatus = await this.permissionInstance.check(requestData.module, requestData.data, req, user_id)
|
|
157
154
|
if (!passStatus) {
|
|
158
|
-
this.send(
|
|
155
|
+
this.send(socket, 'permissionError', requestData.data, socketInfo.orgId, socketInfo)
|
|
159
156
|
return;
|
|
160
157
|
}
|
|
161
158
|
}
|
|
162
159
|
|
|
163
160
|
//. checking async status....
|
|
164
161
|
if (requestData.data.async == true) {
|
|
165
|
-
const uuid = CoCreateUUID.generate(), asyncMessage = this.asyncMessages.get(
|
|
166
|
-
|
|
162
|
+
const uuid = CoCreateUUID.generate(), asyncMessage = this.asyncMessages.get(socketInfo.key);
|
|
163
|
+
socketInfo.asyncId = uuid;
|
|
167
164
|
if (asyncMessage) {
|
|
168
165
|
asyncMessage.defineMessage(uuid);
|
|
169
166
|
}
|
|
170
167
|
}
|
|
171
|
-
|
|
172
|
-
|
|
173
|
-
if (cloneRoomInfo.orgId != null) {
|
|
174
|
-
this.emit('changeDB', ws, { db: cloneRoomInfo.orgId }, cloneRoomInfo);
|
|
175
|
-
}
|
|
176
|
-
this.emit(requestData.module, ws, requestData.data, cloneRoomInfo);
|
|
168
|
+
this.emit(requestData.module, socket, requestData.data, socketInfo);
|
|
177
169
|
}
|
|
178
170
|
|
|
179
171
|
} catch(e) {
|
|
@@ -181,49 +173,47 @@ class SocketServer extends EventEmitter{
|
|
|
181
173
|
}
|
|
182
174
|
}
|
|
183
175
|
|
|
184
|
-
broadcast(
|
|
176
|
+
broadcast(socket, namespace, rooms, messageName, data, socketInfo) {
|
|
185
177
|
const self = this;
|
|
186
|
-
const asyncId = this.getAsyncId(
|
|
178
|
+
const asyncId = this.getAsyncId(socketInfo)
|
|
187
179
|
let room_key = `/${this.prefix}/${namespace}`;
|
|
188
|
-
if (room) {
|
|
189
|
-
room_key += `/${room}`;
|
|
190
|
-
}
|
|
191
180
|
const responseData =JSON.stringify({
|
|
192
|
-
module:
|
|
181
|
+
module: messageName,
|
|
193
182
|
data: data
|
|
194
183
|
});
|
|
195
184
|
|
|
196
185
|
let isAsync = false;
|
|
197
186
|
let asyncData = [];
|
|
198
|
-
if (asyncId &&
|
|
187
|
+
if (asyncId && socketInfo && socketInfo.key) {
|
|
199
188
|
isAsync = true;
|
|
200
189
|
}
|
|
201
|
-
|
|
202
|
-
if (
|
|
203
|
-
|
|
204
|
-
|
|
205
|
-
|
|
206
|
-
|
|
207
|
-
|
|
208
|
-
|
|
209
|
-
|
|
210
|
-
|
|
211
|
-
|
|
212
|
-
|
|
213
|
-
|
|
190
|
+
|
|
191
|
+
if (rooms) {
|
|
192
|
+
if (!rooms.isArray())
|
|
193
|
+
rooms = [rooms]
|
|
194
|
+
for (let room of rooms) {
|
|
195
|
+
room_key += `/${room}`;
|
|
196
|
+
const clients = this.clients.get(room_key);
|
|
197
|
+
|
|
198
|
+
if (clients) {
|
|
199
|
+
clients.forEach((client) => {
|
|
200
|
+
if (socket != client || socket == client && data.broadcast_sender != false) {
|
|
201
|
+
if (isAsync) {
|
|
202
|
+
asyncData.push({socket: client, message: responseData})
|
|
203
|
+
} else {
|
|
204
|
+
client.send(responseData);
|
|
205
|
+
}
|
|
206
|
+
self.recordTransfer('out', responseData, namespace)
|
|
214
207
|
}
|
|
215
|
-
|
|
216
|
-
|
|
217
|
-
})
|
|
208
|
+
})
|
|
209
|
+
}
|
|
218
210
|
}
|
|
219
211
|
|
|
220
212
|
} else {
|
|
221
|
-
|
|
222
213
|
this.clients.forEach((value, key) => {
|
|
223
|
-
console.log(value.length, key)
|
|
224
214
|
if (key.includes(room_key)) {
|
|
225
215
|
value.forEach(client => {
|
|
226
|
-
if (
|
|
216
|
+
if (socket != client || socket == client && data.broadcast_sender != false) {
|
|
227
217
|
if (isAsync) {
|
|
228
218
|
asyncData.push({socket: client, message: responseData})
|
|
229
219
|
} else {
|
|
@@ -238,38 +228,38 @@ class SocketServer extends EventEmitter{
|
|
|
238
228
|
|
|
239
229
|
//. set async processing
|
|
240
230
|
if (isAsync) {
|
|
241
|
-
this.asyncMessages.get(
|
|
231
|
+
this.asyncMessages.get(socketInfo.key).setMessage(asyncId, asyncData)
|
|
242
232
|
}
|
|
243
233
|
|
|
244
234
|
}
|
|
245
235
|
|
|
246
|
-
send(
|
|
247
|
-
const asyncId = this.getAsyncId(
|
|
236
|
+
send(socket, messageName, data, orgId, socketInfo){
|
|
237
|
+
const asyncId = this.getAsyncId(socketInfo)
|
|
248
238
|
let responseData = JSON.stringify({
|
|
249
|
-
module:
|
|
239
|
+
module: messageName,
|
|
250
240
|
data: data
|
|
251
241
|
});
|
|
252
242
|
|
|
253
|
-
if (asyncId &&
|
|
254
|
-
this.asyncMessages.get(
|
|
243
|
+
if (asyncId && socketInfo && socketInfo.key) {
|
|
244
|
+
this.asyncMessages.get(socketInfo.key).setMessage(asyncId, [{socket, message: responseData}]);
|
|
255
245
|
} else {
|
|
256
|
-
|
|
246
|
+
socket.send(responseData);
|
|
257
247
|
}
|
|
258
248
|
this.recordTransfer('out', responseData, orgId)
|
|
259
249
|
|
|
260
250
|
}
|
|
261
251
|
|
|
262
|
-
getAsyncId(
|
|
263
|
-
if (!
|
|
252
|
+
getAsyncId(socketInfo) {
|
|
253
|
+
if (!socketInfo) return null;
|
|
264
254
|
|
|
265
|
-
if (
|
|
266
|
-
return
|
|
255
|
+
if (socketInfo.asyncId) {
|
|
256
|
+
return socketInfo.asyncId;
|
|
267
257
|
}
|
|
268
258
|
return null
|
|
269
259
|
}
|
|
270
260
|
|
|
271
|
-
sendBinary(
|
|
272
|
-
|
|
261
|
+
sendBinary(socket, data, orgId) {
|
|
262
|
+
socket.send(data, {binary: true});
|
|
273
263
|
this.recordTransfer('out', data, orgId)
|
|
274
264
|
}
|
|
275
265
|
|
package/index.js
DELETED