|
1 | | -function appendMessage(sender, message) { |
2 | | - const chat = document.getElementById("chat"); |
3 | | - const msg = document.createElement("div"); |
4 | | - |
5 | | - msg.classList.add("message"); |
6 | | - msg.classList.add(sender === "user" ? "user-message" : "agent-message"); |
7 | | - msg.textContent = message; |
8 | | - |
9 | | - chat.appendChild(msg); |
10 | | - chat.scrollTop = chat.scrollHeight; |
11 | | -} |
12 | | - |
13 | 1 | async function startSignalR() { |
14 | | - try { |
15 | | - const response = await fetch("https://chatproxy.azurewebsites.net/api/negotiate", { |
16 | | - method: "POST" |
17 | | - }); |
18 | | - |
19 | | - if (!response.ok) { |
20 | | - throw new Error(`Negotiate failed: ${response.status}`); |
| 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."); |
21 | 33 | } |
22 | | - |
23 | | - const connectionInfo = await response.json(); |
24 | | - |
25 | | - console.log("🔗 Verbinding URL:", connectionInfo.url); |
26 | | - console.log("🔑 Token start:", connectionInfo.accessToken?.slice(0, 30) + "..."); |
27 | | - |
28 | | - const connection = new signalR.HubConnectionBuilder() |
29 | | - .withUrl(connectionInfo.url, { |
30 | | - accessTokenFactory: () => connectionInfo.accessToken, |
31 | | - skipNegotiation: true, |
32 | | - transport: signalR.HttpTransportType.LongPolling |
33 | | - }) |
34 | | - .configureLogging(signalR.LogLevel.Information) |
35 | | - .build(); |
36 | | - |
37 | | - connection.on("newMessage", message => { |
38 | | - appendMessage("assistant", message); |
39 | | - }); |
40 | | - |
41 | | - await connection.start(); |
42 | | - console.log("✅ Verbonden met SignalR hub"); |
43 | | - |
44 | | - } catch (err) { |
45 | | - console.error("❌ SignalR fout:", err); |
46 | | - appendMessage("assistant", "⚠️ Verbinden met SignalR is mislukt."); |
47 | | - } |
48 | 34 | } |
49 | 35 |
|
50 | | -document.getElementById("input-form").addEventListener("submit", async event => { |
51 | | - event.preventDefault(); |
52 | | - const inputField = document.getElementById("user-input"); |
53 | | - const message = inputField.value.trim(); |
54 | | - if (message === "") return; |
55 | | - |
56 | | - appendMessage("user", message); |
57 | | - inputField.value = ""; |
58 | | - |
59 | | - // Hier kun je optioneel het bericht doorsturen naar je backend met fetch() |
60 | | -}); |
61 | | - |
62 | 36 | startSignalR(); |
0 commit comments