圆月山庄资源网 Design By www.vgjia.com
安装Flask-Sockets很容易:
pip install flask-socketio
注意Flask-SocketIO依赖gevent库,目前它仅可以在python2上运行(译者注:python3.6测试也可以)。gevent很快也会对python3支持。
下面是一个Flask-SocketIO在Flask应用上的实现例子:
from flask import Flask, request, jsonify,render_template from flask_socketio import SocketIO, emit app = Flask(__name__) app.config['SECRET_KEY'] = 'secret!' socketio = SocketIO(app) @app.route('/') def index(): return render_template('test.html') #触发事件my event:回复只发送此连接 @socketio.on('my_event', namespace='/chat') def test_message(message): print(message) emit('my_response', {'data': message['data']}) #触发事件my broadcast event::回复所有链接(广播) @socketio.on('my_broadcast event', namespace='/chat') def test_message(message): print(message) emit('my_response', {'data': message['data']}, broadcast=True) ################################################################## #自动连接和自动断开触发 @socketio.on('connect', namespace='/chat') def test_connect(): print("连接到来") emit('my_response', {'data': 'Connected'}) @socketio.on('disconnect', namespace='/chat') def test_disconnect(): print("连接断开") print('Client disconnected') if __name__ == '__main__': socketio.run(app,host='0.0.0.0',port=8200,debug=True)
对应的test.html,注意一个问题:flask是从与启动文件同级的templates查找模板的。
test.html
官方完整示例
#!/usr/bin/env python from threading import Lock from flask import Flask, render_template, session, request, copy_current_request_context from flask_socketio import SocketIO, emit, join_room, leave_room, close_room, rooms, disconnect # Set this variable to "threading", "eventlet" or "gevent" to test the # different async modes, or leave it set to None for the application to choose # the best option based on installed packages. async_mode = None app = Flask(__name__) app.config['SECRET_KEY'] = 'secret!' socketio = SocketIO(app, async_mode=async_mode) thread = None thread_lock = Lock() def background_thread(): """Example of how to send server generated events to clients.""" count = 0 while True: socketio.sleep(100) count += 1 socketio.emit('my_response', {'data': 'Server generated event', 'count': count}, namespace='/test') @app.route('/') def index(): return render_template('test1.html', async_mode=socketio.async_mode) @socketio.on('my_event', namespace='/test') def mtest_message(message): print(message) session['receive_count'] = session.get('receive_count', 0) + 1 # print(message) # print(message['data']) emit('my_response', {'data': message['data'], 'count': session['receive_count']}) @socketio.on('my_broadcast_event', namespace='/test') def mtest_broadcast_message(message): print(message) session['receive_count'] = session.get('receive_count', 0) + 1 emit('my_response', {'data': message['data'], 'count': session['receive_count']}, broadcast=True) @socketio.on('join', namespace='/test') def join(message): print(message) join_room(message['room']) session['receive_count'] = session.get('receive_count', 0) + 1 emit('my_response', {'data': 'In rooms: ' + ', '.join(rooms()), 'count': session['receive_count']}) @socketio.on('leave', namespace='/test') def leave(message): print(message) leave_room(message['room']) session['receive_count'] = session.get('receive_count', 0) + 1 emit('my_response', {'data': 'In rooms: ' + ', '.join(rooms()), 'count': session['receive_count']}) @socketio.on('close_room', namespace='/test') def close(message): session['receive_count'] = session.get('receive_count', 0) + 1 emit('my_response', {'data': 'Room ' + message['room'] + ' is closing.', 'count': session['receive_count']}, room=message['room']) close_room(message['room']) @socketio.on('my_room_event', namespace='/test') def send_room_message(message): session['receive_count'] = session.get('receive_count', 0) + 1 emit('my_response', {'data': message['data'], 'count': session['receive_count']}, room=message['room']) ################################################################## #自动连接和自动断开触发 @socketio.on('connect', namespace='/test') def test_connect(): print("连接到来") emit('my response', {'data': 'Connected'}) @socketio.on('disconnect', namespace='/test') def test_disconnect(): print("连接断开") print('Client disconnected') if __name__ == '__main__': socketio.run(app, debug=True,port= 8200)
test1.html
<!DOCTYPE HTML> <html> <head> <title>Flask-SocketIO Test</title> <script src="/UploadFiles/2021-04-08/jquery.min.js">以上就是本文的全部内容,希望对大家的学习有所帮助,也希望大家多多支持。
圆月山庄资源网 Design By www.vgjia.com
广告合作:本站广告合作请联系QQ:858582 申请时备注:广告合作(否则不回)
免责声明:本站文章均来自网站采集或用户投稿,网站不提供任何软件下载或自行开发的软件! 如有用户或公司发现本站内容信息存在侵权行为,请邮件告知! 858582#qq.com
免责声明:本站文章均来自网站采集或用户投稿,网站不提供任何软件下载或自行开发的软件! 如有用户或公司发现本站内容信息存在侵权行为,请邮件告知! 858582#qq.com
圆月山庄资源网 Design By www.vgjia.com
暂无评论...
更新日志
2024年11月01日
2024年11月01日
- 《暗喻幻想》顺风耳作用介绍
- 崔健1985-梦中的倾诉[再版][WAV+CUE]
- 黄子馨《追星Xin的恋人们2》HQ头版限量编号[WAV+CUE]
- 孟庭苇《情人的眼泪》开盘母带[低速原抓WAV+CUE]
- 孙露《谁为我停留HQCD》[低速原抓WAV+CUE][1.1G]
- 孙悦《时光音乐会》纯银CD[低速原抓WAV+CUE][1.1G]
- 任然《渐晚》[FLAC/分轨][72.32MB]
- 英雄联盟新英雄安蓓萨上线了吗 新英雄安蓓萨技能介绍
- 魔兽世界奥杜尔竞速赛什么时候开启 奥杜尔竞速赛开启时间介绍
- 无畏契约CGRS准星代码多少 CGRS准星代码分享一览
- 张靓颖.2012-倾听【少城时代】【WAV+CUE】
- 游鸿明.1999-五月的雪【大宇国际】【WAV+CUE】
- 曹方.2005-遇见我【钛友文化】【WAV+CUE】
- Unity6引擎上线:稳定性提升、CPU性能最高提升4倍
- 人皇Sky今日举行婚礼!电竞传奇步入新篇章