【发布时间】:2015-08-29 04:13:52
【问题描述】:
我正在按照本教程尝试在我的应用中使用 Flask-WebSockets。
http://blog.miguelgrinberg.com/post/easy-websockets-with-flask-and-gevent
我的问题是我不知道如何正确连接到服务器。
当我调用我的烧瓶应用程序时,它们的格式为:
http://localhost:80/myapp/<route_goes_here>
我的应用结构如下:
couponmonk/venv/couponmonk
__init__.py
views.py
templates/
index.html
__init__.py
from flask import Flask
from flask.ext.sqlalchemy import SQLAlchemy
from sqlalchemy.orm import sessionmaker
from sqlalchemy import *
from flask.ext.socketio import SocketIO, emit
app = Flask(__name__)
socketio = SocketIO(app)
engine = create_engine('mysql://root:my_password@localhost/my_db_name')
DBSession = sessionmaker(bind=engine)
import couponmonk.views
views.py
from couponmonk import app, DBSession, socketio
@socketio.on('my event', namespace='/test')
def test_message(message):
emit('my response', {'data': message['data']})
@app.route('/', methods = ['GET'])
def index():
return render_template('index.html')
根据我的设置,我应该把这行放在哪里:
if __name__ == '__main__':
socketio.run(app)
我也不确定在index.html 中为以下行添加什么地址:
index.html
此文件中的代码与此相同(https://github.com/miguelgrinberg/Flask-SocketIO/blob/master/example/templates/index.html)
var socket = io.connect('what to put here?');
我试过http://localhost:80/test 并没有得到任何回应。我不确定namespace 是否应该是地址的一部分。
感谢您的帮助。
【问题讨论】:
标签: python flask xampp flask-sockets