@cocreate/socket-server 1.1.13 → 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 CHANGED
@@ -1,3 +1,18 @@
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
+
1
16
  ## [1.1.13](https://github.com/CoCreate-app/CoCreate-socket-server/compare/v1.1.12...v1.1.13) (2022-03-03)
2
17
 
3
18
 
package/package.json CHANGED
@@ -1,6 +1,6 @@
1
1
  {
2
2
  "name": "@cocreate/socket-server",
3
- "version": "1.1.13",
3
+ "version": "1.2.0",
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 url_info = this.getKeyFromUrl(pathname)
36
- if (url_info.type == this.prefix) {
37
- self.wss.handleUpgrade(req, socket, head, function(ws) {
38
- self.onWebSocket(req, ws, url_info);
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, ws, info) {
43
+ onWebSocket(req, socket, socketInfo) {
46
44
  const self = this;
47
45
 
48
- this.addClient(ws, info.key, info);
46
+ this.addClient(socket, socketInfo.key, socketInfo);
49
47
 
50
- ws.on('message', async (message) => {
51
- await self.onMessage(req, ws, message, info);
48
+ socket.on('message', async (message) => {
49
+ await self.onMessage(req, socket, message, socketInfo);
52
50
  })
53
51
 
54
- ws.on('close', function () {
55
- self.removeClient(ws, info.key, info)
52
+ socket.on('close', function () {
53
+ self.removeClient(socket, socketInfo.key, socketInfo)
56
54
  })
57
55
 
58
- ws.on("error", () => {
59
- self.removeClient(ws, info.key, info)
56
+ socket.on("error", () => {
57
+ self.removeClient(socket, socketInfo.key, socketInfo)
60
58
  });
61
59
 
62
- this.send(ws, 'connect', info.key);
60
+ this.send(socket, 'connect', socketInfo.key);
63
61
 
64
62
  }
65
63
 
66
- removeClient(ws, key, roomInfo) {
64
+ removeClient(socket, key, socketInfo) {
67
65
  let room_clients = this.clients.get(key)
68
- const index = room_clients.indexOf(ws);
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', ws, {info: key.replace(`/${this.prefix}/`, ''), status: 'off'}, roomInfo);
76
- this.emit("removeMetrics", null, { org_id: roomInfo.orgId });
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: roomInfo.orgId,
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(ws, key, roomInfo) {
89
+ addClient(socket, key, socketInfo) {
92
90
  let room_clients = this.clients.get(key);
93
91
  if (room_clients) {
94
- room_clients.push(ws);
92
+ room_clients.push(socket);
95
93
  } else {
96
- room_clients = [ws];
94
+ room_clients = [socket];
97
95
  }
98
96
  this.clients.set(key, room_clients);
99
- this.addAsyncMessage(roomInfo.key)
97
+ this.addAsyncMessage(socketInfo.key)
100
98
 
101
- this.emit('userStatus', ws, {info: key.replace(`/${this.prefix}/`, ''), status: 'on'}, roomInfo);
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: roomInfo.orgId,
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, ws, message, roomInfo) {
132
+ async onMessage(req, socket, message, socketInfo) {
135
133
  try {
136
- this.recordTransfer('in', message, roomInfo.orgId)
134
+ this.recordTransfer('in', message, socketInfo.orgId)
137
135
 
138
136
  // ToDo remove
139
137
  if (message instanceof Buffer) {
140
- this.emit('importFile2DB', ws, message, roomInfo);
141
- console.log('importFile2DB', ws, message, roomInfo);
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(ws, 'permissionError', requestData.data, cloneRoomInfo.orgId, cloneRoomInfo)
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(cloneRoomInfo.key);
166
- cloneRoomInfo.asyncId = uuid;
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, ws, requestData.data, cloneRoomInfo);
168
+ this.emit(requestData.module, socket, requestData.data, socketInfo);
172
169
  }
173
170
 
174
171
  } catch(e) {
@@ -176,40 +173,40 @@ class SocketServer extends EventEmitter{
176
173
  }
177
174
  }
178
175
 
179
- broadcast(ws, namespace, room, messageType, data, isExact, roomInfo) {
176
+ broadcast(socket, namespace, rooms, messageName, data, socketInfo) {
180
177
  const self = this;
181
- const asyncId = this.getAsyncId(roomInfo)
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: messageType,
181
+ module: messageName,
188
182
  data: data
189
183
  });
190
184
 
191
185
  let isAsync = false;
192
186
  let asyncData = [];
193
- if (asyncId && roomInfo && roomInfo.key) {
187
+ if (asyncId && socketInfo && socketInfo.key) {
194
188
  isAsync = true;
195
189
  }
196
-
197
- if (isExact) {
198
- const clients = this.clients.get(room_key);
199
- // console.log('client-count, room_key', clients, room_key)
200
- // console.log(clients.length)
201
-
202
- if (clients) {
203
- clients.forEach((client) => {
204
- if (ws != client) {
205
- if (isAsync) {
206
- asyncData.push({socket: client, message: responseData})
207
- } else {
208
- client.send(responseData);
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
- self.recordTransfer('out', responseData, namespace)
211
- }
212
- })
208
+ })
209
+ }
213
210
  }
214
211
 
215
212
  } else {
@@ -217,7 +214,7 @@ class SocketServer extends EventEmitter{
217
214
  console.log(value.length, key)
218
215
  if (key.includes(room_key)) {
219
216
  value.forEach(client => {
220
- if (ws != client) {
217
+ if (socket != client || socket == client && data.broadcast_sender != false) {
221
218
  if (isAsync) {
222
219
  asyncData.push({socket: client, message: responseData})
223
220
  } else {
@@ -232,38 +229,38 @@ class SocketServer extends EventEmitter{
232
229
 
233
230
  //. set async processing
234
231
  if (isAsync) {
235
- this.asyncMessages.get(roomInfo.key).setMessage(asyncId, asyncData)
232
+ this.asyncMessages.get(socketInfo.key).setMessage(asyncId, asyncData)
236
233
  }
237
234
 
238
235
  }
239
236
 
240
- send(ws, messageType, data, orgId, roomInfo){
241
- const asyncId = this.getAsyncId(roomInfo)
237
+ send(socket, messageName, data, orgId, socketInfo){
238
+ const asyncId = this.getAsyncId(socketInfo)
242
239
  let responseData = JSON.stringify({
243
- module: messageType,
240
+ module: messageName,
244
241
  data: data
245
242
  });
246
243
 
247
- if (asyncId && roomInfo && roomInfo.key) {
248
- this.asyncMessages.get(roomInfo.key).setMessage(asyncId, [{socket: ws, message: responseData}]);
244
+ if (asyncId && socketInfo && socketInfo.key) {
245
+ this.asyncMessages.get(socketInfo.key).setMessage(asyncId, [{socket, message: responseData}]);
249
246
  } else {
250
- ws.send(responseData);
247
+ socket.send(responseData);
251
248
  }
252
249
  this.recordTransfer('out', responseData, orgId)
253
250
 
254
251
  }
255
252
 
256
- getAsyncId(roomInfo) {
257
- if (!roomInfo) return null;
253
+ getAsyncId(socketInfo) {
254
+ if (!socketInfo) return null;
258
255
 
259
- if (roomInfo.asyncId) {
260
- return roomInfo.asyncId;
256
+ if (socketInfo.asyncId) {
257
+ return socketInfo.asyncId;
261
258
  }
262
259
  return null
263
260
  }
264
261
 
265
- sendBinary(ws, data, orgId) {
266
- ws.send(data, {binary: true});
262
+ sendBinary(socket, data, orgId) {
263
+ socket.send(data, {binary: true});
267
264
  this.recordTransfer('out', data, orgId)
268
265
  }
269
266