fixes gevent websocket example . close #343

This commit is contained in:
benoitc 2012-05-15 00:13:48 +02:00
parent 90e698aa3c
commit 7a32616407

View File

@ -10,6 +10,7 @@ import struct
import logging import logging
from socket import error as SocketError from socket import error as SocketError
import gevent
from gunicorn.workers.async import ALREADY_HANDLED from gunicorn.workers.async import ALREADY_HANDLED
logger = logging.getLogger(__name__) logger = logging.getLogger(__name__)
@ -408,3 +409,37 @@ class WebSocket(object):
self._send_closing_frame() self._send_closing_frame()
self.socket.shutdown(True) self.socket.shutdown(True)
self.socket.close() self.socket.close()
# demo app
import os
import random
def handle(ws):
""" This is the websocket handler function. Note that we
can dispatch based on path in here, too."""
if ws.path == '/echo':
while True:
m = ws.wait()
if m is None:
break
ws.send(m)
elif ws.path == '/data':
for i in xrange(10000):
ws.send("0 %s %s\n" % (i, random.random()))
gevent.sleep(0.1)
wsapp = WebSocketWSGI(handle)
def app(environ, start_response):
""" This resolves to the web page or the websocket depending on
the path."""
if environ['PATH_INFO'] == '/' or environ['PATH_INFO'] == "":
data = open(os.path.join(
os.path.dirname(__file__),
'websocket.html')).read()
data = data % environ
start_response('200 OK', [('Content-Type', 'text/html'),
('Content-Length', len(data))])
return [data]
else:
return wsapp(environ, start_response)