核心代碼:html
@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)
效果:vue
參考:web
http://www.javashuo.com/article/p-nwgtzoei-bp.htmlwebsocket
--------2019/07/25 補充vue採坑--------socket
vue端不能像原生js那麼寫:ui
原生js寫法:spa