tornado硬件管理系统-websocket的c/s模式(4)

2021-02-05 19:17

阅读:1007

YPE html>

标签:host   temp   doctype   协议   映射   ide   function   tps   get   

技术图片

 

 

创建视图连接池

#-*- coding: utf-8 -*-
#app/views/views_real_time.py
from sockjs.tornado import SockJSConnection class RealTimeHandler(SockJSConnection): #建立连接池 waiters = set() def on_open(self, request): try: self.waiters.add(self) except Exception as e: print(e) #发送消息 def on_message(self, message): try: # self.broadcast(self.waiters,message) except Exception as e: print(e) #关闭连接 def on_close(self): try: self.waiters.remove(self) except Exception as e: print(e)

做路由映射:

#-*- coding: utf-8 -*-
from sockjs.tornado import SockJSRouter
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

导入index.html文件处理路径问题

技术图片技术图片
"en">"utf-8">
    "viewport" content="width=device-width, initial-scale=1, shrink-to-fit=no">
    "description" content="">
    "author" content="">
    硬件实时监控"{{static_url(‘dist/css/bootstrap.min.css‘)}}" rel="stylesheet">
    "{{static_url(‘css/dashboard.css‘)}}" rel="stylesheet">
    
class="container-fluid">
class="row">
"main" class="col-md-9 ml-sm-auto col-lg-10 px-4">
class="d-flex justify-content-between flex-wrap flex-md-nowrap align-items-center pt-3 pb-2 mb-3 border-bottom">
系统监控
class="jumbotron foot-section">
class="my-4">
class="container">
class="row">
class="col-md-2"> "{{static_url(‘images/face.png‘)}}" class="lazyload blur-up img-fluid mx-auto d-block">
class="col-md-4">
topass123

class="info-text">— 人生苦短,我用Python!

class="info-text">微信:ooooooooooo

class="info-text">手机:kkkkkkkkkkk

class="info-text">邮箱:niubi@qq.com

class="info-text">编程爱好者,擅长python、centos,hadoop集群搭建等技术。

class="col-md-6">
class="row">
class="col-md-4">

class="text-center info-text">站长微信

"{{static_url(‘images/weixin.jpg‘)}}" class="lazyload blur-up img-fluid mx-auto d-block">
class="col-md-4">

class="text-center info-text">站长QQ

"{{static_url(‘images/qq.jpg‘)}}" class="lazyload blur-up img-fluid mx-auto d-block">
class="col-md-4">

class="text-center info-text">python问答QQ群

"{{static_url(‘images/qq_group.jpg‘)}}" class="lazyload blur-up img-fluid mx-auto d-block">

class="my-4">
class="container">
View Code

处理系统查找,使用路径拼接的方式

#-*- coding: utf-8 -*-
#app/configs.py
import os root_path = os.path.dirname(__file__) #配置调试模式 configs = dict( debug = True, template_path = os.path.join(root_path,"templates"), static_path = os.path.join(root_path,"static"), )

渲染网页,即可看到完美的页面

#-*- conding: utf-8 -*-
import tornado.web

#定义一个首页视图
class IndexHandler(tornado.web.RequestHandler):
    def get(self,*args,**kwargs):
        self.render("index.html")

最后使用websocketjs实现长连接。

//定义长连接
var conn = null;

//定义连接的函数
function  connect() {
    //优先将以前的所有的连接关闭掉。
    disconnect();
    //定义协议
    var transports = ["websocket"];
    //创建连接的对象
    conn = new SockJS("http://"+window.location.host+"/real/time",transports);
    //创建连接
    conn.onopen=function () {
        console.log("is success");
    };
    //连接发送消息
    conn.onmessage=function (e) {
        console.log(e.data)
    };
    //连接关闭
    conn.onclose=function () {
        console.log("is colse")
    };
    //每个几秒触发一个事件,解决连接是否完成的
    setInterval(function () {
        conn.send("system");

    },1000);
}

//定义断开连接函数
function disconnect() {
    if (conn!=null){
        conn.close();
        conn=null;
    }
}

//刷新页面的时候,处理断开与重连的函数,重连判断
if(conn==null){
    connect()
}else{
    disconnect();
}

 

tornado硬件管理系统-websocket的c/s模式(4)

标签:host   temp   doctype   协议   映射   ide   function   tps   get   

原文地址:https://www.cnblogs.com/topass123/p/13111593.html


评论


亲,登录后才可以留言!