pip install django-socketio python INSTALLED_APPS = [ ... 'socketio', ... ] MIDDLEWARE_CLASSES = [ ... 'socketio.middleware.SocketIOMiddleware', ... ] TEMPLATE_CONTEXT_PROCESSORS = [ ... 'socketio.context_processors.socketio', ... ] SOCKETIO_HOST = 'localhost' SOCKETIO_PORT = 8000 python from django.shortcuts import render from socketio import socketio_manage from socketio.mixins import RoomsMixin from socketio.namespace import BaseNamespace class ChatNamespace(BaseNamespace, RoomsMixin): def on_chat_message(self, message): self.emit('chat_message', message) def chat(request): return render(request, 'chat.html') def socketio(request): socketio_manage(request.environ, {'': ChatNamespace}, request) python @sio.on_connect def on_connect(request): if not authenticate_user(request): return False if not authorize_user(request): return False return True pip install django-cors-headers python INSTALLED_APPS = [ ... 'corsheaders', ... ] MIDDLEWARE_CLASSES = [ ... 'corsheaders.middleware.CorsMiddleware', 'django.middleware.common.CommonMiddleware', ... ] CORS_ORIGIN_ALLOW_ALL = True


上一篇:
下一篇:
切换中文