Use flash socket / ws request from context

I would like to be able to use the Flask request from the context.

I understand that with Flask 0.10 there is a decorator ( @copy_current_request_context) available for this, and that is how I use this decorator to try and modify flask-socket . In particular, the decorator @socket.route, which is part of the flask sockets :

def route(self, rule, request, **options):

    def decorator(f):
        endpoint = options.pop('endpoint', None)
        @copy_current_request_context
        def do_some_work():
            env = request.environ['wsgi.websocket']
            self.add_url_rule(rule, endpoint, f,env, **options)
        gevent.spawn(do_some_work)
        return f
    return decorator

Although the error that occurs makes sense to me - I guess there is a way to do what I want.

RuntimeError: This decorator can only be used at local scopes when a   
request context is on the stack.  For instance within view functions.

I tried passing the request to the decorator, but that didn't work.

To give a little more context, I am trying to add request.environ['wsgi.websocket']a dict inside the Sockets object to be able to access the variable ws(which I understand as the request environment).

ws.send() -, @route - , , .

- Socket-IO, socketio - , , send() recieve() - Sockets , ws, request.environ['wsgi.websocket']

+4

Source: https://habr.com/ru/post/1599488/


All Articles