Mercurial > cosocket
changeset 15:71e093cafa69
The example server's front page now lets you play around with server push.
author | Atul Varma <varmaa@toolness.com> |
---|---|
date | Sun, 19 Apr 2009 14:36:04 -0700 |
parents | 545927beb490 |
children | 4b3a30d863cc |
files | example.html example.py |
diffstat | 2 files changed, 72 insertions(+), 14 deletions(-) [+] |
line wrap: on
line diff
--- /dev/null Thu Jan 01 00:00:00 1970 +0000 +++ b/example.html Sun Apr 19 14:36:04 2009 -0700 @@ -0,0 +1,47 @@ +<html> +<head> + <title>Example cosocket stuff</title> +</head> +<body> +<p> + This page should print stuff when you send messages to the + server by clicking <span style="background: yellow; cursor: pointer" + id="send">here</span>. +</p> +<div id="messages"> +</div> +<script> +document.getElementById("send").addEventListener( + "click", + function() { + var req = new XMLHttpRequest(); + req.open('POST', 'send', true); + req.send(null); + }, + false +); + +window.addEventListener( + "load", + function() { + var req = new XMLHttpRequest(); + req.multipart = true; + req.open('GET', 'listen', true); + req.overrideMimeType('text/plain'); + req.addEventListener( + "load", + function onload(evt) { + var messages = document.getElementById("messages"); + var message = document.createElement('div'); + message.textContent = req.responseText; + messages.appendChild(message); + }, + false + ); + req.send(null); + }, + false +); +</script> +</body> +</html>
--- a/example.py Sun Apr 19 13:24:23 2009 -0700 +++ b/example.py Sun Apr 19 14:36:04 2009 -0700 @@ -1,34 +1,45 @@ from cosocket import * import channels -def _make_http_response(msg): +def _make_http_response(msg, mimetype = 'text/plain'): return ('HTTP/1.1 200 OK\r\n' + 'Content-Length: %d\r\n' % len(msg) + - 'Content-Type: text/plain\r\n\r\n' + + 'Content-Type: %s\r\n\r\n' % mimetype + msg) +def _make_chunk(msg, mimetype = 'text/plain'): + return ('Content-Length: %d\r\n' % len(msg) + + 'Content-Type: text/plain\r\n\r\n' + + msg + + '\r\n--chunk\r\n') + +num_messages = 0 + def example_http_server_coroutine(addr): + global num_messages request_line = yield until_received(terminator = '\r\n') request_headers = yield until_received(terminator = '\r\n\r\n') req_parts = request_line.split() if req_parts[1] == '/listen': yield until_sent('HTTP/1.1 200 OK\r\n' + - 'Content-Type: multipart/mixed; ' + - 'boundary="chunk"\r\n\r\n') - i = 0 + 'Content-Type: multipart/x-mixed-replace; ' + + 'boundary="chunk"\r\n\r\n--chunk\r\n') while 1: - i += 1 - ip = yield channels.until_message_received('global') - msg = 'Got message %d from %s.\r\n' % (i, ip) - yield until_sent('--chunk\r\n' + - 'Content-Length: %d\r\n' % len(msg) + - 'Content-Type: text/plain\r\n\r\n' + - msg) + ip, num = yield channels.until_message_received('global') + msg = 'Got message %d from %s.' % (num, ip) + yield until_sent(_make_chunk(msg)) elif req_parts[1] == '/send': - yield channels.until_message_sent('global', addr[0]) + num_messages += 1 + yield channels.until_message_sent('global', + (addr[0], num_messages)) yield until_sent(_make_http_response('sent.')) else: - yield until_sent(_make_http_response('hello %s.' % addr[0])) + yield until_sent(_make_http_response( + # TODO: This is bad since we're reading the file + # synchronously. + open('example.html', 'r').read(), + 'text/html' + )) def _example_nested_coroutine(): chunk = yield until_received(bytes = 20)