努力做到日更
2019/02/20 13:33
建立websocket服务端
这一章 最重要
sockjs-tornado 实现
- 建立连接
- 发送消息,主动把消息推送给所有连接着服务端的客户端
- 关闭连接 (因开关机异常 关闭浏览器等因素造成异常 关闭连接)
urls.py 路由规则文件
# -*- coding: utf-8 -*-
from sockjs.tornado import SockJSRouter # 定义socket路由
from app.views.views_index import IndexHandler as index # 导入系统监控视图
from app.views.views_real_time import RealTimeHandler as real_time
# 配置路由视图映射规则
# [路由地址,视图]
urls = [
(r"/", index),
] + SockJSRouter(real_time, "/real/time").urls
建立websocket客户端
打开views_index.py
1.布局,渲染模板index.html
打开configs.py
2.配置:模板目录配置template_path、静态目录配置static_path
tornado的模板引擎:{{ static_url() }} 替换所有的static相对路径
建立websocket客户端,依赖sockjs-client
1.建立长连接
2.定义连接函数
3.定义断开连接函数
网友评论