-
Notifications
You must be signed in to change notification settings - Fork 30
catch exception on socketio connection #1365
New issue
Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.
By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.
Already on GitHub? Sign in to your account
Merged
sanderegg
merged 3 commits into
ITISFoundation:master
from
sanderegg:bugfix/exception_socketio
Mar 13, 2020
Merged
Changes from 1 commit
Commits
Show all changes
3 commits
Select commit
Hold shift + click to select a range
File filter
Filter by extension
Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
There are no files selected for viewing
This file contains hidden or bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains hidden or bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Original file line number | Diff line number | Diff line change |
---|---|---|
|
@@ -7,6 +7,9 @@ | |
from .config import APP_CLIENT_SOCKET_DECORATED_HANDLERS_KEY, get_socket_server | ||
|
||
|
||
socketio_handlers_registry = [] | ||
|
||
|
||
def socket_io_handler(app: web.Application): | ||
"""this decorator allows passing additional paramters to python-socketio compatible handlers. | ||
I.e. python-socketio handler expect functions of type `async def function(sid, *args, **kwargs)` | ||
|
@@ -18,6 +21,7 @@ def decorator(func): | |
async def wrapped(*args, **kwargs): | ||
return await func(*args, **kwargs, app=app) | ||
|
||
|
||
return wrapped | ||
|
||
return decorator | ||
|
@@ -33,13 +37,9 @@ def has_socket_io_handler_signature(fun) -> bool: | |
|
||
def register_handlers(app: web.Application, module: ModuleType): | ||
sio = get_socket_server(app) | ||
predicate = ( | ||
lambda obj: inspect.isfunction(obj) | ||
and has_socket_io_handler_signature(obj) | ||
and inspect.iscoroutinefunction(obj) | ||
and inspect.getmodule(obj) == module | ||
) | ||
member_fcts = inspect.getmembers(module, predicate) | ||
member_fcts = [ | ||
fct for fct in socketio_handlers_registry if inspect.getmodule(fct) == module | ||
] | ||
# convert handler | ||
partial_fcts = [ | ||
socket_io_handler(app)(func_handler) for _, func_handler in member_fcts | ||
|
@@ -48,3 +48,23 @@ def register_handlers(app: web.Application, module: ModuleType): | |
# register the fcts | ||
for func in partial_fcts: | ||
sio.on(func.__name__, handler=func) | ||
|
||
|
||
def register_socketio_handler(func: callable) -> callable: | ||
There was a problem hiding this comment. Choose a reason for hiding this commentThe reason will be displayed to describe this comment to others. Learn more. MINOR: from typing import Callable, Dict, Optional
HandlerT = Callable[[str, Dict, web.Application], Optional[bool]]
def register_socketio_handler(func: HandlerT) -> HandlerT: And can even use |
||
"""this decorator appends handlers to a registry if they fit certain rules | ||
|
||
:param func: the function to call | ||
:type func: callable | ||
:return: the function to call | ||
:rtype: callable | ||
""" | ||
is_handler = ( | ||
inspect.isfunction(func) | ||
and has_socket_io_handler_signature(func) | ||
and inspect.iscoroutinefunction(func) | ||
) | ||
if is_handler: | ||
socketio_handlers_registry.append(func) | ||
else: | ||
raise SyntaxError("the function shall be of type fct(*args, app: web.Application") | ||
return func |
Add this suggestion to a batch that can be applied as a single commit.
This suggestion is invalid because no changes were made to the code.
Suggestions cannot be applied while the pull request is closed.
Suggestions cannot be applied while viewing a subset of changes.
Only one suggestion per line can be applied in a batch.
Add this suggestion to a batch that can be applied as a single commit.
Applying suggestions on deleted lines is not supported.
You must change the existing code in this line in order to create a valid suggestion.
Outdated suggestions cannot be applied.
This suggestion has been applied or marked resolved.
Suggestions cannot be applied from pending reviews.
Suggestions cannot be applied on multi-line comments.
Suggestions cannot be applied while the pull request is queued to merge.
Suggestion cannot be applied right now. Please check back later.
There was a problem hiding this comment.
Choose a reason for hiding this comment
The reason will be displayed to describe this comment to others. Learn more.
Minor Minor: capitalize :-)