因为要上生产环境,所以配置还是专业一些比较好。

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+,\sdeploy\sprogress\s\d+/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,tornado,websocket,supervisord配置成型的更多相关文章

  1. Nginx 支持websocket的配置

    Nginx 支持websocket的配置server { listen 80; server_name 域名; location / { proxy_pass http://127.0.0.1:808 ...

  2. 部署项目Nginx+Tornado+Supervisor

    http://www.jianshu.com/p/9bebb99368ea Tornado Tornado 和现在的主流 Web 服务器框架(包括大多数 Python 的框架)有着明显的区别:它是非阻 ...

  3. Tornado WebSocket简单聊天

    Tornado实现了对socket的封装:tornado.web.RequestHandler 工程目录: 1.主程序 manage.py import tornado.web import torn ...

  4. centos7 使用nginx + tornado + supervisor搭建服务

    如何在Linux下部署一个简单的基于Nginx+Tornado+Supervisor的Python web服务. Tornado:官方介绍,是使用Python编写出来的一个极轻量级.高可伸缩性和非阻塞 ...

  5. Nginx反向代理的配置

    Chapter: Nginx基本操作释疑 1. Nginx的端口修改问题 2. Nginx 301重定向的配置 3. Windows下配置Nginx使之支持PHP 4. Linux下配置Nginx使之 ...

  6. nginx上用fastcgi配置python环境

    费了2天的功夫,翻阅了无数的中文.英文资料,终于搞定.写下此文留待以后翻阅用      本文环境,centOS 5.4 ,Nignx-0.8.49, Python 2.6.5   ========== ...

  7. 基于nginx的WebSocket反向代理

    系统:windows 版本:nginx 1.4.5 配置内容: location ~ \.do$ {             proxy_pass   http://192.168.20.102:80 ...

  8. Nginx支持WebSocket反向代理-学习小结

    WebSocket是目前比较成熟的技术了,WebSocket协议为创建客户端和服务器端需要实时双向通讯的webapp提供了一个选择.其为HTML5的一部分,WebSocket相较于原来开发这类app的 ...

  9. 解决nginx转发websocket报400错误

    解决nginx转发websocket报400错误 说明 由于个人服务器上面有多个项目,配置了二级域名,需要对二级域名进行转发,在转发工作这快采取了大名鼎鼎的nginx.在这之前所有的项目运行转发都没问 ...

随机推荐

  1. Install-Package : “XXXX”已拥有为“XXXX”定义的依赖项。

    Install-Package : “AutoMapper”已拥有为“NETStandard.Library”定义的依赖项.所在位置 行:1 字符: 16+ Install-Package <& ...

  2. Sql2008事务日志已满处理

    处理方式: USE [master] GO ALTER DATABASE gzl SET RECOVERY SIMPLE WITH NO_WAIT GO ALTER DATABASE gzl SET ...

  3. javascript异步下载 Promise实现

    一般下载都是直接打开一个链接就行.var URL = 'XXXX';window.open(URL)其实这样会有些问题:1. 浏览器禁止打开新窗口,导致无法下载 那么怎么解决呢?这样: <a h ...

  4. 前端面试题HTML

    浏览器页面有哪三层构成,分别是什么,作用是什么?

  5. js类型识别

    typeof总结: 可以识别标准类型(Null除外) 不能识别具体的对象类型(Function除外) Object.prototype.toString总结: 可以识别标准类型和内置对象类型 不能识别 ...

  6. node遍历给定目录下特定文件,内容合并到一个文件

    遍历目录用了fs.readdir这个异步方法,得到当前目录下所有的文件和目录的一个数组.然后判断: if文件,并且后缀符合设定的规则(本文例子是符合后缀ts,js)直接用同步方法写入, if目录,继续 ...

  7. 模板TemplateRef

    TemplateRef<void> <ng-template #模板名称></ng-template>

  8. java虚拟机(八)--java性能监控与故障处理工具

    问题定位: 除了个人经验,知识,工具也是很重要的,通过数据进行问题分析,包括:运行日志.异常堆栈.GC日志.线程快照(threaddump/javacore文件 ).堆转储快照(heapdump/hp ...

  9. JavaScipt30(第二个案例)

    承接上篇https://www.cnblogs.com/wangxi01/p/10641115.html,这是第二个案例 附上项目链接: https://github.com/wesbos/JavaS ...

  10. idea_复制包名类名