MCPcopy Create free account
hub / github.com/miguelgrinberg/python-socketio / on

Method on

src/socketio/base_server.py:72–130  ·  view source on GitHub ↗

Register an event handler. :param event: The event name. It can be any string. The event names ``'connect'``, ``'message'`` and ``'disconnect'`` are reserved and should not be used. The ``'*'`` event name can be used to defin

(self, event, handler=None, namespace=None)

Source from the content-addressed store, hash-verified

70 return False
71
72 def on(self, event, handler=None, namespace=None):
73 """Register an event handler.
74
75 :param event: The event name. It can be any string. The event names
76 ``'connect'``, ``'message'`` and ``'disconnect'`` are
77 reserved and should not be used. The ``'*'`` event name
78 can be used to define a catch-all event handler.
79 :param handler: The function that should be invoked to handle the
80 event. When this parameter is not given, the method
81 acts as a decorator for the handler function.
82 :param namespace: The Socket.IO namespace for the event. If this
83 argument is omitted the handler is associated with
84 the default namespace. A catch-all namespace can be
85 defined by passing ``'*'`` as the namespace.
86
87 Example usage::
88
89 # as a decorator:
90 @sio.on('connect', namespace='/chat')
91 def connect_handler(sid, environ):
92 print('Connection request')
93 if environ['REMOTE_ADDR'] in blacklisted:
94 return False # reject
95
96 # as a method:
97 def message_handler(sid, msg):
98 print('Received message: ', msg)
99 sio.send(sid, 'response')
100 socket_io.on('message', namespace='/chat', handler=message_handler)
101
102 The arguments passed to the handler function depend on the event type:
103
104 - The ``'connect'`` event handler receives the ``sid`` (session ID) for
105 the client and the WSGI environment dictionary as arguments.
106 - The ``'disconnect'`` handler receives the ``sid`` for the client as
107 only argument.
108 - The ``'message'`` handler and handlers for custom event names receive
109 the ``sid`` for the client and the message payload as arguments. Any
110 values returned from a message handler will be passed to the client's
111 acknowledgement callback function if it exists.
112 - A catch-all event handler receives the event name as first argument,
113 followed by any arguments specific to the event.
114 - A catch-all namespace event handler receives the namespace as first
115 argument, followed by any arguments specific to the event.
116 - A combined catch-all namespace and catch-all event handler receives
117 the event name as first argument and the namespace as second
118 argument, followed by any arguments specific to the event.
119 """
120 namespace = namespace or '/'
121
122 def set_handler(handler):
123 if namespace not in self.handlers:
124 self.handlers[namespace] = {}
125 self.handlers[namespace][event] = handler
126 return handler
127
128 if handler is None:
129 return set_handler

Callers 3

eventMethod · 0.95
set_handlerMethod · 0.95
__init__Method · 0.45

Calls

no outgoing calls

Tested by

no test coverage detected