nginx,tornado,websocket,supervisord配置成型
因為要上生產環境,所以配置還是專業一些比較好。
nginx.conf
upstream websocket_host { server 127.0.0.1:9527; } location /ws_log { proxy_redirect off; proxy_set_header X-Real-IP $remote_addr; proxy_set_header X-Forwarded-For $proxy_add_x_forwarded_for; proxy_set_header Host $host; proxy_pass http://websocket_host; } location /websocket { proxy_redirect off; proxy_set_header X-Real-IP $remote_addr; proxy_set_header X-Forwarded-For $proxy_add_x_forwarded_for; proxy_set_header Host $host; proxy_pass http://websocket_host; proxy_http_version 1.1; proxy_set_header Upgrade $http_upgrade; proxy_set_header Connection "upgrade"; }
supervisord.conf
[program:tornado-9527] command=python /X/X/websocket/ws.py stdout_logfile=/var/log/tornado.log stderr_logfile=/var/log/tornado.log autostart=true autorestart=true startsecs=10 stopwaitsecs=600
wsapp.py
import tornado.ioloop import tornado.web import tornado.websocket from tornado.ioloop import IOLoop from datetime import timedelta import time import os import sys import tornado.httpserver class WebSocketHandler(tornado.websocket.WebSocketHandler): file_content = "" filename = "test.log" def check_origin(self, origin): return True def open(self): pass def update_client(self): self.write_message(self._read_file(self.filename)) def on_message(self, message): print message self.filename = message.split(`-`)[1] self.update_client() def on_close(self): pass def _read_file(self, filename): print filename, `=================` with open("/tmp/"+filename) as f: content = f.read() content_diff = content.replace(self.file_content, ``) self.file_content = content return content_diff class IndexPageHandler(tornado.web.RequestHandler): def get(self): self.render("websockets.html") class Application(tornado.web.Application): def __init__(self): handlers = [ (r`/ws_log/`, IndexPageHandler), (r`/websocket/ws`, WebSocketHandler) ] settings = dict( template_path=os.path.join(os.path.dirname(__file__), "templates"), static_path=os.path.join(os.path.dirname(__file__), "static"), debug=True ) tornado.web.Application.__init__(self, handlers, **settings) if __name__ == `__main__`: ws_app = Application() server = tornado.httpserver.HTTPServer(ws_app) server.listen(9527) tornado.ioloop.IOLoop.instance().start()
websocket.html
<link rel="stylesheet" href="/static/uikit-2.22.0/css/uikit.min.css" /> <link rel="stylesheet" href="/static/uikit-2.22.0/css/components/progress.gradient.css"/> <link rel="stylesheet" href="/static/css/radialindicator.css"/> <script src="/static/js/jquery-1.11.2.js"></script> <script src="/static/js/radialIndicator.min.js"></script> <script src="/static/uikit-2.22.0/js/uikit.js"></script> <script src="/static/uikit-2.22.0/js/components/sticky.min.js"></script> html <div id="procPer"></div> <br> <textarea id="logoutput" placeholder="日誌輸出" style="background:#000; color:#FFF; width:530;height:400" >...</textarea> <br> <a href="javascript:void(0);" onClick="wsFunc(`stop`, `DEMO`)"><span id="actionSpn" class="uk-button">停止重新整理</span></a> <script> function GetRequest() { var url = location.search; var theRequest = new Object(); if (url.indexOf("?") != -1) { var str = url.substr(1); strs = str.split("&"); for(var i = 0; i < strs.length; i ++) { theRequest[strs[i].split("=")[0]]=unescape(strs[i].split("=")[1]); } } return theRequest; } var Request = new Object(); Request = GetRequest(); console.log(Request); var dev_version = Request[`dev_version`]; console.log(dev_version); wsFunc("refresh", dev_version); function wsFunc(msg, dev_version) { var messageContainer = document.getElementById("logoutput"); var percentContainer = document.getElementById("procPer"); if ("WebSocket" in window) { var ws = new WebSocket("ws://1.2.3.4/websocket/ws"); ws.onopen = function() {}; ws.onmessage = function (evt) { messageContainer.innerHTML += evt.data; var d = messageContainer.scrollHeight; messageContainer.scrollTop = d; var pattern = /progress/gi; var ans = messageContainer.innerHTML.match(/w+.w+,sdeploysprogresssd+/g); var dic = new Array(); for (item in ans){ dic_key = ans[item].split(`,`)[0]; dic_value = ans[item].split(`,`)[1].split(" ")[3]; dic[dic_key] = dic_value; } for (key in dic) { if(document.getElementById(key + "label") == undefined){ var span=document.createElement(`span`); span.setAttribute("id", key + "label"); percentContainer.appendChild(span); document.getElementById(key + "label").innerHTML = key } if(document.getElementById(key) == undefined){ var span=document.createElement(`span`); span.setAttribute("id", key); percentContainer.appendChild(span); //document.getElementById(key).innerHTML =dic[key]; $(span).radialIndicator({ barColor: { 10: `#0000FF`, 70: `#0000FF`, 90: `#0000FF`, 100: `#33CC33` }, radius: 25, barWidth: 3, initValue: dic[key], roundCorner : true, percentage: true }); radialObj =$(span).data(`radialIndicator`); } radialObj.animate(dic[key]); console.log(key, dic[key]); } }; ws.onclose = function() {}; console.log(msg); if (msg == "refresh") { sh=setInterval(function(){ ws.send(msg+"-"+dev_version, function(){})} ,2000); } else { clearInterval(sh); document.getElementById("actionSpn").innerText = `OutPut`; } } else { messageContainer.innerHTML += "此瀏覽器不支援websocket!"; } } </script>
相關文章
- Nginx 支援websocket的配置NginxWeb
- nginx配置支援websocket協議wssNginxWeb協議
- 如何使用 Nginx + Supervisord 部署 Hyperf 專案Nginx
- Nginx 伺服器配置支援SignalR (WebSocket)Nginx伺服器SignalRWeb
- nginx代理websocketNginxWeb
- nginx 代理websocketNginxWeb
- Nginx 反向代理 websocketNginxWeb
- Let’s Encrypt + Nginx + Tornado 開啟 HTTPSNginxHTTP
- 【Nginx】面試官問我Nginx如何配置WebSocket?我給他現場演示了一番!!Nginx面試Web
- supervisord 程式管家
- CentOS7 supervisord 程式管理器一些配置CentOS
- SpringBoot配置WebSocketSpring BootWeb
- Spring中配置WebSocketSpringWeb
- 配置nginxNginx
- Nginx 配置Nginx
- nginx配置Nginx
- Nginx之(三)Nginx配置Nginx
- 採用nginx反向代理讓websocket 支援 wssNginxWeb
- Nginx支援WebSocket反向代理-學習小結NginxWeb
- nginx之 nginx限流配置Nginx
- Nginx篇--解讀nginx配置Nginx
- Nginx代理websocket為什麼要這樣做?NginxWeb
- 挖掘性質初步成型
- Docker配置nginxDockerNginx
- nginx常用配置Nginx
- nginx代理配置Nginx
- Nginx Location 配置Nginx
- NGINX基本配置Nginx
- Nginx 配置域名Nginx
- Nginx部署配置Nginx
- Nginx - 最小配置Nginx
- nginx location配置Nginx
- nginx 配置使用Nginx
- nginx的配置Nginx
- thinkphp nginx 配置PHPNginx
- nginx配置404Nginx
- 配置nginx支援Nginx
- 【Nginx】Referer配置Nginx