Flasksocketi阻止我的应用程序

2024-06-14 02:35:04 发布

您现在位置:Python中文网/ 问答频道 /正文

我正在尝试创建一个简单的WebSocket连接。当我执行下一个代码时,我的服务器接收到字符串“hey!”客户收到字符串“哟!”,此时一切正常,但当我在其他视图中单击时,服务器冻结。在

代码:

客户端:

html格式:

<ul>
  <li><a class="active" href="#home">Home</a></li>
  <li><a href="editor-page2">News</a></li>
</ul>

JavaScript代码:

^{pr2}$

服务器端:

from flask import Flask, render_template
from flask.ext.socketio import SocketIO, emit

app = Flask('flaskwp1')
app.config['SECRET_KEY'] = 'secret!'
socketio = SocketIO(app)

@app.route('/editor-page1')
def webprint():
    return render_template('editor-page1.html')

@app.route('/editor-page2')
def webprint2():
    return render_template('editor-page2.html')

@socketio.on('my event', namespace='/test')
def handle_my_custom_event(json):
    print('received json: ' + str(json))
    emit('my response', {'data': 'yo!'})

Tags: 字符串代码服务器jsonappmydefhtml