django websocket 实现后台日志在web端展示(+前端vue设置)

核心代码:

@accept_websocket
def get_log(req):
    if req.is_websocket():
        print('收到websocket请求')
        with open(settings.LOG_FILE, 'r', encoding='UTF-8') as f:
            log_length = len(f.readlines())
            time.sleep(1)
        while True:
            with open(settings.LOG_FILE, 'r', encoding='UTF-8') as f:
                contents = f.readlines()
                length_tmp = len(contents)
            for i in range(log_length, length_tmp):
                req.websocket.send(contents[i].encode('utf-8'))
            log_length = length_tmp
            time.sleep(1)

效果:

django websocket 实现后台日志在web端展示(+前端vue设置)_第1张图片

 

参考:

https://www.cnblogs.com/sui776265233/p/10176275.html#_labelTop

--------2019/07/25 补充vue采坑--------

vue端不能像原生js那么写:

django websocket 实现后台日志在web端展示(+前端vue设置)_第2张图片

原生js写法:

django websocket 实现后台日志在web端展示(+前端vue设置)_第3张图片

 

转载于:https://www.cnblogs.com/dannyyao/p/11240563.html

你可能感兴趣的:(django websocket 实现后台日志在web端展示(+前端vue设置))