Ping example¶
The browser requests the ping method of the server every 2 seconds. When sending the ping request -> ping is written to the browser window and upon receiving the response a <- pong is added.
Server - examples/ping-server.py¶
#!/usr/bin/env python
import example_pythonpath
from example_utils import ExampleServer, ExampleStaticFile
class PingServer(ExampleServer):
urls = [
('/', ExampleStaticFile('ping.html')),
] + ExampleServer.urls
def rpc_ping(self, request):
return 'pong'
if __name__ == '__main__':
PingServer.main()
Client - examples/ping.html¶
<!DOCTYPE html>
<html>
<head>
<title>Mushroom Test Client</title>
</head>
<body>
<script type="text/javascript" src="/js/mushroom.js"></script>
<script type="text/javascript">
var client = new mushroom.Client({
url: '/'
});
client.connect();
window.setInterval(function() {
document.write('<div>→ ping</div>');
client.request('ping', null, function(data) {
document.write('<div>← pong</div>');
});
}, 2000);
</script>
</body>
</html>