mirror of
https://github.com/frappe/gunicorn.git
synced 2026-01-14 11:09:11 +08:00
fixes gevent websocket example . close #343
This commit is contained in:
parent
90e698aa3c
commit
7a32616407
@ -10,6 +10,7 @@ import struct
|
||||
import logging
|
||||
from socket import error as SocketError
|
||||
|
||||
import gevent
|
||||
from gunicorn.workers.async import ALREADY_HANDLED
|
||||
|
||||
logger = logging.getLogger(__name__)
|
||||
@ -408,3 +409,37 @@ class WebSocket(object):
|
||||
self._send_closing_frame()
|
||||
self.socket.shutdown(True)
|
||||
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)
|
||||
|
||||
Loading…
x
Reference in New Issue
Block a user