37
@cppgohan with Server-Sent Events

Flask With Server-Sent Event

  • Upload
    tencent

  • View
    2.359

  • Download
    7

Embed Size (px)

DESCRIPTION

Flask With Server-Sent Event by cppgohan sztechparty 20130317

Citation preview

Page 1: Flask With Server-Sent Event

@cppgohan

with

Server-Sent Events

Page 2: Flask With Server-Sent Event

Flask?

Page 3: Flask With Server-Sent Event

Flask?MicroFramework again...

Page 4: Flask With Server-Sent Event

Flask?MicroFramework again...Written in Python

Page 5: Flask With Server-Sent Event

=

Page 6: Flask With Server-Sent Event

=

Page 7: Flask With Server-Sent Event

Werkzeug

WSGI utility library

=

Page 8: Flask With Server-Sent Event

+Werkzeug

WSGI utility library

=

Page 9: Flask With Server-Sent Event

+Werkzeug

WSGI utility library

=

Page 10: Flask With Server-Sent Event

+Werkzeug

WSGI utility library

Jinja2

template engine

=

Page 11: Flask With Server-Sent Event

Hello.py

Demo

Page 12: Flask With Server-Sent Event

Hello.py

Demo

Page 13: Flask With Server-Sent Event

Hello.py

Demo

Page 14: Flask With Server-Sent Event

index.html

Demo

Page 15: Flask With Server-Sent Event

index.html

Demo

result???

Page 16: Flask With Server-Sent Event

index.html

Demo

result???

• terminal run: python hello.py

• open http://localhost:5000

Page 17: Flask With Server-Sent Event

Server-Sent Event

server push的html5标准, 用法简易, 单次连接比polling高效些.

相比websocket的双向二进制数据, Server-Sent Event只是单向, HTTP协议. (但浏览器调试工具往往没法看到服务器发来的数据, X_X)

Server对Client的请求, 做stream方式的响应.

Page 18: Flask With Server-Sent Event

Server-Sent Event

Page 19: Flask With Server-Sent Event

Server-Sent Event

支持率不高, 暂时只能”玩”

要兼容处理

Page 20: Flask With Server-Sent Event

Demo

index.html

Page 21: Flask With Server-Sent Event

Demohello.py

Page 22: Flask With Server-Sent Event

Demoredis:

Page 23: Flask With Server-Sent Event

Demoredis:

demo:FILE_TEXT -- 每次python读出来的文本

demo:FILE_TEXT_SEQ -- 为文本版本号, 每次文本更新, 版本号+1

/sever_push响应 -- 当FILE_TEXT_SEQ增加时, 将最新的文本返回给浏览器

Page 24: Flask With Server-Sent Event

Demo

result???

Page 25: Flask With Server-Sent Event

Demo

result???

• terminal run: python hello.py

Page 26: Flask With Server-Sent Event

Demo

result???

• terminal run: python hello.py

• open http://localhost:5000

Page 27: Flask With Server-Sent Event

Demo

result???

• terminal run: python hello.py

• open http://localhost:5000

• block!!!

Page 28: Flask With Server-Sent Event

Demo

result???

• terminal run: python hello.py

• open http://localhost:5000

• block!!!

solution???

Page 29: Flask With Server-Sent Event

Demo

result???

• terminal run: python hello.py

• open http://localhost:5000

• block!!!

• async WSGI server! (eg. gunicorn + gevent, gevent)

solution???

Page 30: Flask With Server-Sent Event

Demo

result???

• terminal run: python hello.py

• open http://localhost:5000

• block!!!

• async WSGI server! (eg. gunicorn + gevent, gevent)

• terminal run: gunicorn -k gevent -b '0.0.0.0:5000' hello:app

solution???

Page 31: Flask With Server-Sent Event

Demo

result???

• terminal run: python hello.py

• open http://localhost:5000

• block!!!

• async WSGI server! (eg. gunicorn + gevent, gevent)

• terminal run: gunicorn -k gevent -b '0.0.0.0:5000' hello:app

• open http://localhost:5000

solution???

Page 32: Flask With Server-Sent Event

Demo

result???

• terminal run: python hello.py

• open http://localhost:5000

• block!!!

• async WSGI server! (eg. gunicorn + gevent, gevent)

• terminal run: gunicorn -k gevent -b '0.0.0.0:5000' hello:app

• open http://localhost:5000

• open http://localhost:5000 again!

solution???

Page 33: Flask With Server-Sent Event

More Demo

Server下发代码示例

Page 34: Flask With Server-Sent Event

More Demo

• start web server

• open http://localhost:5000

• server push js script...

• browser excute js script...

• ...

Server下发代码示例

Page 35: Flask With Server-Sent Event

More Usage

•网站动态更新

•区分客户端推送

•微博墙实现

•webgame服务器推送

•浏览器插件

Page 37: Flask With Server-Sent Event

END; 2013, 分享不止!