@@ -135,30 +135,39 @@ Defining Event Handlers
135135The Socket.IO protocol is event based. When a client wants to communicate with
136136the server it *emits * an event. Each event has a name, and a list of
137137arguments. The server registers event handler functions with the
138- :func: `socketio.Server.on ` decorator::
138+ :func: `socketio.Server.event ` or :func: `socketio.Server.on ` decorators::
139+
140+ @sio.event
141+ def my_event(sid, data):
142+ pass
139143
140144 @sio.on('my custom event')
141- def my_custom_event (sid, data):
145+ def another_event (sid, data):
142146 pass
143147
148+ In the first example the event name is obtained from the name of the handler
149+ function. The second example is slightly more verbose, but it allows the event
150+ name to be different than the function name or to include characters that are
151+ illegal in function names, such as spaces.
152+
144153For asyncio servers, event handlers can optionally be given as coroutines::
145154
146- @sio.on('my custom event')
147- async def my_custom_event (sid, data):
155+ @sio.event
156+ async def my_event (sid, data):
148157 pass
149158
150159The ``sid `` argument is the Socket.IO session id, a unique identifier of each
151160client connection. All the events sent by a given client will have the same
152161``sid `` value.
153162
154- The ``connect `` and ``disconnect `` are special; they are invoked automatically
155- when a client connects or disconnects from the server::
163+ The ``connect `` and ``disconnect `` events are special; they are invoked
164+ automatically when a client connects or disconnects from the server::
156165
157- @sio.on('connect')
166+ @sio.event
158167 def connect(sid, environ):
159168 print('connect ', sid)
160169
161- @sio.on('disconnect')
170+ @sio.event
162171 def disconnect(sid):
163172 print('disconnect ', sid)
164173
@@ -172,9 +181,9 @@ headers. After inspecting the request, the connect event handler can return
172181Sometimes it is useful to pass data back to the client being rejected. In that
173182case instead of returning ``False ``
174183:class: `socketio.exceptions.ConnectionRefusedError ` can be raised, and all of
175- its argument will be sent to the client with the rejection::
184+ its arguments will be sent to the client with the rejection message ::
176185
177- @sio.on('connect')
186+ @sio.event
178187 def connect(sid, environ):
179188 raise ConnectionRefusedError('authentication failed')
180189
@@ -210,8 +219,8 @@ has processed the event. While this is entirely managed by the client, the
210219server can provide a list of values that are to be passed on to the callback
211220function, simply by returning them from the handler function::
212221
213- @sio.on('my event', namespace='/chat')
214- def my_event_handler (sid, data):
222+ @sio.event
223+ def my_event (sid, data):
215224 # handle the message
216225 return "OK", 123
217226
@@ -240,6 +249,10 @@ that use multiple namespaces specify the correct namespace when setting up
240249their event handlers and rooms, using the optional ``namespace `` argument
241250available in all the methods in the :class: `socketio.Server ` class::
242251
252+ @sio.event(namespace='/chat')
253+ def my_custom_event(sid, data):
254+ pass
255+
243256 @sio.on('my custom event', namespace='/chat')
244257 def my_custom_event(sid, data):
245258 pass
@@ -322,11 +335,11 @@ rooms as needed and can be moved between rooms as often as necessary.
322335
323336::
324337
325- @sio.on('chat')
338+ @sio.event
326339 def begin_chat(sid):
327340 sio.enter_room(sid, 'chat_users')
328341
329- @sio.on('exit_chat')
342+ @sio.event
330343 def exit_chat(sid):
331344 sio.leave_room(sid, 'chat_users')
332345
@@ -338,8 +351,8 @@ during the broadcast.
338351
339352::
340353
341- @sio.on('my message')
342- def message (sid, data):
354+ @sio.event
355+ def my_message (sid, data):
343356 sio.emit('my reply', data, room='chat_users', skip_sid=sid)
344357
345358User Sessions
@@ -353,52 +366,52 @@ of the connection, such as usernames or user ids.
353366The ``save_session() `` and ``get_session() `` methods are used to store and
354367retrieve information in the user session::
355368
356- @sio.on('connect')
357- def on_connect (sid, environ):
369+ @sio.event
370+ def connect (sid, environ):
358371 username = authenticate_user(environ)
359372 sio.save_session(sid, {'username': username})
360373
361- @sio.on('message')
362- def on_message (sid, data):
374+ @sio.event
375+ def message (sid, data):
363376 session = sio.get_session(sid)
364377 print('message from ', session['username'])
365378
366379For the ``asyncio `` server, these methods are coroutines::
367380
368381
369- @sio.on('connect')
370- async def on_connect (sid, environ):
382+ @sio.event
383+ async def connect (sid, environ):
371384 username = authenticate_user(environ)
372385 await sio.save_session(sid, {'username': username})
373386
374- @sio.on('message')
375- async def on_message (sid, data):
387+ @sio.event
388+ async def message (sid, data):
376389 session = await sio.get_session(sid)
377390 print('message from ', session['username'])
378391
379392The session can also be manipulated with the `session() ` context manager::
380393
381- @sio.on('connect')
382- def on_connect (sid, environ):
394+ @sio.event
395+ def connect (sid, environ):
383396 username = authenticate_user(environ)
384397 with sio.session(sid) as session:
385398 session['username'] = username
386399
387- @sio.on('message')
388- def on_message (sid, data):
400+ @sio.event
401+ def message (sid, data):
389402 with sio.session(sid) as session:
390403 print('message from ', session['username'])
391404
392405For the ``asyncio `` server, an asynchronous context manager is used::
393406
394- @sio.on('connect')
395- def on_connect (sid, environ):
407+ @sio.event
408+ def connect (sid, environ):
396409 username = authenticate_user(environ)
397410 async with sio.session(sid) as session:
398411 session['username'] = username
399412
400- @sio.on('message')
401- def on_message (sid, data):
413+ @sio.event
414+ def message (sid, data):
402415 async with sio.session(sid) as session:
403416 print('message from ', session['username'])
404417
0 commit comments