Skip to content

Commit 5401cc3

Browse files
committed
bah
1 parent f0ff8e5 commit 5401cc3

16 files changed

+558
-1
lines changed

v3/opt.py

Lines changed: 1 addition & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -138,7 +138,7 @@ def opt_pre_run_code_hook(self):
138138
last_cmd = self.history_manager.input_hist_parsed[-1]
139139
#print 'last_cmd:', last_cmd
140140
self.meta.opt_history.run_str(last_cmd, filtered_ns)
141-
urllib2.urlopen("http://localhost:8888/post", last_cmd)
141+
#urllib2.urlopen("http://localhost:8888/post", last_cmd)
142142

143143

144144
def load_ipython_extension(ipython):

v3/tornado-server/chatdemo.py

Lines changed: 103 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,103 @@
1+
# Adapted from https://github.com/facebook/tornado/tree/master/demos/websocket
2+
3+
import logging
4+
import tornado.escape
5+
import tornado.ioloop
6+
import tornado.options
7+
import tornado.web
8+
import tornado.websocket
9+
import os.path
10+
import uuid
11+
12+
from tornado.options import define, options
13+
14+
define("port", default=8888, help="run on the given port", type=int)
15+
16+
17+
class Application(tornado.web.Application):
18+
def __init__(self):
19+
handlers = [
20+
(r"/", MainHandler),
21+
(r"/post", PostHandler),
22+
(r"/chatsocket", ChatSocketHandler),
23+
]
24+
settings = dict(
25+
template_path=os.path.join(os.path.dirname(__file__), "templates"),
26+
static_path=os.path.join(os.path.dirname(__file__), "static"),
27+
)
28+
tornado.web.Application.__init__(self, handlers, **settings)
29+
30+
31+
class MainHandler(tornado.web.RequestHandler):
32+
def get(self):
33+
self.render("index.html", messages=ChatSocketHandler.cache)
34+
35+
36+
class PostHandler(tornado.web.RequestHandler):
37+
def post(self):
38+
message = self.request.body
39+
logging.info("got message %r", message)
40+
chat = {
41+
"id": str(uuid.uuid4()),
42+
"body": message
43+
}
44+
chat["html"] = tornado.escape.to_basestring(
45+
self.render_string("message.html", message=chat))
46+
47+
ChatSocketHandler.update_cache(chat)
48+
ChatSocketHandler.send_updates(chat)
49+
50+
51+
class ChatSocketHandler(tornado.websocket.WebSocketHandler):
52+
waiters = set()
53+
cache = []
54+
cache_size = 200
55+
56+
def allow_draft76(self):
57+
# for iOS 5.0 Safari
58+
return True
59+
60+
def open(self):
61+
ChatSocketHandler.waiters.add(self)
62+
63+
def on_close(self):
64+
ChatSocketHandler.waiters.remove(self)
65+
66+
@classmethod
67+
def update_cache(cls, chat):
68+
cls.cache.append(chat)
69+
if len(cls.cache) > cls.cache_size:
70+
cls.cache = cls.cache[-cls.cache_size:]
71+
72+
@classmethod
73+
def send_updates(cls, chat):
74+
logging.info("sending message to %d waiters", len(cls.waiters))
75+
for waiter in cls.waiters:
76+
try:
77+
waiter.write_message(chat)
78+
except:
79+
logging.error("Error sending message", exc_info=True)
80+
81+
def on_message(self, message):
82+
logging.info("got message %r", message)
83+
parsed = tornado.escape.json_decode(message)
84+
chat = {
85+
"id": str(uuid.uuid4()),
86+
"body": parsed["body"],
87+
}
88+
chat["html"] = tornado.escape.to_basestring(
89+
self.render_string("message.html", message=chat))
90+
91+
ChatSocketHandler.update_cache(chat)
92+
ChatSocketHandler.send_updates(chat)
93+
94+
95+
def main():
96+
tornado.options.parse_command_line()
97+
app = Application()
98+
app.listen(options.port)
99+
tornado.ioloop.IOLoop.instance().start()
100+
101+
102+
if __name__ == "__main__":
103+
main()
Lines changed: 103 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,103 @@
1+
# Adapted from https://github.com/facebook/tornado/tree/master/demos/websocket
2+
3+
import logging
4+
import tornado.escape
5+
import tornado.ioloop
6+
import tornado.options
7+
import tornado.web
8+
import tornado.websocket
9+
import os.path
10+
import uuid
11+
12+
from tornado.options import define, options
13+
14+
define("port", default=8888, help="run on the given port", type=int)
15+
16+
17+
class Application(tornado.web.Application):
18+
def __init__(self):
19+
handlers = [
20+
(r"/", MainHandler),
21+
(r"/post", PostHandler),
22+
(r"/chatsocket", ChatSocketHandler),
23+
]
24+
settings = dict(
25+
template_path=os.path.join(os.path.dirname(__file__), "templates"),
26+
static_path=os.path.join(os.path.dirname(__file__), "static"),
27+
)
28+
tornado.web.Application.__init__(self, handlers, **settings)
29+
30+
31+
class MainHandler(tornado.web.RequestHandler):
32+
def get(self):
33+
self.render("index.html", messages=ChatSocketHandler.cache)
34+
35+
36+
class PostHandler(tornado.web.RequestHandler):
37+
def post(self):
38+
message = self.request.body
39+
logging.info("got message %r", message)
40+
chat = {
41+
"id": str(uuid.uuid4()),
42+
"body": message
43+
}
44+
chat["html"] = tornado.escape.to_basestring(
45+
self.render_string("message.html", message=chat))
46+
47+
ChatSocketHandler.update_cache(chat)
48+
ChatSocketHandler.send_updates(chat)
49+
50+
51+
class ChatSocketHandler(tornado.websocket.WebSocketHandler):
52+
waiters = set()
53+
cache = []
54+
cache_size = 200
55+
56+
def allow_draft76(self):
57+
# for iOS 5.0 Safari
58+
return True
59+
60+
def open(self):
61+
ChatSocketHandler.waiters.add(self)
62+
63+
def on_close(self):
64+
ChatSocketHandler.waiters.remove(self)
65+
66+
@classmethod
67+
def update_cache(cls, chat):
68+
cls.cache.append(chat)
69+
if len(cls.cache) > cls.cache_size:
70+
cls.cache = cls.cache[-cls.cache_size:]
71+
72+
@classmethod
73+
def send_updates(cls, chat):
74+
logging.info("sending message to %d waiters", len(cls.waiters))
75+
for waiter in cls.waiters:
76+
try:
77+
waiter.write_message(chat)
78+
except:
79+
logging.error("Error sending message", exc_info=True)
80+
81+
def on_message(self, message):
82+
logging.info("got message %r", message)
83+
parsed = tornado.escape.json_decode(message)
84+
chat = {
85+
"id": str(uuid.uuid4()),
86+
"body": parsed["body"],
87+
}
88+
chat["html"] = tornado.escape.to_basestring(
89+
self.render_string("message.html", message=chat))
90+
91+
ChatSocketHandler.update_cache(chat)
92+
ChatSocketHandler.send_updates(chat)
93+
94+
95+
def main():
96+
tornado.options.parse_command_line()
97+
app = Application()
98+
app.listen(options.port)
99+
tornado.ioloop.IOLoop.instance().start()
100+
101+
102+
if __name__ == "__main__":
103+
main()

