Slide 1

Slide 1 text

@cppgohan with Server-Sent Events

Slide 2

Slide 2 text

Flask?

Slide 3

Slide 3 text

Flask? MicroFramework again...

Slide 4

Slide 4 text

Flask? MicroFramework again... Written in Python

Slide 5

Slide 5 text

=

Slide 6

Slide 6 text

=

Slide 7

Slide 7 text

Werkzeug WSGI utility library =

Slide 8

Slide 8 text

+ Werkzeug WSGI utility library =

Slide 9

Slide 9 text

+ Werkzeug WSGI utility library =

Slide 10

Slide 10 text

+ Werkzeug WSGI utility library Jinja2 template engine =

Slide 11

Slide 11 text

Hello.py Demo

Slide 12

Slide 12 text

Hello.py Demo

Slide 13

Slide 13 text

Hello.py Demo

Slide 14

Slide 14 text

index.html Demo

Slide 15

Slide 15 text

index.html Demo result???

Slide 16

Slide 16 text

index.html Demo result??? • terminal run: python hello.py • open http://localhost:5000

Slide 17

Slide 17 text

Server-Sent Event server push的html5标准, 用法简易, 单次连接比polling高效些. 相比websocket的双向二进制数据, Server-Sent Event只是单向, HTTP协议. (但浏览器调试工具往往没法看到服务器发来的数据, X_X) Server对Client的请求, 做stream方式的响应.

Slide 18

Slide 18 text

Server-Sent Event

Slide 19

Slide 19 text

Server-Sent Event 支 持 率 不 高 , 暂 时 只 能 ”玩 ” 要 兼 容 处 理

Slide 20

Slide 20 text

Demo index.html

Slide 21

Slide 21 text

Demo hello.py

Slide 22

Slide 22 text

Demo redis:

Slide 23

Slide 23 text

Demo redis: demo:FILE_TEXT -- 每次python读出来的文本 demo:FILE_TEXT_SEQ -- 为文本版本号, 每次文本更新, 版本号+1 /sever_push响应 -- 当FILE_TEXT_SEQ增加时, 将最新的文本返回给浏览器

Slide 24

Slide 24 text

Demo result???

Slide 25

Slide 25 text

Demo result??? • terminal run: python hello.py

Slide 26

Slide 26 text

Demo result??? • terminal run: python hello.py • open http://localhost:5000

Slide 27

Slide 27 text

Demo result??? • terminal run: python hello.py • open http://localhost:5000 • block!!!

Slide 28

Slide 28 text

Demo result??? • terminal run: python hello.py • open http://localhost:5000 • block!!! solution???

Slide 29

Slide 29 text

Demo result??? • terminal run: python hello.py • open http://localhost:5000 • block!!! • async WSGI server! (eg. gunicorn + gevent, gevent) solution???

Slide 30

Slide 30 text

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???

Slide 31

Slide 31 text

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???

Slide 32

Slide 32 text

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???

Slide 33

Slide 33 text

More Demo Server下发代码示例

Slide 34

Slide 34 text

More Demo • start web server • open http://localhost:5000 • server push js script... • browser excute js script... • ... Server下发代码示例

Slide 35

Slide 35 text

More Usage •网站动态更新 •区分客户端推送 •微博墙实现 •webgame服务器推送 •浏览器插件

Slide 36

Slide 36 text

Resources • http://www.python.org/dev/peps/pep-0333/ • http://werkzeug.pocoo.org/ • http://jinja.pocoo.org/ • http://flask.pocoo.org/ • http://www.gevent.org/ • http://gunicorn.org/ • http://dev.w3.org/html5/eventsource/ • http://www.html5rocks.com/en/tutorials/eventsource/basics/ • http://www.w3schools.com/html/html5_serversentevents.asp

Slide 37

Slide 37 text

END; 2013, 分享不止!