@@ -179,7 +179,128 @@ def _get_item(model, view_arg, name):
179179```
180180
181181
182- ## Example 3 from indico
182+ ## Example 3 from Flask-SocketIO
183+ [ Flask-SocketIO] ( https://github.com/miguelgrinberg/Flask-SocketIO )
184+ ([ PyPI package information] ( https://pypi.org/project/Flask-SocketIO/ ) ,
185+ [ official tutorial] ( https://blog.miguelgrinberg.com/post/easy-websockets-with-flask-and-gevent )
186+ and
187+ [ project documentation] ( https://flask-socketio.readthedocs.io/en/latest/ ) )
188+ is a code library by [ Miguel Grinberg] ( https://blog.miguelgrinberg.com/index )
189+ that provides Socket.IO integration for [ Flask] ( /flask.html ) applications.
190+ This extension makes it easier to add bi-directional communications on the
191+ web via the [ WebSockets] ( /websockets.html ) protocol.
192+
193+ The Flask-SocketIO project is open source under the
194+ [ MIT license] ( https://github.com/miguelgrinberg/Flask-SocketIO/blob/master/LICENSE ) .
195+
196+ [ ** Flask-SocketIO / flask_socketio / __ init__ .py** ] ( https://github.com/miguelgrinberg/Flask-SocketIO/blob/master/./flask_socketio/__init__.py )
197+
198+ ``` python
199+ # __init__.py
200+ from functools import wraps
201+ import os
202+ import sys
203+
204+ gevent_socketio_found = True
205+ try :
206+ from socketio import socketio_manage
207+ except ImportError :
208+ gevent_socketio_found = False
209+ if gevent_socketio_found:
210+ print (' The gevent-socketio package is incompatible with this version of '
211+ ' the Flask-SocketIO extension. Please uninstall it, and then '
212+ ' install the latest version of python-socketio in its place.' )
213+ sys.exit(1 )
214+
215+ import flask
216+ ~~ from flask import _request_ctx_stack, has_request_context, json as flask_json
217+ from flask.sessions import SessionMixin
218+ import socketio
219+ from socketio.exceptions import ConnectionRefusedError
220+ from werkzeug.debug import DebuggedApplication
221+ from werkzeug.serving import run_with_reloader
222+
223+ from .namespace import Namespace
224+ from .test_client import SocketIOTestClient
225+
226+ __version__ = ' 4.3.2dev'
227+
228+
229+ class _SocketIOMiddleware (socketio .WSGIApp ):
230+ def __init__ (self , socketio_app , flask_app , socketio_path = ' socket.io' ):
231+ self .flask_app = flask_app
232+ super (_SocketIOMiddleware, self ).__init__ (socketio_app,
233+ flask_app.wsgi_app,
234+ socketio_path = socketio_path)
235+
236+ def __call__ (self , environ , start_response ):
237+ environ = environ.copy()
238+ environ[' flask.app' ] = self .flask_app
239+ return super (_SocketIOMiddleware, self ).__call__ (environ,
240+ start_response)
241+
242+
243+ # # ... source file abbreviated to get to has_request_context examples ...
244+
245+
246+ self .default_exception_handler = exception_handler
247+ return exception_handler
248+
249+ def on_event (self , message , handler , namespace = None ):
250+ self .on(message, namespace = namespace)(handler)
251+
252+ def on_namespace (self , namespace_handler ):
253+ if not isinstance (namespace_handler, Namespace):
254+ raise ValueError (' Not a namespace instance.' )
255+ namespace_handler._set_socketio(self )
256+ if self .server:
257+ self .server.register_namespace(namespace_handler)
258+ else :
259+ self .namespace_handlers.append(namespace_handler)
260+
261+ def emit (self , event , * args , ** kwargs ):
262+ namespace = kwargs.pop(' namespace' , ' /' )
263+ room = kwargs.pop(' room' , None )
264+ include_self = kwargs.pop(' include_self' , True )
265+ skip_sid = kwargs.pop(' skip_sid' , None )
266+ if not include_self and not skip_sid:
267+ skip_sid = flask.request.sid
268+ callback = kwargs.pop(' callback' , None )
269+ if callback:
270+ sid = None
271+ ~~ if has_request_context():
272+ sid = getattr (flask.request, ' sid' , None )
273+ original_callback = callback
274+
275+ def _callback_wrapper (* args ):
276+ return self ._handle_event(original_callback, None , namespace,
277+ sid, * args)
278+
279+ if sid:
280+ callback = _callback_wrapper
281+ self .server.emit(event, * args, namespace = namespace, room = room,
282+ skip_sid = skip_sid, callback = callback, ** kwargs)
283+
284+ def send (self , data , json = False , namespace = None , room = None ,
285+ callback = None , include_self = True , skip_sid = None , ** kwargs ):
286+ skip_sid = flask.request.sid if not include_self else skip_sid
287+ if json:
288+ self .emit(' json' , data, namespace = namespace, room = room,
289+ skip_sid = skip_sid, callback = callback, ** kwargs)
290+ else :
291+ self .emit(' message' , data, namespace = namespace, room = room,
292+ skip_sid = skip_sid, callback = callback, ** kwargs)
293+
294+ def close_room (self , room , namespace = None ):
295+ self .server.close_room(room, namespace)
296+
297+
298+ # # ... source file continues with no further has_request_context examples...
299+
300+ ```
301+
302+
303+ ## Example 4 from indico
183304[ indico] ( https://github.com/indico/indico )
184305([ project website] ( https://getindico.io/ ) ,
185306[ documentation] ( https://docs.getindico.io/en/stable/installation/ )
@@ -231,6 +352,12 @@ class AddRequestIDFilter(object):
231352 return True
232353
233354
355+ class AddUserIDFilter (object ):
356+ def filter (self , record ):
357+ ~~ record.user_id = unicode (session.user.id) if has_request_context() and session and session.user else ' -'
358+ return True
359+
360+
234361class RequestInfoFormatter (logging .Formatter ):
235362 def format (self , record ):
236363 rv = super (RequestInfoFormatter, self ).format(record)
0 commit comments