Iam New to use falsk-ScocketIo to make chat,i have an issue with meet events and printing data and no customer event sent it seam to be an issue for server side I don’t Know ,
there are connection between socit and server as attached image
and if i need to print data in terminal or log no data loged or printed ,i think thre are an issue with Bluebrint or Name space as when i removed Bluprint and code dirictly it working Fine i serached more and more and i didnt find solution . here is my code
my socketioController.py :-
# Import necessary modules and libraries
from flask import render_template,url_for,flash,redirect,request,Blueprint,request,json,jsonify,session,Response,Flask
from flask_login import login_user, current_user, logout_user, login_required
from myproject import db
from myproject import app
from myproject.models import User ,Presentaions,PresntaionImages,PresntaionVideos,Settings,Colors,Hand,PresnterFrameMode,SlidingMode
from myproject.users.dataValidator import *
from myproject.handler import *
import datetime
from sqlalchemy import func
from sqlalchemy.sql import text
import cv2
from flask_socketio import SocketIO, join_room, leave_room, send,emit
from app import socketio
# Define a Flask Blueprint named 'streaming'
socketio_blueprint = Blueprint('socketio_blueprint', __name__)
#
@socketio_blueprint.route('/soket/show', methods=['GET','POST'])
@login_required
def show():
pagetitle = "Presenter X | Streaming Details"
return render_template('socit_template/show.html')
@socketio.on('connect')
def test_connect():
print('Client connected')
emit('custom-event', 'Connected to server!')
@socketio.on('message')
def handle_message(data):
print(f'Message received: {data}')
emit('custom-event', f'Server got your message: {data}')
my html code and Js :-
<!DOCTYPE html>
<html lang="en">
<head>
<meta charset="UTF-8">
<meta name="viewport" content="width=device-width, initial-scale=1.0">
<title>Flask-SocketIO Test</title>
<script src="https://cdn.socket.io/4.0.1/socket.io.min.js"></script>
</head>
<body>
<h1>Flask-SocketIO WebSocket Test</h1>
<script>
const socket = io.connect(`${location.protocol}//${document.domain}:${location.port}`);
socket.on('connect', () => {
console.log('WebSocket connected');
socket.send('Hello, Server!');
});
socket.on('custom-event', data => {
console.log(`Custom event received: ${data}`);
});
socket.on('message', data => {
console.log(`Message from server: ${data}`);
});
socket.on('connect_error', (error) => {
console.error(`Connection error: ${error}`);
});
</script>
</body>
</html>
here my app.py code :-
from myproject import app
from flask_socketio import SocketIO
#socketio = SocketIO(app)
socketio = SocketIO(app, cors_allowed_origins="*", async_mode=None) # Ensure proper initialization
if __name__ == '__main__':
#app.run(debug=True)
socketio.run(app,debug=True)
here my init.py code:
from flask import Flask
from flask_sqlalchemy import SQLAlchemy
from flask_migrate import Migrate
from flask_login import LoginManager
import pymysql
from myproject.config import *
from flask_mail import Mail
from flask_socketio import SocketIO
# Initialize the Flask app
app = Flask(__name__)
# Configure Mail
app.config["MAIL_SERVER"] = "smtp.googlemail.com"
app.config["MAIL_PORT"] = 587
app.config["MAIL_USE_TLS"] = True
app.config["MAIL_USERNAME"] = os.environ.get("EMAIL_USER")
app.config["MAIL_PASSWORD"] = os.environ.get("EMAIL_PASS")
mail = Mail(app)
# Initialize the login manager
login_manager = LoginManager()
login_manager.init_app(app)
login_manager.login_view = "users.login"
login_manager.login_message_category = "info"
login_manager.login_message = ''
# Initialize database and migrations
db = SQLAlchemy(app)
migrate = Migrate(app, db)
# Initialize SocketIO (ensure it's done after app is created)
socketio = SocketIO(app, cors_allowed_origins="*", async_mode=None)
# Import the blueprints AFTER initializing the app, db, and login manager
from myproject.core.coreController import core
from myproject.errors.errorhandlers import errors
from myproject.users.userController import users
from myproject.setting.settingController import setting
from myproject.color.colorController import colors
from myproject.slidingmode.slidingController import slidingmode
from myproject.hand.handController import hand
from myproject.presentaion.presentationController import presentation
from myproject.presnterframemode.presnterframemodeController import presnterframemode
from myproject.streaming.streamingController import streaming
from myproject.socketio.socketioController import socketio_blueprint
# Register blueprints
app.register_blueprint(core)
app.register_blueprint(errors)
app.register_blueprint(users)
app.register_blueprint(colors)
app.register_blueprint(setting)
app.register_blueprint(slidingmode)
app.register_blueprint(hand)
app.register_blueprint(presnterframemode)
app.register_blueprint(streaming)
app.register_blueprint(presentation)
app.register_blueprint(socketio_blueprint)
it seem to be no reply from server side , i dont Know hopping any one help me may be issue is name sapace .
thanks