Skip to content

Commit 46d98a0

Browse files
authored
Update script.js
1 parent fd43e64 commit 46d98a0

File tree

1 file changed

+45
-32
lines changed

1 file changed

+45
-32
lines changed

script.js

Lines changed: 45 additions & 32 deletions
Original file line numberDiff line numberDiff line change
@@ -1,37 +1,50 @@
11
async function startSignalR() {
2-
try {
3-
// Stap 1: Vraag negotiate-informatie op van je Function App
4-
const response = await fetch("https://chatproxy.azurewebsites.net/api/negotiate", {
5-
method: "POST"
6-
});
7-
8-
if (!response.ok) {
9-
throw new Error(`Negotiate failed: ${response.status}`);
10-
}
11-
12-
const connectionInfo = await response.json();
13-
14-
// Stap 2: Bouw de verbinding met het opgehaalde URL + token
15-
const connection = new signalR.HubConnectionBuilder()
16-
.withUrl(connectionInfo.url, {
17-
accessTokenFactory: () => connectionInfo.accessToken // ⬅️ essentieel
18-
})
19-
.configureLogging(signalR.LogLevel.Information)
20-
.build();
21-
22-
// Stap 3: Stel eventhandler in
23-
connection.on("newMessage", message => {
24-
appendMessage("assistant", message);
25-
});
26-
27-
// Stap 4: Start de verbinding
28-
await connection.start();
29-
console.log("✅ Verbonden met SignalR hub");
30-
} catch (err) {
31-
console.error("❌ SignalR fout:", err);
32-
appendMessage("assistant", "⚠️ Verbinden met SignalR is mislukt.");
2+
try {
3+
// Stap 1: Vraag negotiate-informatie op van je Azure Function
4+
const response = await fetch("https://chatproxy.azurewebsites.net/api/negotiate", {
5+
method: "POST"
6+
});
7+
8+
if (!response.ok) {
9+
throw new Error(`Negotiate failed: ${response.status}`);
10+
}
11+
12+
const connectionInfo = await response.json();
13+
14+
// Logging: check de opgehaalde data
15+
console.log("🔗 Verbinding URL:", connectionInfo.url);
16+
console.log("🔑 Token start:", connectionInfo.accessToken?.slice(0, 30) + "...");
17+
18+
// Stap 2: Bouw de SignalR-verbinding
19+
const connection = new signalR.HubConnectionBuilder()
20+
.withUrl(connectionInfo.url, {
21+
accessTokenFactory: () => connectionInfo.accessToken
22+
})
23+
.configureLogging(signalR.LogLevel.Information)
24+
.build();
25+
26+
// Stap 3: Event handler voor binnenkomende berichten
27+
connection.on("newMessage", message => {
28+
if (typeof appendMessage === "function") {
29+
appendMessage("assistant", message);
30+
} else {
31+
console.log("📩 Bericht ontvangen (fallback):", message);
32+
}
33+
});
34+
35+
// Stap 4: Start de verbinding
36+
await connection.start();
37+
console.log("✅ Verbonden met SignalR hub");
38+
39+
} catch (err) {
40+
console.error("❌ SignalR fout:", err);
41+
if (typeof appendMessage === "function") {
42+
appendMessage("assistant", "⚠️ Verbinden met SignalR is mislukt.");
43+
} else {
44+
console.warn("⚠️ Verbinding mislukt (fallback):", err.message);
3345
}
46+
}
3447
}
3548

36-
// Start verbinding bij laden
49+
// Start de verbinding zodra het script geladen wordt
3750
startSignalR();

0 commit comments

Comments
 (0)