Skip to content

Commit

Permalink
Add ConnectionRefusedError exception from python-socketio (Fixes #989)
Browse files Browse the repository at this point in the history
  • Loading branch information
miguelgrinberg committed Jun 8, 2019
1 parent 75a75d4 commit 3f9fda8
Show file tree
Hide file tree
Showing 2 changed files with 15 additions and 5 deletions.
17 changes: 13 additions & 4 deletions docs/index.rst
Original file line number Diff line number Diff line change
Expand Up @@ -314,16 +314,25 @@ Connection Events
Flask-SocketIO also dispatches connection and disconnection events. The
following example shows how to register handlers for them::

@socketio.on('connect', namespace='/chat')
@socketio.on('connect')
def test_connect():
emit('my response', {'data': 'Connected'})

@socketio.on('disconnect', namespace='/chat')
@socketio.on('disconnect')
def test_disconnect():
print('Client disconnected')

The connection event handler can optionally return ``False`` to reject the
connection. This is so that the client can be authenticated at this point.
The connection event handler can return ``False`` to reject the connection, or
it can also raise `ConectionRefusedError`. This is so that the client can be
authenticated at this point. When using the exception, any arguments passed to
the exception are returned to the client in the error packet. Examples::

from flask_socketio import ConnectionRefusedError

@socketio.on('connect')
def connect():
if not self.authenticate(request.args):
raise ConnectionRefusedError('unauthorized!')

Note that connection and disconnection events are sent individually on each
namespace used.
Expand Down
3 changes: 2 additions & 1 deletion flask_socketio/__init__.py
Original file line number Diff line number Diff line change
Expand Up @@ -15,10 +15,11 @@
'install the latest version of python-socketio in its place.')
sys.exit(1)

import socketio
import flask
from flask import _request_ctx_stack, json as flask_json
from flask.sessions import SessionMixin
import socketio
from socketio.exceptions import ConnectionRefusedError
from werkzeug.debug import DebuggedApplication
from werkzeug.serving import run_with_reloader

Expand Down

0 comments on commit 3f9fda8

Please sign in to comment.