1 2 3 4 5 6 7 8 9 10 11 12 13 14 15 16 17 18 19 20 21 22 23 24 25 26 27 28 29 30 31 32 33 34 35 36 37 38
| from flask import Flask
from flask_cors import CORS
from flask_socketio import SocketIO from flask_socketio import send, emit
app = Flask(__name__)
CORS(app)
socketio = SocketIO(app, cors_allowed_origins='*')
@socketio.on('hello') def hello(str): print("接收到的数据为->", str) emit('respond', f'服务器:我收到了{str}')
@socketio.on('connect', namespace='/') def connected(): print('Client connect') emit('response', {'data': 'Connected'})
@socketio.on('disconnect', namespace='/') def disconnected(): print('Client disconnected')
if __name__ == '__main__': socketio.run(app)
|