另外,对于长连接和socket连接也有区分:
长连接:在页面里嵌入一个隐蔵iframe,将这个隐蔵iframe的src属性设为对一个长连接的请求,服务器端就能源源不断地往客户端输入数据。优点:消息即时到达,不发无用请求。缺点:服务器维护一个长连接会增加开销。实例:gmail聊天flash socket:在页面中内嵌入一个使用了socket类的 flash 程序javascript通过调用此flash程序提供的socket接口与服务器端的socket接口进行通信,javascript在收到服务器端传送的信息后控制页面的显示。优点:实现真正的即时通信,而不是伪即时。缺点:客户端必须安装flash插件;非http协议,无法自动穿越防火墙。实例:网络互动游戏。flask版
app.py
app.pyfrom flask import flask,render_template,request,jsonifyapp = flask(__name__)urs = { '1':{'name':'路人甲','count':1}, '2':{'name':'路人乙','count':0}, '3':{'name':'路人丙','count':0},}@app.route('/ur/list')def ur_list(): import time return render_template('ur_list.html',urs=urs)@app.route('/vote',methods=['post'])def vote(): uid = request.form.get('uid') urs[uid]['count'] += 1 return "投票成功"@app.route('/get/vote',methods=['get'])def get_vote(): return jsonify(urs)if __name__ == '__main__': # app.run(host='127.0.0.1',threaded=true) app.run(threaded=true)
template饭店情景英语s/ur_list.html
<!doctype html><html lang="zh-cn"><head> <meta chart="utf-8"> <title>title</title> <meta name="viewport" content="width=device-width, initial-scale=1"> <style> li{ cursor: pointer; } </style></head><body> <ul id="urlist"> {% for key,val in urs.items() %} <li uid="{{key}}">{{val.name}} ({{val.count}})</li> {% endfor %} </ul> <script src="https://cdn.bootcss.com/jquery/3.3.0/jquery.min.js"></script> <script> $(function () { $('#urlist').on('dblclick','li',function () { var uid = $(this).attr('uid'); $.ajax({ url:'/vote', type:'post', data:{uid:uid}, success:function (arg) { console.log(arg); } }) }); }); /* 获取投票信息 */ function get_vote() { $.ajax({ url:'/get/vote', type:"get", datatype:'json', success:function (arg) { $('#urlist').empty(); $.each(arg,function (k,v) { var li = document.createelement('li'); li.tattribute('uid',k); li.innertext = v.name + "(" + v.count + ')' ; $('#urlist').append(li); }) } }) } /* 定时任务 */ tinterval(get_vote,3000); </script></b淘宝网店推销ody></html>
长轮询示例代码
flask版
app.py
from flask import flask,render_template,request,jsonify,ss早发白帝城古诗ionimport uuidimport queueapp = flask(__name__)app.cret_key = 'asdfasdfasd'urs = { '1':{'name':'路人甲','count':1}, '2':{'name':'路人乙','count':0}, '3':{'name':'路人丙','count':0},}queque_dict = { # 'asdfasdfasdfasdf':queue()}@app.route('/ur/list')def ur_list(): ur_uuid = str(uuid.uuid4()) queque_dict[ur_uuid] = queue.queue() ssion['current_ur_uuid'] = ur_uuid return render_template('ur_list.html',urs=urs)@app.route('/vote',methods=['post'])def vote(): uid = request.form.get('uid') urs[uid]['count'] += 1 for q in queque_dict.values(): q.put(urs) return "投票成功"@app.route('/get/vote',methods=['get'])def get_vote(): ur_uuid = ssion['current_ur_uuid'] q = queque_dict[ur_uuid] ret = {'status':true,'data':none} try: urs = q.get(timeout=5) ret['data'] = urs except queue.empty: ret['status'] = fal return jsonify(ret)if __name__ == '__main__': app.run(host='127.0.0.1',threaded=true) # app.run(threaded=true)
\templates\ur_list.html
<!doctype html><html lang="zh-cn"><head> <meta chart="utf-8"> <title>title</title> <meta name="viewport" content="width=device-width, initial-scale=1"> <style> li{ cursor: pointer; } </style></hea张旭书法作品d><body> <ul id="urlist"> {% for key,val in urs.items() %} <li uid="{{key}}">{{val.name}} ({{val.count}})</li> {% endfor %} </ul> <script src="https://cdn.bootcss.com/jquery/3.3.0/jquery.min.js"></script> <script> $(function () { $('#urlist').on('click','li',function () { var uid = $(this).attr('uid'); $.ajax({ url:'/vote', type:'post', data:{uid:uid}, success:function (arg) { console.log(arg); } }) }); get_vote(); }); /* 获取投票信息 */ function get_vote() { $.ajax({ url:'/get/vote', type:"get", datatype:'json', success:function (arg) { if(arg.status){ $('#urlist').empty(); $.each(arg.data,function (k,v) { var li = document.createelement('li'); li.tattribute('uid',k); li.innertext = v.name + "(" + v.count + ')' ; $('#urlist').append(li); }) } get_vote(); } }) } </script></body></html>
本文发布于:2023-04-05 11:40:49,感谢您对本站的认可!
本文链接:https://www.wtabcd.cn/fanwen/zuowen/7eda0070224e45e6dfe497d6bf502fd8.html
版权声明:本站内容均来自互联网,仅供演示用,请勿用于商业和其他非法用途。如果侵犯了您的权益请与我们联系,我们将在24小时内删除。
本文word下载地址:ajax轮询请求现实(ajax并发请求阻塞讲解).doc
本文 PDF 下载地址:ajax轮询请求现实(ajax并发请求阻塞讲解).pdf
留言与评论(共有 0 条评论) |