-
Notifications
You must be signed in to change notification settings - Fork 0
/
Copy pathindex.js
236 lines (196 loc) · 7.3 KB
/
index.js
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
61
62
63
64
65
66
67
68
69
70
71
72
73
74
75
76
77
78
79
80
81
82
83
84
85
86
87
88
89
90
91
92
93
94
95
96
97
98
99
100
101
102
103
104
105
106
107
108
109
110
111
112
113
114
115
116
117
118
119
120
121
122
123
124
125
126
127
128
129
130
131
132
133
134
135
136
137
138
139
140
141
142
143
144
145
146
147
148
149
150
151
152
153
154
155
156
157
158
159
160
161
162
163
164
165
166
167
168
169
170
171
172
173
174
175
176
177
178
179
180
181
182
183
184
185
186
187
188
189
190
191
192
193
194
195
196
197
198
199
200
201
202
203
204
205
206
207
208
209
210
211
212
213
214
215
216
217
218
219
220
221
222
223
224
225
226
227
228
229
230
231
232
233
234
235
236
const express = require("express");
const { Observable } = require("rxjs");
const isEqual = require("lodash/isEqual");
/**
* This is the signalling server i created, now it does seem to working according to my tests with postman, although i dont really get http with the dart client
* so please check
*/
class SignallingServer {
constructor(port) {
this.app = express();
this.port = port || 3000;
this.iceCandidates = new Map(); // Map keyed by userId
this.answers = new Map(); // Map keyed by userId
this.offer = new Map(); // Map keyed by userId
this.iceCandidatesSubscriptions = new Map();
this.answerSubscriptions = new Map();
this.offerSubscriptions = new Map();
this.nextClientId = 0;
this.setupRoutes();
}
setupRoutes() {
const app = this.app;
// Middleware to parse JSON body
app.use(express.json());
// Function to create a subscribable stream for the iceCandidates map
const createIceCandidatesChangeStream = (userId) => {
let lastCandidateIndex = this.iceCandidates.get(userId)
? this.iceCandidates.get(userId).length
: 0;
return new Observable((subscriber) => {
const checkArrayChange = () => {
const candidates = this.iceCandidates.get(userId) || [];
if (candidates.length > lastCandidateIndex) {
const newCandidate = candidates[lastCandidateIndex];
lastCandidateIndex = candidates.length;
subscriber.next(newCandidate); // Emit the new candidate
}
};
const intervalId = setInterval(checkArrayChange, 100); // Check for changes periodically
return () => clearInterval(intervalId);
});
};
// Function to create a subscribable stream for the answer map
const createAnswerChangeStream = (userId) => {
let currentAnswer = this.answers.get(userId) || "";
return new Observable((subscriber) => {
const checkAnswerChange = () => {
const answer = this.answers.get(userId) || "";
if (answer !== currentAnswer) {
currentAnswer = answer;
subscriber.next(currentAnswer); // Emit the new answer
}
};
const intervalId = setInterval(checkAnswerChange, 100); // Check for changes periodically
return () => clearInterval(intervalId);
});
};
// Function to create a subscribable stream for the offer map
const createOfferChangeStream = (userId) => {
let currentOffer = this.offer.get(userId) || "";
return new Observable((subscriber) => {
const checkOfferChange = () => {
const offer = this.offer.get(userId) || "";
if (offer !== currentOffer) {
currentOffer = offer;
subscriber.next(currentOffer); // Emit the new offer
}
};
const intervalId = setInterval(checkOfferChange, 100); // Check for changes periodically
return () => clearInterval(intervalId);
});
};
// SSE endpoint for clients to subscribe to iceCandidates changes
app.get("/subscribe-iceCandidates/:userId", (req, res) => {
const { userId } = req.params;
res.setHeader("Content-Type", "text/event-stream");
res.setHeader("Cache-Control", "no-cache");
res.setHeader("Connection", "keep-alive");
res.flushHeaders();
const iceCandidatesChangeStream = createIceCandidatesChangeStream(userId);
const subscription = iceCandidatesChangeStream.subscribe({
next: (newArray) => {
res.write(`data: ${JSON.stringify(newArray)}\n\n`);
},
error: (err) => {
console.error("Error:", err);
res.end();
},
complete: () => {
res.end();
},
});
this.iceCandidatesSubscriptions.set(subscription, userId);
req.on("close", () => {
subscription.unsubscribe();
this.iceCandidatesSubscriptions.delete(subscription);
res.end();
});
});
// SSE endpoint for clients to subscribe to answer changes
app.get("/subscribe-answer/:userId", (req, res) => {
const { userId } = req.params;
res.setHeader("Content-Type", "text/event-stream");
res.setHeader("Cache-Control", "no-cache");
res.setHeader("Connection", "keep-alive");
res.flushHeaders();
const answerChangeStream = createAnswerChangeStream(userId);
const subscription = answerChangeStream.subscribe({
next: (newAnswer) => {
res.write(`data: ${JSON.stringify(newAnswer)}\n\n`);
},
error: (err) => {
console.error("Error:", err);
res.end();
},
complete: () => {
res.end();
},
});
this.answerSubscriptions.set(subscription, userId);
req.on("close", () => {
subscription.unsubscribe();
this.answerSubscriptions.delete(subscription);
res.end();
});
});
// SSE endpoint for clients to subscribe to offer changes
app.get("/subscribe-offer/:userId", (req, res) => {
const { userId } = req.params;
res.setHeader("Content-Type", "text/event-stream");
res.setHeader("Cache-Control", "no-cache");
res.setHeader("Connection", "keep-alive");
res.flushHeaders();
const offerChangeStream = createOfferChangeStream(userId);
const subscription = offerChangeStream.subscribe({
next: (newOffer) => {
res.write(`data: ${JSON.stringify(newOffer)}\n\n`);
},
error: (err) => {
console.error("Error:", err);
res.end();
},
complete: () => {
res.end();
},
});
this.offerSubscriptions.set(subscription, userId);
req.on("close", () => {
subscription.unsubscribe();
this.offerSubscriptions.delete(subscription);
res.end();
});
});
// Endpoint to add an ICE candidate
app.post("/add-iceCandidate/:userId", (req, res) => {
const { userId } = req.params;
const { value } = req.body; // Expecting a map with keys candidate, sdpMid, sdpMLineIndex
const { candidate, sdpMid, sdpMLineIndex } = value;
if (!this.iceCandidates.has(userId)) {
this.iceCandidates.set(userId, []);
}
this.iceCandidates.get(userId).push({ candidate, sdpMid, sdpMLineIndex });
res.send(
`Ice candidate added: ${JSON.stringify({
candidate,
sdpMid,
sdpMLineIndex,
})}`
);
});
// Endpoint to set the answer variable
app.post("/set-answer/:userId", (req, res) => {
const { userId } = req.params;
const { value } = req.body;
this.answers.set(userId, value);
res.send(`Answer set to: ${this.answers.get(userId)}`);
});
// Endpoint to set the offer variable
app.post("/set-offer/:userId", (req, res) => {
const { userId } = req.params;
const { value } = req.body;
this.offer.set(userId, value);
res.send(`Offer set to: ${this.offer.get(userId)}`);
});
// Default route
app.get("/", (req, res) => {
res.send("Welcome to the server");
});
// Start the server
this.app.listen(this.port, () => {
console.log(`Signalling Server running at http://localhost:${this.port}`);
});
}
}
// Create an instance of SignallingServer and start listening
const signallingServer = new SignallingServer(3000);