I've been playing around with flask-socketio but can't get it to work.
My project directory is set up like this:
Project
├── app
│ └── main
│ │ └── __init__.py
│ │ └── events.py
│ │ └── portal
│ │ └── __init__.py
│ │ └── routes.py
│ └── templates
│ | └── index.html
│ └── __init__.py
└── run.py
└── __init__.py
My run.py
file:
from app import create_app, socketio
app = create_app()
if __name__ == "__main__":
socketio.run(app, debug=True)
app\__init__.py
file
from flask import Flask
from app.main.admin.routes import admin
from app.main.api.routes import api
from app.main.website.routes import website
from app.main.portal.routes import portal
import os
from flask_socketio import SocketIO
socketio = SocketIO()
def create_app():
app = Flask(__name__)
app.secret_key = os.urandom(24)
# Initialise extensions
socketio.init_app(app)
with app.app_context():
app.register_blueprint(website)
app.register_blueprint(portal, url_prefix="/portal")
return app
events.py
:
from .. import socketio
@socketio.on('hello')
def handle_hello(data):
print(data)
And finally alert.html
:
<script src="http://ajax.googleapis.com/ajax/libs/jquery/1.7.1/jquery.min.js" type="text/javascript"></script>
<script type="text/javascript" src="//code.jquery.com/jquery-1.4.2.min.js"></script>
<script type="text/javascript" src="//cdnjs.cloudflare.com/ajax/libs/socket.io/1.3.6/socket.io.min.js"></script>
<script>
var socket = io.connect('http://' + document.domain + ':' + location.port);
function hey() {
socket.emit('hello', {'data': 'hello peter r'});
}
</script>
</head>
<body>
<button onclick="hey()">Hello</button>
</body>
In events.py the event hello never seems to be received and nothing is printed. I'm assuming it's a problem with the way I imported the file but I don't see any problems in the code. Can someone please help?
Update: I've added a socket.on('connect')
event to my alert.html (client) code and the client is connecting but the server (events.py) wont receive any thing.
from Flask-socketio (server side) not receivng events
No comments:
Post a Comment