v3/tornado-server/static/chat.css

Lines changed: 56 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,56 @@
1+
/*
2+
* Copyright 2009 FriendFeed
3+
*
4+
* Licensed under the Apache License, Version 2.0 (the "License"); you may
5+
* not use this file except in compliance with the License. You may obtain
6+
* a copy of the License at
7+
*
8+
* http://www.apache.org/licenses/LICENSE-2.0
9+
*
10+
* Unless required by applicable law or agreed to in writing, software
11+
* distributed under the License is distributed on an "AS IS" BASIS, WITHOUT
12+
* WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied. See the
13+
* License for the specific language governing permissions and limitations
14+
* under the License.
15+
*/
16+
17+
body {
18+
background: white;
19+
margin: 10px;
20+
}
21+
22+
body,
23+
input {
24+
font-family: sans-serif;
25+
font-size: 10pt;
26+
color: black;
27+
}
28+
29+
table {
30+
border-collapse: collapse;
31+
border: 0;
32+
}
33+
34+
td {
35+
border: 0;
36+
padding: 0;
37+
}
38+
39+
#body {
40+
position: absolute;
41+
bottom: 10px;
42+
left: 10px;
43+
}
44+
45+
#input {
46+
margin-top: 0.5em;
47+
}
48+
49+
#inbox .message {
50+
padding-top: 0.25em;
51+
}
52+
53+
#nav {
54+
float: right;
55+
z-index: 99;
56+
}

