@@ -2,8 +2,13 @@ let connection;
22
33async function startSignalR ( ) {
44 try {
5- // Haal token en URL op van negotiate backend
6- const negotiateResponse = await fetch ( "/api/negotiate" , { method : "POST" } ) ;
5+ // Gebruik GET in plaats van POST
6+ const negotiateResponse = await fetch ( "/api/negotiate" ) ;
7+
8+ if ( ! negotiateResponse . ok ) {
9+ throw new Error ( "Negotiate endpoint gaf status: " + negotiateResponse . status ) ;
10+ }
11+
712 const negotiateData = await negotiateResponse . json ( ) ;
813
914 connection = new signalR . HubConnectionBuilder ( )
@@ -14,19 +19,21 @@ async function startSignalR() {
1419 . configureLogging ( signalR . LogLevel . Information )
1520 . build ( ) ;
1621
17- // Ontvangen bericht van backend
22+ // Luister naar berichten van backend via SignalR
1823 connection . on ( "newToken" , ( data ) => {
1924 console . log ( "✅ Ontvangen van SignalR:" , data ) ;
20- appendMessage ( "agent" , data ) ; // Tekstbericht toevoegen aan chatvenster
25+ appendMessage ( "agent" , data ) ;
2126 } ) ;
2227
2328 await connection . start ( ) ;
24- console . log ( "✅ Verbonden met SignalR" ) ;
29+ console . log ( "✅ SignalR verbonden " ) ;
2530 } catch ( err ) {
2631 console . error ( "❌ SignalR fout:" , err ) ;
32+ appendMessage ( "agent" , "⚠️ Verbinden met de server is mislukt." ) ;
2733 }
2834}
2935
36+ // Bericht tonen in de chatbox
3037function appendMessage ( sender , text ) {
3138 const chat = document . getElementById ( "chat" ) ;
3239 const msgDiv = document . createElement ( "div" ) ;
@@ -36,7 +43,7 @@ function appendMessage(sender, text) {
3643 chat . scrollTop = chat . scrollHeight ;
3744}
3845
39- // Verstuur bericht naar backend via fetch (start response)
46+ // Verstuur gebruikersbericht naar backend
4047document . getElementById ( "input-form" ) . addEventListener ( "submit" , async function ( e ) {
4148 e . preventDefault ( ) ;
4249 const input = document . getElementById ( "user-input" ) ;
@@ -56,10 +63,11 @@ document.getElementById("input-form").addEventListener("submit", async function
5663 } ) ;
5764 } catch ( error ) {
5865 console . error ( "❌ Fout bij versturen bericht:" , error ) ;
59- appendMessage ( "agent" , "⚠️ Er ging iets mis bij het verzenden." ) ;
66+ appendMessage ( "agent" , "⚠️ Bericht verzenden mislukt ." ) ;
6067 }
6168} ) ;
6269
70+ // Start SignalR bij laden van de pagina
6371window . onload = ( ) => {
6472 startSignalR ( ) ;
6573} ;
0 commit comments