socket and server same port
This commit is contained in:
@@ -4,6 +4,7 @@ from flask_socketio import SocketIO
|
||||
import threading
|
||||
import json
|
||||
import time
|
||||
import os
|
||||
from loguru import logger
|
||||
import blinker
|
||||
|
||||
@@ -96,6 +97,19 @@ def init_socketio(app, datastore):
|
||||
@socketio.on('connect')
|
||||
def handle_connect():
|
||||
"""Handle client connection"""
|
||||
from changedetectionio.auth_decorator import login_optionally_required
|
||||
from flask import request
|
||||
from flask_login import current_user
|
||||
|
||||
# Access datastore from socketio
|
||||
datastore = socketio.datastore
|
||||
|
||||
# Check if authentication is required and user is not authenticated
|
||||
has_password_enabled = datastore.data['settings']['application'].get('password') or os.getenv("SALTED_PASS", False)
|
||||
if has_password_enabled and not current_user.is_authenticated:
|
||||
logger.warning("Socket.IO: Rejecting unauthenticated connection")
|
||||
return False # Reject the connection
|
||||
|
||||
logger.info("Socket.IO: Client connected")
|
||||
|
||||
@socketio.on('disconnect')
|
||||
|
||||
@@ -19,15 +19,18 @@ $(document).ready(function () {
|
||||
});
|
||||
|
||||
|
||||
// Try to create the socket connection to the SocketIO server - if it fails, the site will still work normally
|
||||
try {
|
||||
// Connect to Socket.IO on the same host/port, with path from template
|
||||
const socket = io({
|
||||
path: socketio_url, // This will be the path prefix like "/app/socket.io" from the template
|
||||
transports: ['polling', 'websocket'], // Try WebSocket but fall back to polling
|
||||
reconnectionDelay: 1000,
|
||||
reconnectionAttempts: 5
|
||||
});
|
||||
// Only try to connect if authentication isn't required or user is authenticated
|
||||
// The 'is_authenticated' variable will be set in the template
|
||||
if (typeof is_authenticated !== 'undefined' ? is_authenticated : true) {
|
||||
// Try to create the socket connection to the SocketIO server - if it fails, the site will still work normally
|
||||
try {
|
||||
// Connect to Socket.IO on the same host/port, with path from template
|
||||
const socket = io({
|
||||
path: socketio_url, // This will be the path prefix like "/app/socket.io" from the template
|
||||
transports: ['polling', 'websocket'], // Try WebSocket but fall back to polling
|
||||
reconnectionDelay: 1000,
|
||||
reconnectionAttempts: 5
|
||||
});
|
||||
|
||||
// Connection status logging
|
||||
socket.on('connect', function () {
|
||||
@@ -63,4 +66,5 @@ $(document).ready(function () {
|
||||
// If Socket.IO fails to initialize, just log it and continue
|
||||
console.log('Socket.IO initialization error:', e);
|
||||
}
|
||||
}
|
||||
});
|
||||
@@ -29,6 +29,7 @@
|
||||
<script>
|
||||
const csrftoken="{{ csrf_token() }}";
|
||||
const socketio_url="{{ get_socketio_path() }}/socket.io";
|
||||
const is_authenticated = {% if current_user.is_authenticated or not has_password %}true{% else %}false{% endif %};
|
||||
</script>
|
||||
<script src="{{url_for('static_content', group='js', filename='jquery-3.6.0.min.js')}}"></script>
|
||||
<script src="{{url_for('static_content', group='js', filename='csrf.js')}}" defer></script>
|
||||
|
||||
Reference in New Issue
Block a user