@cocreate/socket-server 1.1.13 → 1.2.2
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 +29 -0
- package/CoCreate.config.js +1 -1
- package/package.json +2 -4
- package/src/index.js +69 -73
package/CHANGELOG.md
CHANGED
|
@@ -1,3 +1,32 @@
|
|
|
1
|
+
## [1.2.2](https://github.com/CoCreate-app/CoCreate-socket-server/compare/v1.2.1...v1.2.2) (2022-05-06)
|
|
2
|
+
|
|
3
|
+
|
|
4
|
+
### Bug Fixes
|
|
5
|
+
|
|
6
|
+
* update config organization_Id to organization_id ([2aa908d](https://github.com/CoCreate-app/CoCreate-socket-server/commit/2aa908d940c5a8ccef5faa79ea5658a84270257b))
|
|
7
|
+
|
|
8
|
+
## [1.2.1](https://github.com/CoCreate-app/CoCreate-socket-server/compare/v1.2.0...v1.2.1) (2022-03-22)
|
|
9
|
+
|
|
10
|
+
|
|
11
|
+
### Bug Fixes
|
|
12
|
+
|
|
13
|
+
* removed testing console.logs ([48358eb](https://github.com/CoCreate-app/CoCreate-socket-server/commit/48358eb3939e60c9401defb774161cbd65b6e774))
|
|
14
|
+
|
|
15
|
+
# [1.2.0](https://github.com/CoCreate-app/CoCreate-socket-server/compare/v1.1.13...v1.2.0) (2022-03-06)
|
|
16
|
+
|
|
17
|
+
|
|
18
|
+
### Bug Fixes
|
|
19
|
+
|
|
20
|
+
* broadcast_sender if condition ([663da41](https://github.com/CoCreate-app/CoCreate-socket-server/commit/663da417cb127048a5bf7be92b3988ba88c9ad37))
|
|
21
|
+
* removed querystring dependancy ([340b131](https://github.com/CoCreate-app/CoCreate-socket-server/commit/340b13124fc3d222cdd57c0b20db0913a71eb807))
|
|
22
|
+
* update param roomInfo to socketInfo ([7d651d6](https://github.com/CoCreate-app/CoCreate-socket-server/commit/7d651d65b1299b18499559a747eba6b10395ef00))
|
|
23
|
+
* update param ws to socket for convetion purposes ([17e5989](https://github.com/CoCreate-app/CoCreate-socket-server/commit/17e598940acec73286465c5947a9b9b9d58b0c56))
|
|
24
|
+
|
|
25
|
+
|
|
26
|
+
### Features
|
|
27
|
+
|
|
28
|
+
* broadcast supports multiple rooms ([a8edf12](https://github.com/CoCreate-app/CoCreate-socket-server/commit/a8edf125e0b925e597bd54f7179e1c9aae3b39c3))
|
|
29
|
+
|
|
1
30
|
## [1.1.13](https://github.com/CoCreate-app/CoCreate-socket-server/compare/v1.1.12...v1.1.13) (2022-03-03)
|
|
2
31
|
|
|
3
32
|
|
package/CoCreate.config.js
CHANGED
package/package.json
CHANGED
|
@@ -1,6 +1,6 @@
|
|
|
1
1
|
{
|
|
2
2
|
"name": "@cocreate/socket-server",
|
|
3
|
-
"version": "1.
|
|
3
|
+
"version": "1.2.2",
|
|
4
4
|
"description": "CoCreate-socket-server",
|
|
5
5
|
"keywords": [
|
|
6
6
|
"cocreate-socket",
|
|
@@ -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,20 +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
|
-
this.emit(requestData.module,
|
|
168
|
+
this.emit(requestData.module, socket, requestData.data, socketInfo);
|
|
172
169
|
}
|
|
173
170
|
|
|
174
171
|
} catch(e) {
|
|
@@ -176,48 +173,47 @@ class SocketServer extends EventEmitter{
|
|
|
176
173
|
}
|
|
177
174
|
}
|
|
178
175
|
|
|
179
|
-
broadcast(
|
|
176
|
+
broadcast(socket, namespace, rooms, messageName, data, socketInfo) {
|
|
180
177
|
const self = this;
|
|
181
|
-
const asyncId = this.getAsyncId(
|
|
178
|
+
const asyncId = this.getAsyncId(socketInfo)
|
|
182
179
|
let room_key = `/${this.prefix}/${namespace}`;
|
|
183
|
-
if (room) {
|
|
184
|
-
room_key += `/${room}`;
|
|
185
|
-
}
|
|
186
180
|
const responseData =JSON.stringify({
|
|
187
|
-
module:
|
|
181
|
+
module: messageName,
|
|
188
182
|
data: data
|
|
189
183
|
});
|
|
190
184
|
|
|
191
185
|
let isAsync = false;
|
|
192
186
|
let asyncData = [];
|
|
193
|
-
if (asyncId &&
|
|
187
|
+
if (asyncId && socketInfo && socketInfo.key) {
|
|
194
188
|
isAsync = true;
|
|
195
189
|
}
|
|
196
|
-
|
|
197
|
-
if (
|
|
198
|
-
|
|
199
|
-
|
|
200
|
-
|
|
201
|
-
|
|
202
|
-
|
|
203
|
-
|
|
204
|
-
|
|
205
|
-
|
|
206
|
-
|
|
207
|
-
|
|
208
|
-
|
|
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)
|
|
209
207
|
}
|
|
210
|
-
|
|
211
|
-
|
|
212
|
-
})
|
|
208
|
+
})
|
|
209
|
+
}
|
|
213
210
|
}
|
|
214
211
|
|
|
215
212
|
} else {
|
|
216
213
|
this.clients.forEach((value, key) => {
|
|
217
|
-
console.log(value.length, key)
|
|
218
214
|
if (key.includes(room_key)) {
|
|
219
215
|
value.forEach(client => {
|
|
220
|
-
if (
|
|
216
|
+
if (socket != client || socket == client && data.broadcast_sender != false) {
|
|
221
217
|
if (isAsync) {
|
|
222
218
|
asyncData.push({socket: client, message: responseData})
|
|
223
219
|
} else {
|
|
@@ -232,38 +228,38 @@ class SocketServer extends EventEmitter{
|
|
|
232
228
|
|
|
233
229
|
//. set async processing
|
|
234
230
|
if (isAsync) {
|
|
235
|
-
this.asyncMessages.get(
|
|
231
|
+
this.asyncMessages.get(socketInfo.key).setMessage(asyncId, asyncData)
|
|
236
232
|
}
|
|
237
233
|
|
|
238
234
|
}
|
|
239
235
|
|
|
240
|
-
send(
|
|
241
|
-
const asyncId = this.getAsyncId(
|
|
236
|
+
send(socket, messageName, data, orgId, socketInfo){
|
|
237
|
+
const asyncId = this.getAsyncId(socketInfo)
|
|
242
238
|
let responseData = JSON.stringify({
|
|
243
|
-
module:
|
|
239
|
+
module: messageName,
|
|
244
240
|
data: data
|
|
245
241
|
});
|
|
246
242
|
|
|
247
|
-
if (asyncId &&
|
|
248
|
-
this.asyncMessages.get(
|
|
243
|
+
if (asyncId && socketInfo && socketInfo.key) {
|
|
244
|
+
this.asyncMessages.get(socketInfo.key).setMessage(asyncId, [{socket, message: responseData}]);
|
|
249
245
|
} else {
|
|
250
|
-
|
|
246
|
+
socket.send(responseData);
|
|
251
247
|
}
|
|
252
248
|
this.recordTransfer('out', responseData, orgId)
|
|
253
249
|
|
|
254
250
|
}
|
|
255
251
|
|
|
256
|
-
getAsyncId(
|
|
257
|
-
if (!
|
|
252
|
+
getAsyncId(socketInfo) {
|
|
253
|
+
if (!socketInfo) return null;
|
|
258
254
|
|
|
259
|
-
if (
|
|
260
|
-
return
|
|
255
|
+
if (socketInfo.asyncId) {
|
|
256
|
+
return socketInfo.asyncId;
|
|
261
257
|
}
|
|
262
258
|
return null
|
|
263
259
|
}
|
|
264
260
|
|
|
265
|
-
sendBinary(
|
|
266
|
-
|
|
261
|
+
sendBinary(socket, data, orgId) {
|
|
262
|
+
socket.send(data, {binary: true});
|
|
267
263
|
this.recordTransfer('out', data, orgId)
|
|
268
264
|
}
|
|
269
265
|
|