v3/tornado-server/static/chat.js

Lines changed: 68 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,68 @@
1+
// Copyright 2009 FriendFeed
2+
//
3+
// Licensed under the Apache License, Version 2.0 (the "License"); you may
4+
// not use this file except in compliance with the License. You may obtain
5+
// a copy of the License at
6+
//
7+
// http://www.apache.org/licenses/LICENSE-2.0
8+
//
9+
// Unless required by applicable law or agreed to in writing, software
10+
// distributed under the License is distributed on an "AS IS" BASIS, WITHOUT
11+
// WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied. See the
12+
// License for the specific language governing permissions and limitations
13+
// under the License.
14+
15+
$(document).ready(function() {
16+
if (!window.console) window.console = {};
17+
if (!window.console.log) window.console.log = function() {};
18+
19+
$("#messageform").live("submit", function() {
20+
newMessage($(this));
21+
return false;
22+
});
23+
$("#messageform").live("keypress", function(e) {
24+
if (e.keyCode == 13) {
25+
newMessage($(this));
26+
return false;
27+
}
28+
});
29+
$("#message").select();
30+
updater.start();
31+
});
32+
33+
function newMessage(form) {
34+
var message = form.formToDict();
35+
updater.socket.send(JSON.stringify(message));
36+
form.find("input[type=text]").val("").select();
37+
}
38+
39+
jQuery.fn.formToDict = function() {
40+
var fields = this.serializeArray();
41+
var json = {}
42+
for (var i = 0; i < fields.length; i++) {
43+
json[fields[i].name] = fields[i].value;
44+
}
45+
if (json.next) delete json.next;
46+
return json;
47+
};
48+
49+
var updater = {
50+
socket: null,
51+
52+
start: function() {
53+
var url = "ws://" + location.host + "/chatsocket";
54+
updater.socket = new WebSocket(url);
55+
updater.socket.onmessage = function(event) {
56+
updater.showMessage(JSON.parse(event.data));
57+
}
58+
},
59+
60+
showMessage: function(message) {
61+
var existing = $("#m" + message.id);
62+
if (existing.length > 0) return;
63+
var node = $(message.html);
64+
node.hide();
65+
$("#inbox").append(node);
66+
node.slideDown();
67+
}
68+
};
Lines changed: 1 addition & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1 @@
1+
../../js/d3.v2.min.js
Lines changed: 1 addition & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1 @@
1+
../../js/jquery-1.8.2.min.js
Lines changed: 1 addition & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1 @@
1+
../../css/ui-lightness/jquery-ui-1.8.24.custom.css
Lines changed: 1 addition & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1 @@
1+
../../js/jquery-ui-1.8.24.custom.min.js
Lines changed: 1 addition & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1 @@
1+
../../js/jquery.ba-bbq.min.js

0 commit comments

Comments
 (0)