MCPcopy
hub / github.com/miguelgrinberg/Flask-SocketIO / _handle_event

Method _handle_event

src/flask_socketio/__init__.py:810–874  ·  view source on GitHub ↗
(self, handler, message, namespace, sid, *args)

Source from the content-addressed store, hash-verified

808 flask_test_client=flask_test_client)
809
810 def _handle_event(self, handler, message, namespace, sid, *args):
811 environ = self.server.get_environ(sid, namespace=namespace)
812 if not environ:
813 # we don't have record of this client, ignore this event
814 return '', 400
815 app = environ['flask.app']
816 with app.request_context(environ):
817 if self.manage_session:
818 # manage a separate session for this client's Socket.IO events
819 # created as a copy of the regular user session
820 if 'saved_session' not in environ:
821 environ['saved_session'] = _ManagedSession(flask.session)
822 session_obj = environ['saved_session']
823 if hasattr(flask, 'globals') and \
824 hasattr(flask.globals, 'app_ctx'):
825 if hasattr(flask.globals.app_ctx, 'session'):
826 # get context for flask >= 3.2
827 ctx = flask.globals.app_ctx._get_current_object()
828 else:
829 # get context for Flask >= 2.2, < 3.2
830 ctx = flask.globals.request_ctx._get_current_object()
831 else: # pragma: no cover
832 # get context for Flask < 2.2
833 ctx = flask._request_ctx_stack.top
834 if hasattr(ctx, '_session'):
835 # update session for Flask >= 3.1.3
836 ctx._session = session_obj
837 else:
838 # update session for Flask < 3.1.3
839 ctx.session = session_obj
840 else:
841 # let Flask handle the user session
842 # for cookie based sessions, this effectively freezes the
843 # session to its state at connection time
844 # for server-side sessions, this allows HTTP and Socket.IO to
845 # share the session, with both having read/write access to it
846 session_obj = flask.session._get_current_object()
847 flask.request.sid = sid
848 flask.request.namespace = namespace
849 flask.request.event = {'message': message, 'args': args}
850 try:
851 if message == 'connect':
852 auth = args[1] if len(args) > 1 else None
853 try:
854 ret = handler(auth)
855 except TypeError:
856 ret = handler()
857 else:
858 ret = handler(*args)
859 except ConnectionRefusedError:
860 raise # let this error bubble up to python-socketio
861 except:
862 err_handler = self.exception_handlers.get(
863 namespace, self.default_exception_handler)
864 if err_handler is None:
865 raise
866 type, value, traceback = sys.exc_info()
867 return err_handler(value)

Callers 3

_handlerMethod · 0.95
_callback_wrapperMethod · 0.95
trigger_eventMethod · 0.80

Calls 1

_ManagedSessionClass · 0.85

Tested by

no test coverage detected