-
Notifications
You must be signed in to change notification settings - Fork 0
/
Copy pathindex copy.js
217 lines (179 loc) · 6.37 KB
/
index copy.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
const express = require("express");
const { Observable } = require("rxjs");
const isEqual = require("lodash/isEqual");
class SignallingServer {
constructor(port) {
this.app = express();
this.port = port || 3000;
this.iceCandidates = [];
this.answers = "";
this.offer = "";
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 array
const createIceCandidatesChangeStream = () => {
let lastCandidateIndex = this.iceCandidates.length;
return new Observable((subscriber) => {
const checkArrayChange = () => {
if (this.iceCandidates.length > lastCandidateIndex) {
const newCandidate = this.iceCandidates[lastCandidateIndex];
lastCandidateIndex = this.iceCandidates.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 variable
const createAnswerChangeStream = () => {
let currentAnswer = this.answer;
return new Observable((subscriber) => {
const checkAnswerChange = () => {
if (this.answer !== currentAnswer) {
currentAnswer = this.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 variable
const createOfferChangeStream = () => {
let currentOffer = this.offer;
return new Observable((subscriber) => {
const checkOfferChange = () => {
if (this.offer !== currentOffer) {
currentOffer = this.offer;
subscriber.next(currentOffer); // Emit the new offer
}
};
const intervalId = setInterval(checkOfferChange, 100); // Check for changes periodically
return () => clearInterval(intervalId);
});
};
// Initialize streams
const iceCandidatesChangeStream = createIceCandidatesChangeStream();
const answerChangeStream = createAnswerChangeStream();
const offerChangeStream = createOfferChangeStream();
// SSE endpoint for clients to subscribe to iceCandidates changes
app.get("/subscribe-iceCandidates", (req, res) => {
res.setHeader("Content-Type", "text/event-stream");
res.setHeader("Cache-Control", "no-cache");
res.setHeader("Connection", "keep-alive");
res.flushHeaders();
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, true);
req.on("close", () => {
subscription.unsubscribe();
this.iceCandidatesSubscriptions.delete(subscription);
res.end();
});
});
// SSE endpoint for clients to subscribe to answer changes
app.get("/subscribe-answer", (req, res) => {
res.setHeader("Content-Type", "text/event-stream");
res.setHeader("Cache-Control", "no-cache");
res.setHeader("Connection", "keep-alive");
res.flushHeaders();
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, true);
req.on("close", () => {
subscription.unsubscribe();
this.answerSubscriptions.delete(subscription);
res.end();
});
});
// SSE endpoint for clients to subscribe to offer changes
app.get("/subscribe-offer", (req, res) => {
res.setHeader("Content-Type", "text/event-stream");
res.setHeader("Cache-Control", "no-cache");
res.setHeader("Connection", "keep-alive");
res.flushHeaders();
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, true);
req.on("close", () => {
subscription.unsubscribe();
this.offerSubscriptions.delete(subscription);
res.end();
});
});
// Endpoint to add an ICE candidate
app.post("/add-iceCandidate", (req, res) => {
const { value } = req.body; // Expecting a map with keys candidate, sdpMid, sdpMLineIndex
const { candidate, sdpMid, sdpMLineIndex } = value;
this.iceCandidates.push({ candidate, sdpMid, sdpMLineIndex });
res.send(
`Ice candidate added: ${JSON.stringify({
candidate,
sdpMid,
sdpMLineIndex,
})}`
);
});
// Endpoint to set the answer variable
app.post("/set-answer", (req, res) => {
const { value } = req.body;
this.answer = value;
res.send(`Answer set to: ${this.answer}`);
});
// Endpoint to set the offer variable
app.post("/set-offer", (req, res) => {
const { value } = req.body;
this.offer = value;
res.send(`Offer set to: ${this.offer}`);
});
// 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);