Skip to content

Instantly share code, notes, and snippets.

@milnomada
Last active July 28, 2021 17:57
Show Gist options
  • Save milnomada/e542a1ffc3f50849edbbb75d6e9e6907 to your computer and use it in GitHub Desktop.
Save milnomada/e542a1ffc3f50849edbbb75d6e9e6907 to your computer and use it in GitHub Desktop.
Simple Socket.io application with Python and uwsgi
import logging
import socketio
from engineio.async_drivers import gevent
logging.basicConfig(level=logging.INFO)
sio = socketio.Server(async_mode="gevent_uwsgi", cors_allowed_origins='*', engineio_logger=True)
logger = logging.getLogger("socket-server")
@sio.on('connect', namespace='/')
def connect(sid, environ):ç
logger.info("connect {}".format(sid))
sio.emit('ok', room=sid)
@sio.on('message', namespace='/')
def message(sid, data):
sio.emit('x-message', data=data, room=data['sid'])
@sio.on('disconnect', namespace='/')
def disconnect(sid):
logger.info("disconnect {}".format(sid))
my_wsgi = socketio.WSGIApp(sio)
app = socketio.Middleware(sio, my_wsgi)
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment