Fix Socket.IO connection with enhanced error handling and remote server detection in launch script

This commit is contained in:
ComputerTech 2026-01-20 17:01:37 +00:00
parent 6fc538336a
commit bd87eb719d
2 changed files with 61 additions and 6 deletions

View File

@ -104,13 +104,42 @@ fi
# Check if Flask server is running
echo ""
echo "Checking Flask server..."
if curl -s http://localhost:5000/library.json > /dev/null 2>&1; then
echo "✅ Flask server is running on port 5000"
# Try to read server URL from settings
SERVER_URL="http://localhost:5000"
SETTINGS_FILE="$HOME/.techdj_settings.json"
if [ -f "$SETTINGS_FILE" ]; then
# Extract server_url from JSON if jq is available
if command -v jq &> /dev/null; then
SAVED_URL=$(jq -r '.server_url // "http://localhost:5000"' "$SETTINGS_FILE" 2>/dev/null)
if [ ! -z "$SAVED_URL" ] && [ "$SAVED_URL" != "null" ]; then
SERVER_URL="$SAVED_URL"
fi
else
# Fallback: simple grep if jq not available
SAVED_URL=$(grep -o '"server_url"[[:space:]]*:[[:space:]]*"[^"]*"' "$SETTINGS_FILE" 2>/dev/null | cut -d'"' -f4)
if [ ! -z "$SAVED_URL" ]; then
SERVER_URL="$SAVED_URL"
fi
fi
fi
echo "Checking server at: $SERVER_URL"
if curl -s --max-time 5 "${SERVER_URL}/library.json" > /dev/null 2>&1; then
echo "✅ Flask server is running at $SERVER_URL"
else
echo "⚠️ Flask server not detected on port 5000"
echo "Please start the server first:"
echo "⚠️ Flask server not detected at $SERVER_URL"
echo "Please make sure the server is running."
echo ""
echo "For local server:"
echo " python3 server.py"
echo ""
echo "For remote server:"
echo " Make sure server.py is running on the remote machine"
echo " and the URL in Settings (⚙️) is correct."
echo ""
read -p "Continue anyway? (y/n) " -n 1 -r
echo
if [[ ! $REPLY =~ ^[Yy]$ ]]; then

View File

@ -1836,13 +1836,39 @@ class TechDJMainWindow(QMainWindow):
# Start broadcast
try:
if self.socket is None:
self.socket = socketio.Client()
print(f"🔌 Connecting to server: {self.server_url}")
self.socket = socketio.Client(logger=True, engineio_logger=False)
# Add connection event handlers
@self.socket.on('connect')
def on_connect():
print("✅ Socket.IO connected successfully")
@self.socket.on('connect_error')
def on_connect_error(data):
print(f"❌ Socket.IO connection error: {data}")
QMessageBox.warning(self, "Connection Error",
f"Failed to connect to server at {self.server_url}\n\nError: {data}")
@self.socket.on('disconnect')
def on_disconnect():
print("⚠️ Socket.IO disconnected")
self.socket.on('listener_count', self.on_listener_count)
self.socket.connect(self.server_url)
try:
self.socket.connect(self.server_url, wait_timeout=10)
print("✅ Connection established")
except Exception as e:
print(f"❌ Connection failed: {e}")
QMessageBox.critical(self, "Connection Failed",
f"Could not connect to {self.server_url}\n\nError: {str(e)}\n\nMake sure the server is running.")
return
bitrate_map = {0: "128k", 1: "96k", 2: "64k", 3: "48k", 4: "32k"}
bitrate = bitrate_map.get(self.quality_combo.currentIndex(), "96k")
print(f"📡 Emitting start_broadcast with bitrate: {bitrate}")
self.socket.emit('start_broadcast', {
'bitrate': bitrate,
'format': 'mp3'