-
Notifications
You must be signed in to change notification settings - Fork 0
Expand file tree
/
Copy pathclipboard.py
More file actions
executable file
·73 lines (60 loc) · 2.11 KB
/
clipboard.py
File metadata and controls
executable file
·73 lines (60 loc) · 2.11 KB
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
61
62
63
64
65
66
67
68
69
70
71
72
73
import logging
import tornado.ioloop
import tornado.options
import tornado.web
import tornado.websocket
import os.path
import uuid
from tornado.options import define, options
define("port", default=33333, help="run on the given port", type=int)
class Application(tornado.web.Application):
def __init__(self):
handlers = [
(r"/", MainHandler),
(r"/sync", ChatSocketHandler),
]
settings = dict(
cookie_secret="HHHHHHHHHHHHHHHHHHHHHHHHHHSDAFSDFASDFdsaf",
template_path=os.path.join(os.path.dirname(__file__), "templates"),
static_path=os.path.join(os.path.dirname(__file__), "static"),
xsrf_cookies=True,
)
tornado.web.Application.__init__(self, handlers, **settings)
class MainHandler(tornado.web.RequestHandler):
def get(self):
self.render("index.html", msg=ChatSocketHandler.content)
class ChatSocketHandler(tornado.websocket.WebSocketHandler):
waiters = {}
content = ""
def allow_draft76(self):
# for iOS 5.0 Safari
return True
def open(self):
uid = str(uuid.uuid4())
ChatSocketHandler.waiters[self] = uid
self.write_message(uid)
def on_close(self):
del ChatSocketHandler.waiters[self]
@classmethod
def send_updates(cls, author):
logging.info("sending message to %d waiters", len(cls.waiters))
for waiter, uid in cls.waiters.items():
if uid == author:
logging.info("pass")
continue
try:
waiter.write_message(cls.content)
except:
logging.error("Error sending message", exc_info=True)
def on_message(self, message):
parsed = tornado.escape.json_decode(message)
logging.info("got message from %s" % parsed["uid"])
ChatSocketHandler.content = parsed["body"]
ChatSocketHandler.send_updates(parsed["uid"])
def main():
tornado.options.parse_command_line()
app = Application()
app.listen(options.port)
tornado.ioloop.IOLoop.instance().start()
if __name__ == "__main__":
main()