@cocreate/socket-server 1.3.4 → 1.4.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.
Files changed (3) hide show
  1. package/CHANGELOG.md +16 -0
  2. package/package.json +2 -2
  3. package/src/index.js +111 -90
package/CHANGELOG.md CHANGED
@@ -1,3 +1,19 @@
1
+ # [1.4.0](https://github.com/CoCreate-app/CoCreate-socket-server/compare/v1.3.4...v1.4.0) (2022-09-28)
2
+
3
+
4
+ ### Bug Fixes
5
+
6
+ * asyncMessages.delete ([d24d0de](https://github.com/CoCreate-app/CoCreate-socket-server/commit/d24d0de90b7a99b3bbcd84fece5ce86728f68260))
7
+ * broadcast params reduced to socket, message, data. emit updateUserStatus ([bfc40a7](https://github.com/CoCreate-app/CoCreate-socket-server/commit/bfc40a754e7f55f23febaaa15534aed3d7ce30ae))
8
+ * bump ws version to 7.5.9 ([910c246](https://github.com/CoCreate-app/CoCreate-socket-server/commit/910c246153f4259af25d19901b4f0a6a30866174))
9
+ * remove param socketInfo when emitting userStatus ([ae45514](https://github.com/CoCreate-app/CoCreate-socket-server/commit/ae45514f1c9435ba0d8217d2c13bc3f9df31d7d7))
10
+ * replaced socketInfo with socket.config ([0e2b047](https://github.com/CoCreate-app/CoCreate-socket-server/commit/0e2b047419313eb4e8023991ae9be4140d80a551))
11
+
12
+
13
+ ### Features
14
+
15
+ * config is now accessible from socket.config ([549ea18](https://github.com/CoCreate-app/CoCreate-socket-server/commit/549ea18180e64a4df9769774881c08a8a5b1cc17))
16
+
1
17
  ## [1.3.4](https://github.com/CoCreate-app/CoCreate-socket-server/compare/v1.3.3...v1.3.4) (2022-09-01)
2
18
 
3
19
 
package/package.json CHANGED
@@ -1,6 +1,6 @@
1
1
  {
2
2
  "name": "@cocreate/socket-server",
3
- "version": "1.3.4",
3
+ "version": "1.4.0",
4
4
  "description": "CoCreate-socket-server",
5
5
  "keywords": [
6
6
  "cocreate-socket",
@@ -45,6 +45,6 @@
45
45
  },
46
46
  "devDependencies": {
47
47
  "express": "^4.17.1",
48
- "ws": "^7.3.0"
48
+ "ws": "7.5.9"
49
49
  }
50
50
  }
package/src/index.js CHANGED
@@ -30,63 +30,41 @@ class SocketServer extends EventEmitter{
30
30
  handleUpgrade(req, socket, head) {
31
31
  const self = this;
32
32
  const pathname = url.parse(req.url).pathname;
33
- const socketInfo = this.getKeyFromUrl(pathname)
34
- if (socketInfo.type == this.prefix) {
33
+ const config = this.getKeyFromUrl(pathname)
34
+ if (config.type == this.prefix) {
35
35
  self.wss.handleUpgrade(req, socket, head, function(socket) {
36
- self.onWebSocket(req, socket, socketInfo);
36
+ socket.config = config
37
+ self.onWebSocket(req, socket);
37
38
  })
38
39
  return true;
39
40
  }
40
41
  return false;
41
42
  }
42
43
 
43
- onWebSocket(req, socket, socketInfo) {
44
+ onWebSocket(req, socket) {
44
45
  const self = this;
45
46
 
46
- this.addClient(socket, socketInfo.key, socketInfo);
47
+ this.addClient(socket);
47
48
 
48
49
  socket.on('message', async (message) => {
49
- await self.onMessage(req, socket, message, socketInfo);
50
+ self.onMessage(req, socket, message);
50
51
  })
51
52
 
52
53
  socket.on('close', function () {
53
- self.removeClient(socket, socketInfo.key, socketInfo)
54
+ self.removeClient(socket)
54
55
  })
55
56
 
56
57
  socket.on("error", () => {
57
- self.removeClient(socket, socketInfo.key, socketInfo)
58
+ self.removeClient(socket)
58
59
  });
59
60
 
60
- this.send(socket, 'connect', socketInfo.key);
61
+ this.send(socket, 'connect', socket.config.key);
61
62
 
62
63
  }
63
64
 
64
- removeClient(socket, key, socketInfo) {
65
- let room_clients = this.clients.get(key)
66
- const index = room_clients.indexOf(socket);
67
-
68
- if (index > -1) {
69
- room_clients.splice(index, 1);
70
- }
71
-
72
- if (room_clients.length == 0) {
73
- this.emit('userStatus', socket, {info: key.replace(`/${this.prefix}/`, ''), status: 'off'}, socketInfo);
74
- this.emit("removeMetrics", null, { org_id: socketInfo.orgId });
75
- // this.addAsyncMessage.delete(key);
76
- } else {
77
- let total_cnt = 0;
78
- this.clients.forEach((c) => total_cnt += c.length)
79
-
80
- this.emit("changeCountMetrics", null, {
81
- org_id: socketInfo.orgId,
82
- total_cnt,
83
- client_cnt: room_clients.length
84
- });
85
- }
86
-
87
- }
88
-
89
- addClient(socket, key, socketInfo) {
65
+ addClient(socket) {
66
+ let organization_id = socket.config.orgId
67
+ let key = socket.config.key
90
68
  let room_clients = this.clients.get(key);
91
69
  if (room_clients) {
92
70
  room_clients.push(socket);
@@ -94,49 +72,64 @@ class SocketServer extends EventEmitter{
94
72
  room_clients = [socket];
95
73
  }
96
74
  this.clients.set(key, room_clients);
97
- this.addAsyncMessage(socketInfo.key)
98
-
99
- this.emit('userStatus', socket, {info: key.replace(`/${this.prefix}/`, ''), status: 'on'}, socketInfo);
75
+ // this.addAsyncMessage(key)
100
76
 
77
+ let asyncMessage = this.asyncMessages.get(key)
78
+ if (!asyncMessage) {
79
+ this.asyncMessages.set(key, new AsyncMessage(key));
80
+ }
81
+
101
82
  //. add metrics
102
83
  let total_cnt = 0;
103
84
  this.clients.forEach((c) => total_cnt += c.length)
104
85
 
105
86
  this.emit("createMetrics", null, {
106
- org_id: socketInfo.orgId,
87
+ org_id: organization_id,
107
88
  client_cnt: room_clients.length,
108
89
  total_cnt: total_cnt
109
90
  });
110
91
  }
111
92
 
112
- addAsyncMessage(key) {
113
- let asyncMessage = this.asyncMessages.get(key)
114
- if (!asyncMessage) {
115
- this.asyncMessages.set(key, new AsyncMessage(key));
93
+ removeClient(socket) {
94
+ let organization_id = socket.config.orgId
95
+ let user_id = socket.config.user_id
96
+ let key = socket.config.key
97
+ let room_clients = this.clients.get(key)
98
+ const index = room_clients.indexOf(socket);
99
+
100
+ if (index > -1) {
101
+ room_clients.splice(index, 1);
116
102
  }
117
- }
118
-
119
- getKeyFromUrl(pathname) {
120
- var path = pathname.split("/");
121
- var params = {
122
- type: null,
123
- key: pathname
124
- }
125
- if (path.length > 0) {
126
- params.type = path[1];
127
- params.orgId = path[2]
103
+
104
+ if (room_clients.length == 0) {
105
+ if (user_id)
106
+ this.emit('userStatus', socket, {user_id, status: 'off', organization_id});
107
+
108
+ this.emit("removeMetrics", null, { org_id: organization_id });
109
+
110
+ this.asyncMessages.delete(key);
111
+ } else {
112
+ let total_cnt = 0;
113
+ this.clients.forEach((c) => total_cnt += c.length)
114
+
115
+ this.emit("changeCountMetrics", null, {
116
+ org_id: organization_id,
117
+ total_cnt,
118
+ client_cnt: room_clients.length
119
+ });
128
120
  }
129
- return params
121
+
130
122
  }
131
-
132
- async onMessage(req, socket, message, socketInfo) {
123
+
124
+ async onMessage(req, socket, message) {
133
125
  try {
134
- this.recordTransfer('in', message, socketInfo.orgId)
126
+ const organization_id = socket.config.orgId
127
+ this.recordTransfer('in', message, organization_id)
135
128
 
136
129
  // ToDo remove
137
130
  if (message instanceof Buffer) {
138
- this.emit('importFile2DB', socket, message, socketInfo);
139
- console.log('importFile2DB', socket, message, socketInfo);
131
+ this.emit('importFile2DB', socket, message);
132
+ console.log('importFile2DB', socket, message);
140
133
  return;
141
134
  }
142
135
 
@@ -147,12 +140,19 @@ class SocketServer extends EventEmitter{
147
140
  if (this.authInstance) {
148
141
  user_id = await this.authInstance.getUserId(req);
149
142
  }
143
+ if (user_id) {
144
+ if (!socket.config.user_id ) {
145
+ socket.config.user_id = user_id
146
+
147
+ this.emit('userStatus', socket, {user_id, status: 'on', organization_id});
148
+ }
149
+ }
150
150
 
151
151
  //. check permission
152
152
  if (this.permissionInstance) {
153
153
  let passStatus = await this.permissionInstance.check(requestData.module, requestData.data, req, user_id)
154
154
  if (!passStatus) {
155
- this.send(socket, 'permissionError', requestData.data, socketInfo.orgId, socketInfo)
155
+ this.send(socket, 'permissionError', requestData.data)
156
156
  return;
157
157
  }
158
158
  }
@@ -160,13 +160,13 @@ class SocketServer extends EventEmitter{
160
160
  //. checking async status....
161
161
  if (requestData.data.async == true) {
162
162
  console.log('async true')
163
- const uuid = CoCreateUUID.generate(), asyncMessage = this.asyncMessages.get(socketInfo.key);
164
- socketInfo.asyncId = uuid;
163
+ const uuid = CoCreateUUID.generate(), asyncMessage = this.asyncMessages.get(socket.config.key);
164
+ socket.config.asyncId = uuid;
165
165
  if (asyncMessage) {
166
166
  asyncMessage.defineMessage(uuid);
167
167
  }
168
168
  }
169
- this.emit(requestData.module, socket, requestData.data, socketInfo);
169
+ this.emit(requestData.module, socket, requestData.data);
170
170
  }
171
171
 
172
172
  } catch(e) {
@@ -174,27 +174,36 @@ class SocketServer extends EventEmitter{
174
174
  }
175
175
  }
176
176
 
177
- broadcast(socket, namespace, rooms, messageName, data, socketInfo) {
177
+ broadcast(socket, messageName, data) {
178
178
  const self = this;
179
- const asyncId = this.getAsyncId(socketInfo)
180
- let room_key = `/${this.prefix}/${namespace}`;
181
- const responseData =JSON.stringify({
179
+ const responseData = JSON.stringify({
182
180
  module: messageName,
183
181
  data: data
184
182
  });
185
-
183
+
184
+ const asyncId = socket.config.asyncId
186
185
  let isAsync = false;
187
186
  let asyncData = [];
188
- if (asyncId && socketInfo && socketInfo.key) {
187
+ if (asyncId && socket.config && socket.config.key) {
189
188
  isAsync = true;
190
189
  }
191
190
 
191
+ let organization_id = socket.config.orgId;
192
+ let url = `/${this.prefix}/${organization_id}`;
193
+
194
+ let namespace = data.namespace;
195
+ if (namespace) {
196
+ url += `/${namespace}`
197
+ }
198
+
199
+ let rooms = data.room;
192
200
  if (rooms) {
193
201
  if (!rooms.isArray())
194
202
  rooms = [rooms]
195
203
  for (let room of rooms) {
196
- room_key += `/${room}`;
197
- const clients = this.clients.get(room_key);
204
+ let url = url;
205
+ url += `/${room}`;
206
+ const clients = this.clients.get(url);
198
207
 
199
208
  if (clients) {
200
209
  clients.forEach((client) => {
@@ -204,7 +213,7 @@ class SocketServer extends EventEmitter{
204
213
  } else {
205
214
  client.send(responseData);
206
215
  }
207
- self.recordTransfer('out', responseData, namespace)
216
+ self.recordTransfer('out', responseData, organization_id)
208
217
  }
209
218
  })
210
219
  }
@@ -212,7 +221,7 @@ class SocketServer extends EventEmitter{
212
221
 
213
222
  } else {
214
223
  this.clients.forEach((value, key) => {
215
- if (key.includes(room_key)) {
224
+ if (key.includes(url)) {
216
225
  value.forEach(client => {
217
226
  if (socket != client || socket == client && data.broadcastSender != false) {
218
227
  if (isAsync) {
@@ -220,7 +229,7 @@ class SocketServer extends EventEmitter{
220
229
  } else {
221
230
  client.send(responseData);
222
231
  }
223
- self.recordTransfer('out', responseData, namespace)
232
+ self.recordTransfer('out', responseData, organization_id)
224
233
  }
225
234
  })
226
235
  }
@@ -229,38 +238,50 @@ class SocketServer extends EventEmitter{
229
238
 
230
239
  //. set async processing
231
240
  if (isAsync) {
232
- this.asyncMessages.get(socketInfo.key).setMessage(asyncId, asyncData)
241
+ this.asyncMessages.get(socket.config.key).setMessage(asyncId, asyncData)
233
242
  }
234
243
 
235
244
  }
236
245
 
237
- send(socket, messageName, data, socketInfo){
238
- const asyncId = this.getAsyncId(socketInfo)
246
+ send(socket, messageName, data){
247
+ const asyncId = socket.config.asyncId
239
248
  let responseData = JSON.stringify({
240
249
  module: messageName,
241
- data: data
250
+ data
242
251
  });
243
252
 
244
- if (asyncId && socketInfo && socketInfo.key) {
245
- this.asyncMessages.get(socketInfo.key).setMessage(asyncId, [{socket, message: responseData}]);
253
+ if (asyncId && socket.config && socket.config.key) {
254
+ this.asyncMessages.get(socket.config.key).setMessage(asyncId, [{socket, message: responseData}]);
246
255
  } else {
247
256
  socket.send(responseData);
248
257
  }
249
258
 
250
- if (socketInfo && socketInfo.orgId)
251
- this.recordTransfer('out', responseData, socketInfo.orgId)
259
+ if (socket.config && socket.config.orgId)
260
+ this.recordTransfer('out', responseData, socket.config.orgId)
252
261
 
253
262
  }
263
+
264
+ // addAsyncMessage(key) {
265
+ // let asyncMessage = this.asyncMessages.get(key)
266
+ // if (!asyncMessage) {
267
+ // this.asyncMessages.set(key, new AsyncMessage(key));
268
+ // }
269
+ // }
254
270
 
255
- getAsyncId(socketInfo) {
256
- if (!socketInfo) return null;
257
-
258
- if (socketInfo.asyncId) {
259
- return socketInfo.asyncId;
271
+ getKeyFromUrl(pathname) {
272
+ var path = pathname.split("/");
273
+ var params = {
274
+ type: null,
275
+ key: pathname
276
+ }
277
+ if (path.length > 0) {
278
+ params.type = path[1];
279
+ params.orgId = path[2]
260
280
  }
261
- return null
281
+ return params
262
282
  }
263
-
283
+
284
+
264
285
  sendBinary(socket, data, orgId) {
265
286
  socket.send(data, {binary: true});
266
287
  this.recordTransfer('out', data, orgId)