just-another-http-api 1.2.4 → 1.2.5
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/package.json +1 -1
- package/utils/WebsocketGroup.js +54 -13
- package/utils/websocketHandler.js +8 -2
package/package.json
CHANGED
|
@@ -1,6 +1,6 @@
|
|
|
1
1
|
{
|
|
2
2
|
"name": "just-another-http-api",
|
|
3
|
-
"version": "1.2.
|
|
3
|
+
"version": "1.2.5",
|
|
4
4
|
"description": "A framework built on top of fastify aimed at removing the need for any network or server configuration. ",
|
|
5
5
|
"homepage": "https://github.com/OllieEdge/just-another-http-api#readme",
|
|
6
6
|
"repository": {
|
package/utils/WebsocketGroup.js
CHANGED
|
@@ -3,35 +3,55 @@ const { redis } = require ( 'eip-cloud-services' );
|
|
|
3
3
|
|
|
4
4
|
class WebsocketGroup {
|
|
5
5
|
|
|
6
|
-
constructor ( groupName ) {
|
|
6
|
+
constructor ( groupName, destroy, messageReceivedHandler, connectionClosedHandler ) {
|
|
7
7
|
this.groupName = groupName;
|
|
8
|
+
this.destroy = destroy;
|
|
8
9
|
this.connections = new Map ();
|
|
10
|
+
this.messageReceivedHandler = messageReceivedHandler || ( async () => {} );
|
|
11
|
+
this.connectionClosedHandler = connectionClosedHandler;
|
|
9
12
|
}
|
|
10
13
|
|
|
11
14
|
async initialize () {
|
|
12
15
|
try {
|
|
13
|
-
await redis.subscribe ( `${this.groupName}
|
|
14
|
-
await redis.subscribe ( `${this.groupName}
|
|
16
|
+
await redis.subscribe ( `${this.groupName}_individualMessage`, this.#handleIndividualMessage.bind ( this ) );
|
|
17
|
+
await redis.subscribe ( `${this.groupName}_broadcast`, this.#broadcastMessageToClients.bind ( this ) );
|
|
18
|
+
await redis.subscribe ( `${this.groupName}_messageReceived`, this.#handleUserMessage.bind ( this ) );
|
|
15
19
|
}
|
|
16
20
|
catch ( error ){
|
|
17
|
-
console.
|
|
21
|
+
console.error ( error );
|
|
18
22
|
}
|
|
19
23
|
}
|
|
20
24
|
|
|
21
|
-
|
|
22
|
-
|
|
25
|
+
async #handleUserMessage ( jsonMessage ) {
|
|
26
|
+
const userMessage = JSON.parse ( jsonMessage );
|
|
27
|
+
await this.messageReceivedHandler ( userMessage );
|
|
23
28
|
}
|
|
24
29
|
|
|
25
|
-
|
|
26
|
-
// don't really need to do anything with this message.
|
|
27
|
-
}
|
|
28
|
-
|
|
29
|
-
broadcastMessageToClients ( message ) {
|
|
30
|
+
#broadcastMessageToClients ( message ) {
|
|
30
31
|
this.connections.forEach ( conn => {
|
|
31
|
-
conn.socket.send ( JSON.stringify ( message ) );
|
|
32
|
+
conn.socket.send ( typeof message === 'string' ? message : JSON.stringify ( message ) );
|
|
32
33
|
} );
|
|
33
34
|
}
|
|
34
35
|
|
|
36
|
+
#handleIndividualMessage ( individualMessage ) {
|
|
37
|
+
const { connectionId, message } = JSON.parse ( individualMessage );
|
|
38
|
+
if(this.connections.has(connectionId)) {
|
|
39
|
+
this.connections.get(connectionId).socket.send(message);
|
|
40
|
+
}
|
|
41
|
+
}
|
|
42
|
+
|
|
43
|
+
getConnections () {
|
|
44
|
+
return this.connections;
|
|
45
|
+
}
|
|
46
|
+
|
|
47
|
+
individualMessage ( connectionId, message ) {
|
|
48
|
+
redis.publish ( `${this.groupName}_individualMessage`, JSON.stringify ( { connectionId, message } ) );
|
|
49
|
+
}
|
|
50
|
+
|
|
51
|
+
broadcastMessage ( message ) {
|
|
52
|
+
redis.publish ( `${this.groupName}_broadcast`, JSON.stringify ( message ) );
|
|
53
|
+
}
|
|
54
|
+
|
|
35
55
|
addNewConnection ( connection, connectionId = crypto.randomUUID () ) {
|
|
36
56
|
this.connections.set ( connectionId, connection );
|
|
37
57
|
|
|
@@ -46,15 +66,36 @@ class WebsocketGroup {
|
|
|
46
66
|
|
|
47
67
|
connection.socket.on ( 'close', () => {
|
|
48
68
|
this.connections.delete ( connectionId );
|
|
69
|
+
this.#clean(connectionId);
|
|
49
70
|
} );
|
|
50
|
-
|
|
71
|
+
|
|
51
72
|
connection.socket.on ( 'error', error => {
|
|
52
73
|
console.error ( 'WebSocket error:', error );
|
|
53
74
|
this.connections.delete ( connectionId );
|
|
75
|
+
this.#clean(connectionId);
|
|
54
76
|
} );
|
|
55
77
|
|
|
56
78
|
return { connectionId, groupName: this.groupName, connection };
|
|
57
79
|
}
|
|
80
|
+
|
|
81
|
+
async #clean (connectionId) {
|
|
82
|
+
if ( this.connections.size === 0 ) {
|
|
83
|
+
this.connections.clear ();
|
|
84
|
+
await redis.unsubscribe ( `${this.groupName}_individualMessage`, this.#handleIndividualMessage.bind ( this ) );
|
|
85
|
+
await redis.unsubscribe ( `${this.groupName}_broadcast`, this.#broadcastMessageToClients.bind ( this ) );
|
|
86
|
+
await redis.unsubscribe ( `${this.groupName}_messageReceived`, this.#handleUserMessage.bind ( this ) );
|
|
87
|
+
this.destroy(this.groupName);
|
|
88
|
+
}
|
|
89
|
+
|
|
90
|
+
if ( this.connectionClosedHandler !== null && typeof this.connectionClosedHandler === 'function') {
|
|
91
|
+
try{
|
|
92
|
+
await this.connectionClosedHandler ( {groupName:this.groupName, connectionId} );
|
|
93
|
+
}
|
|
94
|
+
catch (error) {
|
|
95
|
+
console.error('Error in connectionClosedHandler:', error);
|
|
96
|
+
}
|
|
97
|
+
}
|
|
98
|
+
}
|
|
58
99
|
}
|
|
59
100
|
|
|
60
101
|
module.exports = WebsocketGroup;
|
|
@@ -2,13 +2,19 @@ const WebsocketGroup = require ( './WebsocketGroup' );
|
|
|
2
2
|
|
|
3
3
|
const websocketGroups = {};
|
|
4
4
|
|
|
5
|
-
const getGroupInstance = async ( groupName ) => {
|
|
5
|
+
const getGroupInstance = async ( groupName, messageReceivedHandler, connectionClosedHandler ) => {
|
|
6
6
|
if ( !websocketGroups[ groupName ] ) {
|
|
7
|
-
websocketGroups[ groupName ] = new WebsocketGroup ( groupName );
|
|
7
|
+
websocketGroups[ groupName ] = new WebsocketGroup ( groupName, removeGroupInstance, messageReceivedHandler, connectionClosedHandler );
|
|
8
8
|
await websocketGroups[ groupName ].initialize ();
|
|
9
9
|
}
|
|
10
10
|
|
|
11
11
|
return websocketGroups[ groupName ];
|
|
12
12
|
};
|
|
13
13
|
|
|
14
|
+
const removeGroupInstance = async groupName => {
|
|
15
|
+
if ( websocketGroups[ groupName ] ) {
|
|
16
|
+
delete websocketGroups[ groupName ];
|
|
17
|
+
}
|
|
18
|
+
}
|
|
19
|
+
|
|
14
20
|
module.exports = getGroupInstance;
|