Skip to content

Commit fd43e64

Browse files
authored
Update script.js
1 parent f174a81 commit fd43e64

File tree

1 file changed

+28
-33
lines changed

1 file changed

+28
-33
lines changed

script.js

Lines changed: 28 additions & 33 deletions
Original file line numberDiff line numberDiff line change
@@ -1,42 +1,37 @@
11
async function startSignalR() {
2-
try {
3-
// Stap 1: Haal negotiate-informatie op
4-
const negotiateResponse = await fetch("https://chatproxy.azurewebsites.net/api/negotiate", {
5-
method: "POST"
6-
});
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+
});
77

8-
if (!negotiateResponse.ok) {
9-
throw new Error(`Negotiate failed: ${negotiateResponse.status}`);
10-
}
11-
12-
const negotiateData = await negotiateResponse.json();
13-
console.log("🔐 Negotiation gelukt:", negotiateData);
8+
if (!response.ok) {
9+
throw new Error(`Negotiate failed: ${response.status}`);
10+
}
1411

15-
// Stap 2: Start de SignalR-verbinding
16-
const connection = new signalR.HubConnectionBuilder()
17-
.withUrl("https://chatproxysignalr.service.signalr.net/client/?hub=chat", {
18-
accessTokenFactory: () => negotiateData.accessToken
19-
})
20-
.configureLogging(signalR.LogLevel.Information)
21-
.build();
12+
const connectionInfo = await response.json();
2213

23-
// Stap 3: Ontvang berichten van de backend
24-
connection.on("newToken", token => {
25-
const chat = document.getElementById("chat");
26-
const streamOutput = document.getElementById("streamOutput");
27-
if (streamOutput && chat) {
28-
streamOutput.textContent += token;
29-
chat.scrollTop = chat.scrollHeight;
30-
}
31-
});
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();
3221

33-
// Stap 4: Start verbinding
34-
await connection.start();
35-
console.log("✅ Verbonden met SignalR");
22+
// Stap 3: Stel eventhandler in
23+
connection.on("newMessage", message => {
24+
appendMessage("assistant", message);
25+
});
3626

37-
} catch (err) {
38-
console.error("❌ SignalR fout:", err);
39-
}
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.");
33+
}
4034
}
4135

36+
// Start verbinding bij laden
4237
startSignalR();

0 commit comments

Comments
 (0)