|
| 1 | +let connection; |
| 2 | + |
1 | 3 | async function startSignalR() { |
2 | | - try { |
3 | | - // Ophalen van connectionInfo vanaf jouw negotiate endpoint |
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 | | - // Bouw de echte SignalR verbinding naar Azure SignalR |
15 | | - const connection = new signalR.HubConnectionBuilder() |
16 | | - .withUrl("https://chatproxysignalr.service.signalr.net/client/?hub=chat", { |
17 | | - accessTokenFactory: () => connectionInfo.accessToken |
18 | | - }) |
19 | | - .configureLogging(signalR.LogLevel.Information) |
20 | | - .build(); |
21 | | - |
22 | | - // Eventhandler |
23 | | - connection.on("newMessage", message => { |
24 | | - appendMessage("assistant", message); |
25 | | - }); |
26 | | - |
27 | | - // Start 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 | | - } |
| 4 | + try { |
| 5 | + // Haal token en URL op van negotiate backend |
| 6 | + const negotiateResponse = await fetch("/api/negotiate", { method: "POST" }); |
| 7 | + const negotiateData = await negotiateResponse.json(); |
| 8 | + |
| 9 | + connection = new signalR.HubConnectionBuilder() |
| 10 | + .withUrl(negotiateData.url, { |
| 11 | + accessTokenFactory: () => negotiateData.accessToken |
| 12 | + }) |
| 13 | + .withAutomaticReconnect() |
| 14 | + .configureLogging(signalR.LogLevel.Information) |
| 15 | + .build(); |
| 16 | + |
| 17 | + // Ontvangen bericht van backend |
| 18 | + connection.on("newToken", (data) => { |
| 19 | + console.log("✅ Ontvangen van SignalR:", data); |
| 20 | + appendMessage("agent", data); // Tekstbericht toevoegen aan chatvenster |
| 21 | + }); |
| 22 | + |
| 23 | + await connection.start(); |
| 24 | + console.log("✅ Verbonden met SignalR"); |
| 25 | + } catch (err) { |
| 26 | + console.error("❌ SignalR fout:", err); |
| 27 | + } |
34 | 28 | } |
35 | 29 |
|
36 | | -startSignalR(); |
| 30 | +function appendMessage(sender, text) { |
| 31 | + const chat = document.getElementById("chat"); |
| 32 | + const msgDiv = document.createElement("div"); |
| 33 | + msgDiv.classList.add(sender === "user" ? "user-message" : "bot-message"); |
| 34 | + msgDiv.innerText = text; |
| 35 | + chat.appendChild(msgDiv); |
| 36 | + chat.scrollTop = chat.scrollHeight; |
| 37 | +} |
| 38 | + |
| 39 | +// Verstuur bericht naar backend via fetch (start response) |
| 40 | +document.getElementById("input-form").addEventListener("submit", async function (e) { |
| 41 | + e.preventDefault(); |
| 42 | + const input = document.getElementById("user-input"); |
| 43 | + const message = input.value.trim(); |
| 44 | + if (!message) return; |
| 45 | + |
| 46 | + appendMessage("user", message); |
| 47 | + input.value = ""; |
| 48 | + |
| 49 | + try { |
| 50 | + await fetch("/api/chatproxy", { |
| 51 | + method: "POST", |
| 52 | + headers: { |
| 53 | + "Content-Type": "application/json" |
| 54 | + }, |
| 55 | + body: JSON.stringify({ message: message }) |
| 56 | + }); |
| 57 | + } catch (error) { |
| 58 | + console.error("❌ Fout bij versturen bericht:", error); |
| 59 | + appendMessage("agent", "⚠️ Er ging iets mis bij het verzenden."); |
| 60 | + } |
| 61 | +}); |
| 62 | + |
| 63 | +window.onload = () => { |
| 64 | + startSignalR(); |
| 65 | +}; |
0 commit comments