Time pusher example¶
The server pushes the current time every second and the browser displays it.
Server - examples/time-pusher-server.py¶
#!/usr/bin/env python
from time import time
import gevent
import example_pythonpath
from example_utils import ExampleServer, ExampleStaticFile
class TimePusherServer(ExampleServer):
urls = [
('/', ExampleStaticFile('time-pusher.html')),
] + ExampleServer.urls
def server_init(self):
gevent.spawn(self.time_loop)
def time_loop(self):
while True:
gevent.sleep(1)
self.sessions.notify('time', time())
if __name__ == '__main__':
TimePusherServer.main()
Client - examples/time-pusher.html¶
<!DOCTYPE html>
<html>
<head>
<title>Mushroom Test Client</title>
</head>
<body>
<p id="date"></p>
<script type="text/javascript" src="/js/mushroom.js"></script>
<script type="text/javascript">
var client = new mushroom.Client({
url: '/'
});
client.connect();
client.method('time', function(request) {
var date = new Date(request.data * 1000);
var dateElement = document.getElementById('date');
dateElement.innerHTML = date.toString();
});
</script>
</body>
</html>