You can not select more than 25 topics Topics must start with a letter or number, can include dashes ('-') and can be up to 35 characters long.

82 lines
1.9 KiB

# set async_mode to 'threading', 'eventlet', 'gevent' or 'gevent_uwsgi' to
# force a mode else, the best mode is selected automatically from what's
# installed
async_mode = None
import os
from django.http import HttpResponse
import socketio
basedir = os.path.dirname(os.path.realpath(__file__))
sio = socketio.Server(async_mode=async_mode)
thread = None
def index(request):
global thread
if thread is None:
thread = sio.start_background_task(background_thread)
return HttpResponse(open(os.path.join(basedir, 'static/index.html')))
def background_thread():
"""Example of how to send server generated events to clients."""
count = 0
while True:
sio.sleep(10)
count += 1
sio.emit('my_response', {'data': 'Server generated event'},
namespace='/test')
@sio.event
def my_event(sid, message):
sio.emit('my_response', {'data': message['data']}, room=sid)
@sio.event
def my_broadcast_event(sid, message):
sio.emit('my_response', {'data': message['data']})
@sio.event
def join(sid, message):
sio.enter_room(sid, message['room'])
sio.emit('my_response', {'data': 'Entered room: ' + message['room']},
room=sid)
@sio.event
def leave(sid, message):
sio.leave_room(sid, message['room'])
sio.emit('my_response', {'data': 'Left room: ' + message['room']},
room=sid)
@sio.event
def close_room(sid, message):
sio.emit('my_response',
{'data': 'Room ' + message['room'] + ' is closing.'},
room=message['room'])
sio.close_room(message['room'])
@sio.event
def my_room_event(sid, message):
sio.emit('my_response', {'data': message['data']}, room=message['room'])
@sio.event
def disconnect_request(sid):
sio.disconnect(sid)
@sio.event
def connect(sid, environ):
sio.emit('my_response', {'data': 'Connected', 'count': 0}, room=sid)
@sio.event
def disconnect(sid):
print('Client disconnected')