@cocreate/socket-server 1.1.11 → 1.2.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.
- package/CHANGELOG.md +31 -0
- package/CoCreate.config.js +1 -1
- package/package.json +3 -5
- package/src/index.js +73 -81
- package/index.js +0 -4
package/CHANGELOG.md
CHANGED
|
@@ -1,3 +1,34 @@
|
|
|
1
|
+
# [1.2.0](https://github.com/CoCreate-app/CoCreate-socket-server/compare/v1.1.13...v1.2.0) (2022-03-06)
|
|
2
|
+
|
|
3
|
+
|
|
4
|
+
### Bug Fixes
|
|
5
|
+
|
|
6
|
+
* broadcast_sender if condition ([663da41](https://github.com/CoCreate-app/CoCreate-socket-server/commit/663da417cb127048a5bf7be92b3988ba88c9ad37))
|
|
7
|
+
* removed querystring dependancy ([340b131](https://github.com/CoCreate-app/CoCreate-socket-server/commit/340b13124fc3d222cdd57c0b20db0913a71eb807))
|
|
8
|
+
* update param roomInfo to socketInfo ([7d651d6](https://github.com/CoCreate-app/CoCreate-socket-server/commit/7d651d65b1299b18499559a747eba6b10395ef00))
|
|
9
|
+
* update param ws to socket for convetion purposes ([17e5989](https://github.com/CoCreate-app/CoCreate-socket-server/commit/17e598940acec73286465c5947a9b9b9d58b0c56))
|
|
10
|
+
|
|
11
|
+
|
|
12
|
+
### Features
|
|
13
|
+
|
|
14
|
+
* broadcast supports multiple rooms ([a8edf12](https://github.com/CoCreate-app/CoCreate-socket-server/commit/a8edf125e0b925e597bd54f7179e1c9aae3b39c3))
|
|
15
|
+
|
|
16
|
+
## [1.1.13](https://github.com/CoCreate-app/CoCreate-socket-server/compare/v1.1.12...v1.1.13) (2022-03-03)
|
|
17
|
+
|
|
18
|
+
|
|
19
|
+
### Bug Fixes
|
|
20
|
+
|
|
21
|
+
* removed emit changeDB ([cde4d9f](https://github.com/CoCreate-app/CoCreate-socket-server/commit/cde4d9f6f74d7cbf536eddb4a87436cc315e07fe))
|
|
22
|
+
* update pakage.json main to src/index ([4188d9c](https://github.com/CoCreate-app/CoCreate-socket-server/commit/4188d9c65ef461c26cc7ea3e12d8216f20183ea5))
|
|
23
|
+
|
|
24
|
+
## [1.1.12](https://github.com/CoCreate-app/CoCreate-socket-server/compare/v1.1.11...v1.1.12) (2022-02-28)
|
|
25
|
+
|
|
26
|
+
|
|
27
|
+
### Bug Fixes
|
|
28
|
+
|
|
29
|
+
* update param requestData.action to requestData.module ([8a1a7e0](https://github.com/CoCreate-app/CoCreate-socket-server/commit/8a1a7e0b7eb14c948973c42e97911781e91b9e53))
|
|
30
|
+
* wildcard permission for docs domain ([7a0d5cd](https://github.com/CoCreate-app/CoCreate-socket-server/commit/7a0d5cd1ca0cc275e28362333addd8c3a35b15b3))
|
|
31
|
+
|
|
1
32
|
## [1.1.11](https://github.com/CoCreate-app/CoCreate-socket-server/compare/v1.1.10...v1.1.11) (2022-02-16)
|
|
2
33
|
|
|
3
34
|
|
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.0",
|
|
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,48 +129,43 @@ 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
|
-
if (requestData.
|
|
145
|
+
if (requestData.module) {
|
|
149
146
|
let user_id = null;
|
|
150
147
|
if (this.authInstance) {
|
|
151
148
|
user_id = await this.authInstance.getUserId(req);
|
|
152
149
|
}
|
|
150
|
+
|
|
153
151
|
//. check permission
|
|
154
152
|
if (this.permissionInstance) {
|
|
155
|
-
let passStatus = await this.permissionInstance.check(requestData.
|
|
153
|
+
let passStatus = await this.permissionInstance.check(requestData.module, requestData.data, req, user_id)
|
|
156
154
|
if (!passStatus) {
|
|
157
|
-
this.send(
|
|
155
|
+
this.send(socket, 'permissionError', requestData.data, socketInfo.orgId, socketInfo)
|
|
158
156
|
return;
|
|
159
157
|
}
|
|
160
158
|
}
|
|
161
|
-
|
|
159
|
+
|
|
162
160
|
//. checking async status....
|
|
163
161
|
if (requestData.data.async == true) {
|
|
164
|
-
const uuid = CoCreateUUID.generate(), asyncMessage = this.asyncMessages.get(
|
|
165
|
-
|
|
162
|
+
const uuid = CoCreateUUID.generate(), asyncMessage = this.asyncMessages.get(socketInfo.key);
|
|
163
|
+
socketInfo.asyncId = uuid;
|
|
166
164
|
if (asyncMessage) {
|
|
167
165
|
asyncMessage.defineMessage(uuid);
|
|
168
166
|
}
|
|
169
167
|
}
|
|
170
|
-
|
|
171
|
-
|
|
172
|
-
if (cloneRoomInfo.orgId != null) {
|
|
173
|
-
this.emit('changeDB', ws, { db: cloneRoomInfo.orgId }, cloneRoomInfo);
|
|
174
|
-
}
|
|
175
|
-
this.emit(requestData.action, ws, requestData.data, cloneRoomInfo);
|
|
168
|
+
this.emit(requestData.module, socket, requestData.data, socketInfo);
|
|
176
169
|
}
|
|
177
170
|
|
|
178
171
|
} catch(e) {
|
|
@@ -180,49 +173,48 @@ class SocketServer extends EventEmitter{
|
|
|
180
173
|
}
|
|
181
174
|
}
|
|
182
175
|
|
|
183
|
-
broadcast(
|
|
176
|
+
broadcast(socket, namespace, rooms, messageName, data, socketInfo) {
|
|
184
177
|
const self = this;
|
|
185
|
-
const asyncId = this.getAsyncId(
|
|
178
|
+
const asyncId = this.getAsyncId(socketInfo)
|
|
186
179
|
let room_key = `/${this.prefix}/${namespace}`;
|
|
187
|
-
if (room) {
|
|
188
|
-
room_key += `/${room}`;
|
|
189
|
-
}
|
|
190
180
|
const responseData =JSON.stringify({
|
|
191
|
-
|
|
181
|
+
module: messageName,
|
|
192
182
|
data: data
|
|
193
183
|
});
|
|
194
184
|
|
|
195
185
|
let isAsync = false;
|
|
196
186
|
let asyncData = [];
|
|
197
|
-
if (asyncId &&
|
|
187
|
+
if (asyncId && socketInfo && socketInfo.key) {
|
|
198
188
|
isAsync = true;
|
|
199
189
|
}
|
|
200
|
-
|
|
201
|
-
if (
|
|
202
|
-
|
|
203
|
-
|
|
204
|
-
|
|
205
|
-
|
|
206
|
-
|
|
207
|
-
|
|
208
|
-
|
|
209
|
-
|
|
210
|
-
|
|
211
|
-
|
|
212
|
-
|
|
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)
|
|
213
207
|
}
|
|
214
|
-
|
|
215
|
-
|
|
216
|
-
})
|
|
208
|
+
})
|
|
209
|
+
}
|
|
217
210
|
}
|
|
218
211
|
|
|
219
212
|
} else {
|
|
220
|
-
|
|
221
213
|
this.clients.forEach((value, key) => {
|
|
222
214
|
console.log(value.length, key)
|
|
223
215
|
if (key.includes(room_key)) {
|
|
224
216
|
value.forEach(client => {
|
|
225
|
-
if (
|
|
217
|
+
if (socket != client || socket == client && data.broadcast_sender != false) {
|
|
226
218
|
if (isAsync) {
|
|
227
219
|
asyncData.push({socket: client, message: responseData})
|
|
228
220
|
} else {
|
|
@@ -237,38 +229,38 @@ class SocketServer extends EventEmitter{
|
|
|
237
229
|
|
|
238
230
|
//. set async processing
|
|
239
231
|
if (isAsync) {
|
|
240
|
-
this.asyncMessages.get(
|
|
232
|
+
this.asyncMessages.get(socketInfo.key).setMessage(asyncId, asyncData)
|
|
241
233
|
}
|
|
242
234
|
|
|
243
235
|
}
|
|
244
236
|
|
|
245
|
-
send(
|
|
246
|
-
const asyncId = this.getAsyncId(
|
|
237
|
+
send(socket, messageName, data, orgId, socketInfo){
|
|
238
|
+
const asyncId = this.getAsyncId(socketInfo)
|
|
247
239
|
let responseData = JSON.stringify({
|
|
248
|
-
|
|
240
|
+
module: messageName,
|
|
249
241
|
data: data
|
|
250
242
|
});
|
|
251
243
|
|
|
252
|
-
if (asyncId &&
|
|
253
|
-
this.asyncMessages.get(
|
|
244
|
+
if (asyncId && socketInfo && socketInfo.key) {
|
|
245
|
+
this.asyncMessages.get(socketInfo.key).setMessage(asyncId, [{socket, message: responseData}]);
|
|
254
246
|
} else {
|
|
255
|
-
|
|
247
|
+
socket.send(responseData);
|
|
256
248
|
}
|
|
257
249
|
this.recordTransfer('out', responseData, orgId)
|
|
258
250
|
|
|
259
251
|
}
|
|
260
252
|
|
|
261
|
-
getAsyncId(
|
|
262
|
-
if (!
|
|
253
|
+
getAsyncId(socketInfo) {
|
|
254
|
+
if (!socketInfo) return null;
|
|
263
255
|
|
|
264
|
-
if (
|
|
265
|
-
return
|
|
256
|
+
if (socketInfo.asyncId) {
|
|
257
|
+
return socketInfo.asyncId;
|
|
266
258
|
}
|
|
267
259
|
return null
|
|
268
260
|
}
|
|
269
261
|
|
|
270
|
-
sendBinary(
|
|
271
|
-
|
|
262
|
+
sendBinary(socket, data, orgId) {
|
|
263
|
+
socket.send(data, {binary: true});
|
|
272
264
|
this.recordTransfer('out', data, orgId)
|
|
273
265
|
}
|
|
274
266
|
|
package/index.js
DELETED