Connection and messages are now correct and fluently pass between client and server.

This commit is contained in:
Josako
2024-05-23 12:11:23 +02:00
parent 54e2297399
commit 06333bf8de
5 changed files with 82 additions and 32 deletions

View File

@@ -1,4 +1,3 @@
// static/js/eveai-chat-widget.js
class EveAIChatWidget extends HTMLElement {
static get observedAttributes() {
return ['tenant-id', 'api-key', 'domain'];
@@ -8,6 +7,7 @@ class EveAIChatWidget extends HTMLElement {
super();
this.socket = null; // Initialize socket to null
this.attributesSet = false; // Flag to check if all attributes are set
this.jwtToken = null; // Initialize jwtToken to null
console.log('EveAIChatWidget constructor called');
}
@@ -76,31 +76,36 @@ class EveAIChatWidget extends HTMLElement {
}
console.log(`Initializing socket connection to ${this.domain}`);
const token = 'Bearer ' + this.apiKey
// Include tenantId in query parameters
// Ensure apiKey is passed in the query parameters
this.socket = io(this.domain, {
path: '/chat/socket.io/',
transports: ['websocket', 'polling'],
auth: {
token: token // Add the token to the authentication object
},
query: {
tenantId: this.tenantId,
// apiKey: this.apiKey
apiKey: this.apiKey // Ensure apiKey is included here
},
auth: {
token: 'Bearer ' + this.apiKey // Ensure token is included here
}
});
this.socket.on('connect', () => {
this.socket.on('connect', (data) => {
console.log('Socket connected');
});
this.socket.on('authenticated', (data) => {
console.log('Authenticated event received: ', data);
if (data.token) {
this.jwtToken = data.token; // Store the JWT token received from the server
}
});
this.socket.on('connect_error', (err) => {
console.error('Socket connection error:', err);
});
this.socket.on('connect_timeout', () => {
console.error('Socket connection timeout')
console.error('Socket connection timeout');
});
this.socket.on('disconnect', () => {
@@ -158,8 +163,12 @@ class EveAIChatWidget extends HTMLElement {
console.error('Socket is not initialized');
return;
}
if (!this.jwtToken) {
console.error('JWT token is not available');
return;
}
console.log('Sending message to backend');
this.socket.emit('user_message', { tenantId: this.tenantId, apiKey: this.apiKey, message });
this.socket.emit('user_message', { tenantId: this.tenantId, token: this.jwtToken, message });
}
}
@@ -169,4 +178,4 @@ function handleFeedback(messageId, feedback) {
// Send feedback to the backend
console.log(`Feedback for ${messageId}: ${feedback}`);
// Implement the actual feedback mechanism
}
}

View File

@@ -4,6 +4,7 @@ class EveAI {
this.tenantId = tenantId;
this.apiKey = apiKey;
this.domain = domain;
console.log('EveAI constructor:', { tenantId, apiKey, domain });
}