pull/26/head
ibuler@qq.com 2015-11-02 11:18:06 +08:00
parent 73f94fecb5
commit ebe8ce7109
2 changed files with 171 additions and 0 deletions

67
run_log_watch.py Normal file
View File

@ -0,0 +1,67 @@
# coding: utf-8
import time
import json
import os.path
import tornado.ioloop
import tornado.options
import tornado.web
import tornado.websocket
import tornado.httpserver
from tornado.options import define, options
define("port", default=8080, help="run on the given port", type=int)
class Application(tornado.web.Application):
def __init__(self):
handlers = [
(r'/', MainHandler),
(r'/send', SendHandler),
]
setting = {
'cookie_secret': 'DFksdfsasdfkasdfFKwlwfsdfsa1204mx',
'template_path': os.path.join(os.path.dirname(__file__), 'templates'),
'static_path': os.path.join(os.path.dirname(__file__), 'static'),
}
tornado.web.Application.__init__(self, handlers, **setting)
class SendHandler(tornado.websocket.WebSocketHandler):
clients = set()
def open(self):
SendHandler.clients.add(self)
self.write_message(json.dumps({'input': 'connected...'}))
self.stream.set_nodelay(True)
def on_message(self, message):
message = json.loads(message)
self.write_message(json.dumps({'input': 'response...'}))
while True:
self.write_message(json.dumps(message))
time.sleep(1)
# # 服务器主动关闭
# self.close()
# SendHandler.clients.remove(self)
def on_close(self):
# 客户端主动关闭
SendHandler.clients.remove(self)
class MainHandler(tornado.web.RequestHandler):
def get(self):
self.render('log_watch.html')
if __name__ == '__main__':
tornado.options.parse_command_line()
app = Application()
server = tornado.httpserver.HTTPServer(app)
server.listen(options.port)
tornado.ioloop.IOLoop.instance().start()

104
templates/log_watch.html Normal file
View File

@ -0,0 +1,104 @@
<html>
<head>
<link href="http://cdn.bootcss.com/bootstrap/3.2.0/css/bootstrap.min.css" rel="stylesheet">
<link href="http://cdn.bootcss.com/bootstrap/3.2.0/css/bootstrap-theme.min.css" rel="stylesheet">
<script src="http://cdn.bootcss.com/jquery/2.1.1/jquery.min.js"></script>
<script src="http://cdn.bootcss.com/bootstrap/3.2.0/js/bootstrap.min.js"></script>
<script>
var wsUri = "ws://j:8080/send";
var ws;
function connect(){
ws = new WebSocket(wsUri);
ws.onopen = function(evt){ console.log(evt.data) };
ws.onclose = function(evt){ console.log(evt.data) };
ws.onmessage = function(evt){ console.log(evt.data) };
ws.onerror = function(evt){ console.log(evt.data) };
return ws;
}
function send() {
var data = {
input:$("#input_data").val()
};
console.log('hello');
ws.send(JSON.stringify(data));
$("#message").empty();
ws.onopen = function() {
ws.send(JSON.stringify(data));
};
$("#message").append('<div class="panel-body"><p>');
ws.onmessage = function(event) {
$("#message").append(JSON.parse(event.data).input + "<br>");
};
ws.onclose = function(event) {
$("#message").append('</p></div>');
};
}
{# function send() {#}
{# var ws = new WebSocket("ws://j:8080/send");#}
{##}
{# var data = {#}
{# input:$("#input_data").val(),#}
{# };#}
{##}
{# $("#message").empty();#}
{# ws.onopen = function() {#}
{# ws.send(JSON.stringify(data));#}
{# };#}
{##}
{# $("#message").append('<div class="panel-body"><p>');#}
{# ws.onmessage = function(event) {#}
{# $("#message").append(JSON.parse(event.data).input + "<br>");#}
{# };#}
{##}
{# ws.onclose = function(event) {#}
{# $("#message").append('</p></div>');#}
{# };#}
{# }#}
function disconnect(){
ws.close();
}
</script>
</head>
<body>
<div id="test">
<form class="form-horizontal" role="form">
<div class="panel panel-default">
<div class="panel-heading">
<h5 class="panel-title">>>输入</h5>
</div>
<div class="panel-body">
<div class="form-group">
<div class="col-md-8">
<input type="text" class="form-control" id="input_data" value="">
</div>
</div>
<div class="form-group">
<div class="col-md-8">
<input type="button" class="btn btn-success" id="input_1_btn" onclick="connect();" value="连接" />
<input type="button" class="btn btn-success" id="input_btn" onclick="send();" value="发送" />
<input type="button" class="btn btn-success" id="input_2_btn" onclick="close();" value="关闭" />
</div>
</div>
</div>
</div>
</form>
<div class="panel panel-default">
<div class="panel-heading">
<h5 class="panel-title">>> 输出</h5>
</div>
<div class="panel-body">
<div id="message"></div>
</div>
</div>
</body>
</html>
</html>