Skip to content

Commit f0909b5

Browse files
author
Kush Bisen
authored
Merge pull request #18 from SolidLabResearch/14-implement-usage-of-the-same-websocket-channel-if-already-subscribed-to-a-resource
fixes #14
2 parents 15f1b69 + 476d81d commit f0909b5

File tree

1 file changed

+23
-8
lines changed

1 file changed

+23
-8
lines changed

src/server/WebSocketServerHandler.ts

Lines changed: 23 additions & 8 deletions
Original file line numberDiff line numberDiff line change
@@ -5,13 +5,13 @@ import { SubscribeNotification } from '../service/SubscribeNotification';
55
export class WebSocketServerHandler {
66

77
public websocket_server: any;
8-
public websocket_connections: Map<string, WebSocket>;
8+
public websocket_connections: Map<string, WebSocket[]>;
99
public subscribe_notification: SubscribeNotification;
1010

1111

1212
constructor(websocket_server: WebSocket.server) {
1313
this.websocket_server = websocket_server;
14-
this.websocket_connections = new Map<string, WebSocket>();
14+
this.websocket_connections = new Map<string, WebSocket[]>();
1515
this.subscribe_notification = new SubscribeNotification();
1616
}
1717

@@ -23,16 +23,14 @@ export class WebSocketServerHandler {
2323

2424
this.websocket_server.on('request', (request: any) => {
2525
const connection = request.accept('solid-stream-notifications-aggregator', request.origin);
26-
connection.on('message', (message: any) => {
26+
connection.on('message', async(message: any) => {
2727
if (message.type === 'utf8') {
2828
const message_utf8 = message.utf8Data;
2929
const ws_message = JSON.parse(message_utf8);
3030
if (Object.keys(ws_message).includes('subscribe')) {
3131
console.log(`Received a subscribe message from the client.`);
3232
let stream_to_subscribe = ws_message.subscribe;
3333
for (let stream of stream_to_subscribe) {
34-
// We first subscribe to the latest inbox of the LDES stream.
35-
this.subscribe_notification.subscribe_inbox(stream);
3634
console.log(`Subscribed to the stream: ${stream}`);
3735
this.set_connections(stream, connection);
3836
}
@@ -41,7 +39,13 @@ export class WebSocketServerHandler {
4139
console.log(`Received a new event message from the client.`);
4240
let connection = this.websocket_connections.get(ws_message.stream);
4341
if (connection !== undefined) {
44-
connection.send(JSON.stringify(ws_message));
42+
for (const [stream, connections] of this.websocket_connections) {
43+
if (stream == ws_message.stream) {
44+
for (let connection of connections) {
45+
connection.send(JSON.stringify(ws_message));
46+
}
47+
}
48+
}
4549
}
4650
}
4751
else if (Object.keys(ws_message).includes('container_location')) {
@@ -59,7 +63,18 @@ export class WebSocketServerHandler {
5963
});
6064
}
6165

62-
public set_connections(subscribed_stream: string, connection: WebSocket): void {
63-
this.websocket_connections.set(subscribed_stream, connection);
66+
public set_connections(subscribed_stream: string, connection: WebSocket){
67+
if (!this.websocket_connections.has(subscribed_stream)) {
68+
this.subscribe_notification.subscribe_inbox(subscribed_stream);
69+
this.websocket_connections.set(subscribed_stream, [connection]);
70+
}
71+
else {
72+
const connections = this.websocket_connections.get(subscribed_stream);
73+
if (connections !== undefined) {
74+
connections.push(connection);
75+
this.websocket_connections.set(subscribed_stream, connections);
76+
}
77+
78+
}
6479
}
6580
}

0 commit comments

Comments
 (0)