zoukankan      html  css  js  c++  java
  • tornado硬件管理系统-websocket的c/s模式(4)

    创建视图连接池

    #-*- 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文件处理路径问题

    <!doctype html>
    <html lang="en">
    <head>
        <meta charset="utf-8">
        <meta name="viewport" content="width=device-width, initial-scale=1, shrink-to-fit=no">
        <meta name="description" content="">
        <meta name="author" content="">
        <title>硬件实时监控</title>
        <link href="{{static_url('dist/css/bootstrap.min.css')}}" rel="stylesheet">
        <link href="{{static_url('css/dashboard.css')}}" rel="stylesheet">
        <script src="{{static_url('js/analogClock.js')}}"></script>
    </head>
    
    <body>
    <nav class="navbar navbar-dark fixed-top bg-dark flex-md-nowrap p-0 shadow">
        <a class="navbar-brand col-sm-3 col-md-2 mr-0" href="#">硬件实时监控</a>
    </nav>
    
    <div class="container-fluid">
        <div class="row">
            <nav class="col-md-2 d-none d-md-block bg-light sidebar">
                <div class="sidebar-sticky">
                    <ul class="nav flex-column">
                        <li class="nav-item">
                            <a class="nav-link active" href="#">
                                系统监控
                            </a>
                        </li>
                        <li class="nav-item">
                            <a class="nav-link active" href="#">
                                日志统计
                            </a>
                        </li>
                    </ul>
                    <div class="col-md-12">
                        <div class="card text-white bg-dark mb-3">
                            <div class="card-header">当前时间</div>
                            <div class="card-body" id="clock"></div>
                        </div>
                    </div>
                </div>
            </nav>
    
            <main role="main" class="col-md-9 ml-sm-auto col-lg-10 px-4">
                <div class="d-flex justify-content-between flex-wrap flex-md-nowrap align-items-center pt-3 pb-2 mb-3 border-bottom">
                    <h5>系统监控</h5>
                </div>
                <!--内容部分-->
    
                <div class="jumbotron foot-section">
                    <hr class="my-4">
                    <div class="container">
                        <div class="row">
                            <div class="col-md-2">
                                <img data-src="{{static_url('images/face.png')}}"
                                     class="lazyload blur-up img-fluid mx-auto d-block">
                            </div>
                            <div class="col-md-4">
                                <h5>topass123</h5>
                                <p class="info-text">— 人生苦短,我用Python!</p>
                                <p class="info-text">微信:ooooooooooo</p>
                                <p class="info-text">手机:kkkkkkkkkkk</p>
                                <p class="info-text">邮箱:niubi@qq.com</p>
                                <p class="info-text">编程爱好者,擅长python、centos,hadoop集群搭建等技术。</p>
                            </div>
                            <div class="col-md-6">
                                <div class="row">
                                    <div class="col-md-4">
                                        <p class="text-center info-text">站长微信</p>
                                        <img data-src="{{static_url('images/weixin.jpg')}}"
                                             class="lazyload blur-up img-fluid mx-auto d-block">
                                    </div>
                                    <div class="col-md-4">
                                        <p class="text-center info-text">站长QQ</p>
                                        <img data-src="{{static_url('images/qq.jpg')}}"
                                             class="lazyload blur-up img-fluid mx-auto d-block">
                                    </div>
                                    <div class="col-md-4">
                                        <p class="text-center info-text">python问答QQ群</p>
                                        <img data-src="{{static_url('images/qq_group.jpg')}}"
                                             class="lazyload blur-up img-fluid mx-auto d-block">
                                    </div>
                                </div>
                            </div>
                        </div>
                    </div>
                    <hr class="my-4">
                    <div class="container">
                        <ul class="list-inline text-center">
                            <li class="info-text"><a href="http://www.miibeian.gov.cn/" target="_blank">滇ICP备17011280号</a>
                            </li>
                            <li class="info-text">© 2020 topass123[学习交流与分享]</li>
                        </ul>
                    </div>
                </div>
            </main>
        </div>
    </div>
    
    <script src="https://cdn.bootcss.com/lazysizes/4.0.2/lazysizes.min.js"></script>
    <!--导入sockjs-client-->
    <script src="https://cdn.bootcss.com/sockjs-client/1.3.0/sockjs.min.js"></script>
    <script src="{{static_url('js/monitor.js')}}"></script>
    <script>
        AnalogClock("clock", new AnalogClockOption(200, "#eee", "#333"));
    </script>
    </body>
    </html>
    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();
    }
  • 相关阅读:
    遮罩层可滚动
    toggle函数
    48.判断文本中回车的数量
    47.输出26个字母
    46.@弹出点击次数
    44.@鼠标点击页面中的任意标签,alert该标签的名称
    43.对象深度克隆
    UIscrollView 多图滑动 frame与bounds的区别比较
    累却真心充实 杂感
    delegate
  • 原文地址:https://www.cnblogs.com/topass123/p/13111593.html
Copyright © 2011-2022 走看看