Update conversation.html
This commit is contained in:
@@ -403,7 +403,7 @@ if (typeof window.ChatManager === 'undefined') {
|
|||||||
transports: ['websocket', 'polling'],
|
transports: ['websocket', 'polling'],
|
||||||
upgrade: true,
|
upgrade: true,
|
||||||
reconnection: true,
|
reconnection: true,
|
||||||
reconnectionAttempts: 5,
|
reconnectionAttempts: Infinity,
|
||||||
reconnectionDelay: 1000,
|
reconnectionDelay: 1000,
|
||||||
reconnectionDelayMax: 5000,
|
reconnectionDelayMax: 5000,
|
||||||
timeout: 20000,
|
timeout: 20000,
|
||||||
@@ -422,6 +422,16 @@ if (typeof window.ChatManager === 'undefined') {
|
|||||||
socketId: socket.id,
|
socketId: socket.id,
|
||||||
existingSocketId: state.connectionState.socketId
|
existingSocketId: state.connectionState.socketId
|
||||||
});
|
});
|
||||||
|
|
||||||
|
// Rejoin conversation room on reconnection
|
||||||
|
if (state.connectionState.hasJoined) {
|
||||||
|
console.log('Rejoining conversation room:', conversationId);
|
||||||
|
socket.emit('join_conversation', {
|
||||||
|
conversation_id: conversationId,
|
||||||
|
timestamp: new Date().toISOString(),
|
||||||
|
socketId: socket.id
|
||||||
|
});
|
||||||
|
}
|
||||||
});
|
});
|
||||||
|
|
||||||
socket.on('disconnect', function(reason) {
|
socket.on('disconnect', function(reason) {
|
||||||
@@ -431,13 +441,16 @@ if (typeof window.ChatManager === 'undefined') {
|
|||||||
connectionState: state.connectionState
|
connectionState: state.connectionState
|
||||||
});
|
});
|
||||||
state.connectionState.isConnected = false;
|
state.connectionState.isConnected = false;
|
||||||
state.connectionState.hasJoined = false;
|
|
||||||
state.connectionState.socketId = null;
|
state.connectionState.socketId = null;
|
||||||
|
// Don't set hasJoined to false to allow reconnection
|
||||||
|
});
|
||||||
|
|
||||||
|
socket.on('connect_error', function(error) {
|
||||||
|
console.error('Connection error:', error);
|
||||||
});
|
});
|
||||||
|
|
||||||
socket.on('error', function(error) {
|
socket.on('error', function(error) {
|
||||||
console.error('Socket error:', error);
|
console.error('Socket error:', error);
|
||||||
cleanup();
|
|
||||||
});
|
});
|
||||||
|
|
||||||
instance = {
|
instance = {
|
||||||
@@ -547,7 +560,7 @@ $(document).ready(function() {
|
|||||||
}
|
}
|
||||||
scrollToBottom();
|
scrollToBottom();
|
||||||
|
|
||||||
// Message handling with deduplication
|
// Message handling with deduplication and reconnection handling
|
||||||
socket.on('new_message', function(message) {
|
socket.on('new_message', function(message) {
|
||||||
const timestamp = new Date().toISOString();
|
const timestamp = new Date().toISOString();
|
||||||
const messageKey = `${message.id}-${socket.id}`;
|
const messageKey = `${message.id}-${socket.id}`;
|
||||||
@@ -595,7 +608,7 @@ $(document).ready(function() {
|
|||||||
}
|
}
|
||||||
});
|
});
|
||||||
|
|
||||||
// Handle message form submission
|
// Handle message form submission with better error handling
|
||||||
let isSubmitting = false;
|
let isSubmitting = false;
|
||||||
$('#messageForm').off('submit').on('submit', function(e) {
|
$('#messageForm').off('submit').on('submit', function(e) {
|
||||||
e.preventDefault();
|
e.preventDefault();
|
||||||
@@ -606,12 +619,6 @@ $(document).ready(function() {
|
|||||||
return false;
|
return false;
|
||||||
}
|
}
|
||||||
|
|
||||||
if (!state.connectionState.isConnected) {
|
|
||||||
console.error('Socket not connected, cannot send message');
|
|
||||||
alert('Connection lost. Please refresh the page.');
|
|
||||||
return false;
|
|
||||||
}
|
|
||||||
|
|
||||||
const messageInput = $('#messageInput');
|
const messageInput = $('#messageInput');
|
||||||
const submitButton = $('#messageForm button[type="submit"]');
|
const submitButton = $('#messageForm button[type="submit"]');
|
||||||
const message = messageInput.val().trim();
|
const message = messageInput.val().trim();
|
||||||
@@ -639,7 +646,6 @@ $(document).ready(function() {
|
|||||||
formData.append('csrf_token', $('input[name="csrf_token"]').val());
|
formData.append('csrf_token', $('input[name="csrf_token"]').val());
|
||||||
formData.append('socket_id', socket.id);
|
formData.append('socket_id', socket.id);
|
||||||
|
|
||||||
// Append each file to the FormData
|
|
||||||
files.forEach((file, index) => {
|
files.forEach((file, index) => {
|
||||||
formData.append(`file_${index}`, file);
|
formData.append(`file_${index}`, file);
|
||||||
});
|
});
|
||||||
@@ -660,6 +666,11 @@ $(document).ready(function() {
|
|||||||
messageInput.val('');
|
messageInput.val('');
|
||||||
fileInput.value = '';
|
fileInput.value = '';
|
||||||
$('#selectedFiles').text('');
|
$('#selectedFiles').text('');
|
||||||
|
|
||||||
|
// If socket is disconnected, append message directly
|
||||||
|
if (!state.connectionState.isConnected) {
|
||||||
|
appendMessage(response.message);
|
||||||
|
}
|
||||||
}
|
}
|
||||||
},
|
},
|
||||||
error: function(xhr, status, error) {
|
error: function(xhr, status, error) {
|
||||||
|
|||||||
Reference in New Issue
Block a user