Skip to content

Instantly share code, notes, and snippets.

@creamidea
Created July 30, 2013 16:26
Show Gist options
  • Select an option

  • Save creamidea/6114508 to your computer and use it in GitHub Desktop.

Select an option

Save creamidea/6114508 to your computer and use it in GitHub Desktop.
这个是实现长轮询的代码。客户端使用的是coffeescript,服务器是Python的Flask。
Post =
getId: ->
$.ajax {
type: 'POST'
url: '/getId'
context: $('body')
success: (data) ->
successHandle(data)
return
error: (xhr, type) ->
console.log xhr, type
return
}
return
# Success Handle
successHandle = (data) ->
console.log data.u_id
$('#show').append(data.u_id)
Post.getId()
return
# after loading run the below codes
$ ->
Post.getId()
return
from flask import Flask, session, redirect, url_for, escape, request, render_template, jsonify
import os
import random
import time
# app = Flask(__name__)
app = Flask(__name__,
# static_path='/static',
static_folder='static',
template_folder='templates')
@app.route('/')
def index():
if 'username' in session:
return render_template('logged.html',
number = random.random(),
username = escape(session['username']),)
return render_template('index.html')
@app.route('/login', methods=['GET', 'POST'])
def login():
"""
"""
if request.method == 'POST':
session['username'] = request.form['username']
return redirect(url_for('index'))
return render_template('login.html')
@app.route('/logout')
def logout():
# remove the username from the session if it's there
session.pop('username', None)
return redirect(url_for('index'))
# set the secret key. keep this really secret:
app.secret_key = 'A0Zr98j/3yX R~XHH!jmN]LWX/,?RT'
@app.route('/getId', methods=['GET', 'POST'])
def getId():
while True:
time.sleep(3)
msg = jsonify(u_id = '100')
return msg
# with app.app_context():
# url_for('static', filename='zepto.min.js')
if __name__ == '__main__':
app.debug = True
app.run()
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment