Thanks to visit codestin.com
Credit goes to github.com

Skip to content

Commit c4eb4c1

Browse files
author
zeekay
committed
Make it possible to access WSGI environ.
Fixes zeekay#2.
1 parent 1dc288d commit c4eb4c1

File tree

4 files changed

+29
-10
lines changed

4 files changed

+29
-10
lines changed

.gitignore

Lines changed: 1 addition & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -2,3 +2,4 @@
22
*.pyc
33
*.pyo
44
dist/
5+
build/

README.rst

Lines changed: 15 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -142,3 +142,18 @@ WebSocket client abstraction with fully non-blocking methods.
142142
``close()``
143143

144144
``connected``
145+
146+
147+
Advanced Usage
148+
--------------
149+
Normally websocket routes happen outside of the normal request context. You can
150+
get a request context in your websocket handler by using
151+
`app.request_context`::
152+
153+
app = Flask(__name__)
154+
ws = GeventWebSocket(app)
155+
156+
@ws.route('/websocket')
157+
def websocket(ws):
158+
with app.request_context(ws.environ):
159+
print request.args

flask_uwsgi_websocket/_gevent.py

Lines changed: 8 additions & 6 deletions
Original file line numberDiff line numberDiff line change
@@ -10,15 +10,17 @@
1010

1111

1212
class GeventWebSocketClient(object):
13-
def __init__(self, fd, send_event, send_queue, recv_event, recv_queue, timeout=60):
14-
self.id = str(uuid.uuid1())
15-
self.fd = fd
13+
def __init__(self, environ, fd, send_event, send_queue, recv_event, recv_queue, timeout=60):
14+
self.environ = environ
15+
self.fd = fd
1616
self.send_event = send_event
1717
self.send_queue = send_queue
1818
self.recv_event = recv_event
1919
self.recv_queue = recv_queue
20-
self.timeout = timeout
21-
self.connected = True
20+
self.timeout = timeout
21+
22+
self.id = str(uuid.uuid1())
23+
self.connected = True
2224

2325
def send(self, message):
2426
self.send_queue.put(message)
@@ -51,7 +53,7 @@ def __call__(self, environ, start_response):
5153
recv_queue = Queue(maxsize=1)
5254

5355
# create websocket client
54-
client = self.client(uwsgi.connection_fd(), send_event, send_queue, recv_event, recv_queue)
56+
client = self.client(environ, uwsgi.connection_fd(), send_event, send_queue, recv_event, recv_queue)
5557

5658
# spawn handler
5759
handler = spawn(handler, client)

flask_uwsgi_websocket/websocket.py

Lines changed: 5 additions & 4 deletions
Original file line numberDiff line numberDiff line change
@@ -9,10 +9,11 @@ class WebSocketClient(object):
99
Default WebSocket client has a blocking recieve method, but still exports
1010
rest of uWSGI API.
1111
'''
12-
def __init__(self, fd, timeout=60):
13-
self.fd = fd
12+
def __init__(self, environ, fd, timeout=60):
13+
self.environ = environ
14+
self.fd = fd
1415
self.timeout = timeout
15-
self.id = str(uuid.uuid1())
16+
self.id = str(uuid.uuid1())
1617

1718
def receive(self):
1819
return uwsgi.websocket_recv()
@@ -51,7 +52,7 @@ def __call__(self, environ, start_response):
5152

5253
if handler:
5354
uwsgi.websocket_handshake(environ['HTTP_SEC_WEBSOCKET_KEY'], environ.get('HTTP_ORIGIN', ''))
54-
handler(self.client(uwsgi.connection_fd(), self.websocket.timeout))
55+
handler(self.client(environ, uwsgi.connection_fd(), self.websocket.timeout))
5556
else:
5657
return self.wsgi_app(environ, start_response)
5758

0 commit comments

Comments
 (0)