diff options
| author | Unbit <info@unbit.it> | 2013-02-03 20:52:42 +0100 |
|---|---|---|
| committer | Unbit <info@unbit.it> | 2013-02-03 20:52:42 +0100 |
| commit | 94aed139782bd58358e4109c88f972d30f3f7414 (patch) | |
| tree | 9596e2efd1606628034458df5004a5bc505e6cee /tests/websockets_echo.py | |
| parent | 96796453540ffebd9bea2f706ebbb16a4fe96249 (diff) | |
| download | uwsgi-94aed139782bd58358e4109c88f972d30f3f7414.tar.gz | |
simplified --http-websockets
Diffstat (limited to 'tests/websockets_echo.py')
| -rw-r--r-- | tests/websockets_echo.py | 56 |
1 files changed, 56 insertions, 0 deletions
diff --git a/tests/websockets_echo.py b/tests/websockets_echo.py new file mode 100644 index 00000000..19e791df --- /dev/null +++ b/tests/websockets_echo.py @@ -0,0 +1,56 @@ +#!./uwsgi --https :8443,foobar.crt,foobar.key --http-raw-body --gevent 100 --module tests.websocket_echo +import uwsgi +import time + +def application(env, sr): + + ws_scheme = 'ws' + if 'HTTPS' in env or env['wsgi.url_scheme'] == 'https': + ws_scheme = 'wss' + + if env['PATH_INFO'] == '/': + sr('200 OK', [('Content-Type','text/html')]) + return """ + <html> + <head> + <script language="Javascript"> + var s = new WebSocket("%s://%s/foobar/"); + s.onopen = function() { + alert("connected !!!"); + s.send("ciao"); + }; + s.onmessage = function(e) { + var bb = document.getElementById('blackboard') + var html = bb.innerHTML; + bb.innerHTML = html + '<br/>' + e.data; + }; + + s.onerror = function(e) { + alert(e); + } + + s.onclose = function(e) { + alert("connection closed"); + } + + function invia() { + var value = document.getElementById('testo').value; + s.send(value); + } + </script> + </head> + <body> + <h1>WebSocket</h1> + <input type="text" id="testo"/> + <input type="button" value="invia" onClick="invia();"/> + <div id="blackboard" style="width:640px;height:480px;background-color:black;color:white;border: solid 2px red;overflow:auto"> + </div> + </body> + </html> + """ % (ws_scheme, env['HTTP_HOST']) + elif env['PATH_INFO'] == '/foobar/': + uwsgi.websocket_handshake(env['HTTP_SEC_WEBSOCKET_KEY'], env.get('HTTP_ORIGIN', '')) + print "websockets..." + while True: + msg = uwsgi.websocket_recv() + uwsgi.websocket_send("[%s] %s" % (time.time(), msg)) |
