Created
May 13, 2010 13:16
-
-
Save masahitojp/399811 to your computer and use it in GitHub Desktop.
This file contains hidden or bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
#!/usr/bin/env python | |
import time | |
import datetime | |
import tornado.httpserver | |
import tornado.ioloop | |
import tornado.web | |
from tornado import websocket | |
import os | |
class ClockWebSocket(websocket.WebSocketHandler): | |
def open(self): | |
self._send_clock() | |
def _send_clock(self): | |
nowtime = datetime.datetime.now().strftime("%Y-%m-%d %H:%M:%S") | |
self.write_message(nowtime) | |
tornado.ioloop.IOLoop.instance().add_timeout(time.time() + 1, self._send_clock) | |
settings = { | |
"static_path": os.path.join(os.path.dirname(__file__)) | |
} | |
application = tornado.web.Application([ | |
(r"/", ClockWebSocket), | |
], **settings) | |
if __name__ == "__main__": | |
print "Listening on http://localhost:8888/" | |
server = tornado.httpserver.HTTPServer(application) | |
server.listen(8888) | |
tornado.ioloop.IOLoop.instance().start() |
This file contains hidden or bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
<!DOCTYPE html> | |
<html> | |
<head> | |
<script type="text/javascript" src="http://www.google.com/jsapi"></script> | |
<script type="text/javascript">google.load("jquery", "1.4.2");</script> | |
<script type='text/javascript' lang='javascript'> | |
var ws = new WebSocket('ws://localhost:8888'); | |
ws.onopen = function () { | |
}; | |
ws.onmessage = function(evt) { | |
$("#time").html(evt.data); | |
}; | |
</script> | |
</head> | |
<body> | |
<p>I am Clock!</p> | |
<div id="time"></div> | |
</body> | |
</html> |
Sign up for free
to join this conversation on GitHub.
Already have an account?
Sign in to comment