mirror of
https://github.com/clearlinux/uwsgi.git
synced 2026-08-29 02:05:38 +00:00
317b0f79a8
The uwsgi.websocket_handshake call on line 52 was not indented properly, causing the Python interpreter to choke. Converting the few tabs sprinkled through the document to spaces fixes that error.
57 lines
1.7 KiB
Python
57 lines
1.7 KiB
Python
#!./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))
|