Fix TTS module initialization and dependency issues. Update module IDs for consistency, improve circular dependency detection, and fix UI Controller event handling.
This commit is contained in:
+45
-74
@@ -8,38 +8,38 @@ import { moduleRegistry } from './module-registry.js';
|
||||
class SocketClientModule extends BaseModule {
|
||||
constructor() {
|
||||
super('socket-client', 'Socket Client');
|
||||
|
||||
// Dependencies
|
||||
this.dependencies = ['text-buffer'];
|
||||
|
||||
this.socket = null;
|
||||
this.textBuffer = null;
|
||||
this.isConnected = false;
|
||||
this.reconnectAttempts = 0;
|
||||
this.maxReconnectAttempts = 5;
|
||||
this.reconnectDelay = 2000; // 2 seconds
|
||||
this.reconnectDelay = 2000;
|
||||
this.url = null;
|
||||
this.eventListeners = {};
|
||||
this.defaultHost = 'localhost:3000'; // Default to localhost:3000 if not running in same origin
|
||||
}
|
||||
|
||||
/**
|
||||
* Load module dependencies
|
||||
* @returns {Promise} - Resolves when dependencies are loaded
|
||||
*/
|
||||
async loadDependencies() {
|
||||
try {
|
||||
// We depend on the text-buffer module
|
||||
this.reportProgress(30, "Waiting for text buffer");
|
||||
|
||||
// Dynamically load Socket.IO client if not already loaded
|
||||
if (!window.io) {
|
||||
this.reportProgress(40, "Loading Socket.IO client");
|
||||
await this.loadSocketIO();
|
||||
this.reportProgress(45, "Socket.IO client loaded");
|
||||
}
|
||||
|
||||
return true;
|
||||
} catch (error) {
|
||||
console.error("Error loading Socket Client dependencies:", error);
|
||||
return false;
|
||||
}
|
||||
this.defaultHost = 'localhost:3000';
|
||||
|
||||
// Bind methods using parent's bindMethods utility
|
||||
this.bindMethods([
|
||||
'connect',
|
||||
'disconnect',
|
||||
'send',
|
||||
'sendCommand',
|
||||
'requestStartGame',
|
||||
'requestSaveGame',
|
||||
'requestLoadGame',
|
||||
'on',
|
||||
'off',
|
||||
'emitEvent',
|
||||
'setupGameEventHandlers',
|
||||
'processTextFragment',
|
||||
'attemptReconnect',
|
||||
'getConnectionStatus',
|
||||
'loadSocketIO'
|
||||
]);
|
||||
}
|
||||
|
||||
/**
|
||||
@@ -47,54 +47,8 @@ class SocketClientModule extends BaseModule {
|
||||
* @returns {Promise<void>}
|
||||
*/
|
||||
loadSocketIO() {
|
||||
return new Promise((resolve, reject) => {
|
||||
// Check if Socket.IO is already loaded
|
||||
if (typeof window.io !== 'undefined') {
|
||||
resolve();
|
||||
return;
|
||||
}
|
||||
|
||||
// Load the Socket.IO client from the same server that served this page
|
||||
const script = document.createElement('script');
|
||||
script.src = '/socket.io/socket.io.js'; // Socket.IO automatically serves this
|
||||
script.async = true;
|
||||
|
||||
script.onload = () => {
|
||||
if (typeof window.io !== 'undefined') {
|
||||
resolve();
|
||||
} else {
|
||||
reject(new Error('Failed to load Socket.IO client'));
|
||||
}
|
||||
};
|
||||
|
||||
script.onerror = () => {
|
||||
reject(new Error('Failed to load Socket.IO client script'));
|
||||
};
|
||||
|
||||
document.head.appendChild(script);
|
||||
});
|
||||
}
|
||||
|
||||
/**
|
||||
* Wait for dependencies to be ready
|
||||
*/
|
||||
async waitForDependencies() {
|
||||
try {
|
||||
// Wait for the text buffer module to be available
|
||||
const textBufferReady = await moduleRegistry.waitForModule('text-buffer', 10000);
|
||||
|
||||
if (textBufferReady) {
|
||||
this.textBuffer = window.TextBuffer;
|
||||
this.reportProgress(60, "Text buffer module ready");
|
||||
return true;
|
||||
} else {
|
||||
console.warn("Text buffer module not ready, Socket Client will have limited functionality");
|
||||
return true; // Continue anyway for graceful degradation
|
||||
}
|
||||
} catch (error) {
|
||||
console.error("Error waiting for dependencies:", error);
|
||||
return false;
|
||||
}
|
||||
// Use parent's loadScript method
|
||||
return this.loadScript('/socket.io/socket.io.js');
|
||||
}
|
||||
|
||||
/**
|
||||
@@ -103,8 +57,25 @@ class SocketClientModule extends BaseModule {
|
||||
*/
|
||||
async initialize() {
|
||||
try {
|
||||
this.reportProgress(10, "Initializing Socket Client");
|
||||
|
||||
// Dynamically load Socket.IO client if not already loaded
|
||||
if (!window.io) {
|
||||
this.reportProgress(20, "Loading Socket.IO client");
|
||||
await this.loadSocketIO();
|
||||
this.reportProgress(30, "Socket.IO client loaded");
|
||||
}
|
||||
|
||||
// Get text buffer using parent's getModule method
|
||||
this.textBuffer = this.getModule('text-buffer');
|
||||
if (!this.textBuffer) {
|
||||
console.error("Socket Client: Failed to get text-buffer module");
|
||||
return false;
|
||||
}
|
||||
|
||||
this.reportProgress(50, "Setting up connection parameters");
|
||||
|
||||
// Use the current origin for the socket connection
|
||||
// This automatically handles the Docker port mapping situation
|
||||
const currentUrl = window.location.origin;
|
||||
|
||||
console.log(`Socket Client: Using origin for connection: ${currentUrl}`);
|
||||
|
||||
Reference in New Issue
Block a user