debug5
This commit is contained in:
10
api/asgi.py
10
api/asgi.py
@@ -10,15 +10,19 @@ https://docs.djangoproject.com/en/5.1/howto/deployment/asgi/
|
|||||||
import os
|
import os
|
||||||
|
|
||||||
from channels.routing import ProtocolTypeRouter, URLRouter
|
from channels.routing import ProtocolTypeRouter, URLRouter
|
||||||
# from channels.auth import AuthMiddlewareStack
|
from channels.auth import AuthMiddlewareStack
|
||||||
|
from channels.security.websocket import AllowedHostsOriginValidator
|
||||||
|
|
||||||
from django.core.asgi import get_asgi_application
|
from django.core.asgi import get_asgi_application
|
||||||
# from .urls import ws_urlpatterns
|
from chat.url import websocket_urlpatterns
|
||||||
|
|
||||||
os.environ.setdefault("DJANGO_SETTINGS_MODULE", "api.settings")
|
os.environ.setdefault("DJANGO_SETTINGS_MODULE", "api.settings")
|
||||||
|
|
||||||
application = ProtocolTypeRouter(
|
application = ProtocolTypeRouter(
|
||||||
{
|
{
|
||||||
"http": get_asgi_application(),
|
"http": get_asgi_application(),
|
||||||
# "websocket": AuthMiddlewareStack(URLRouter(ws_urlpatterns)),
|
"websocket": AllowedHostsOriginValidator(
|
||||||
|
AuthMiddlewareStack(URLRouter(websocket_urlpatterns))
|
||||||
|
),
|
||||||
}
|
}
|
||||||
)
|
)
|
||||||
|
|||||||
@@ -54,6 +54,7 @@ INSTALLED_APPS = [
|
|||||||
"account",
|
"account",
|
||||||
"task",
|
"task",
|
||||||
"submission",
|
"submission",
|
||||||
|
"chat",
|
||||||
]
|
]
|
||||||
|
|
||||||
MIDDLEWARE = [
|
MIDDLEWARE = [
|
||||||
@@ -86,7 +87,7 @@ TEMPLATES = [
|
|||||||
]
|
]
|
||||||
|
|
||||||
WSGI_APPLICATION = "api.wsgi.application"
|
WSGI_APPLICATION = "api.wsgi.application"
|
||||||
|
ASGI_APPLICATION = "api.asgi.application"
|
||||||
|
|
||||||
# Database
|
# Database
|
||||||
# https://docs.djangoproject.com/en/5.1/ref/settings/#databases
|
# https://docs.djangoproject.com/en/5.1/ref/settings/#databases
|
||||||
|
|||||||
@@ -20,7 +20,7 @@ from django.urls import path
|
|||||||
from django.conf import settings
|
from django.conf import settings
|
||||||
from django.conf.urls.static import static
|
from django.conf.urls.static import static
|
||||||
from ninja import NinjaAPI
|
from ninja import NinjaAPI
|
||||||
from .ws import Consumer
|
from chat.consumers import Consumer
|
||||||
|
|
||||||
api = NinjaAPI()
|
api = NinjaAPI()
|
||||||
|
|
||||||
@@ -35,8 +35,4 @@ apis = [
|
|||||||
path("api/", api.urls),
|
path("api/", api.urls),
|
||||||
]
|
]
|
||||||
|
|
||||||
urlpatterns = apis + static(settings.MEDIA_URL, document_root=settings.MEDIA_ROOT)
|
urlpatterns = apis + static(settings.MEDIA_URL, document_root=settings.MEDIA_ROOT)
|
||||||
|
|
||||||
ws_urlpatterns = [
|
|
||||||
path("ws/", Consumer.as_asgi()),
|
|
||||||
]
|
|
||||||
11
api/ws.py
11
api/ws.py
@@ -1,11 +0,0 @@
|
|||||||
from channels.generic.websocket import AsyncWebsocketConsumer
|
|
||||||
|
|
||||||
class Consumer(AsyncWebsocketConsumer):
|
|
||||||
async def connect(self):
|
|
||||||
await self.accept()
|
|
||||||
|
|
||||||
async def disconnect(self):
|
|
||||||
pass
|
|
||||||
|
|
||||||
async def receive(self, text_data):
|
|
||||||
await self.send(text_data=text_data)
|
|
||||||
0
chat/__init__.py
Normal file
0
chat/__init__.py
Normal file
0
chat/api.py
Normal file
0
chat/api.py
Normal file
6
chat/apps.py
Normal file
6
chat/apps.py
Normal file
@@ -0,0 +1,6 @@
|
|||||||
|
from django.apps import AppConfig
|
||||||
|
|
||||||
|
|
||||||
|
class ChatConfig(AppConfig):
|
||||||
|
default_auto_field = 'django.db.models.BigAutoField'
|
||||||
|
name = 'chat'
|
||||||
17
chat/consumers.py
Normal file
17
chat/consumers.py
Normal file
@@ -0,0 +1,17 @@
|
|||||||
|
import json
|
||||||
|
|
||||||
|
from channels.generic.websocket import WebsocketConsumer
|
||||||
|
|
||||||
|
|
||||||
|
class ChatConsumer(WebsocketConsumer):
|
||||||
|
def connect(self):
|
||||||
|
self.accept()
|
||||||
|
|
||||||
|
def disconnect(self, close_code):
|
||||||
|
pass
|
||||||
|
|
||||||
|
def receive(self, text_data):
|
||||||
|
text_data_json = json.loads(text_data)
|
||||||
|
message = text_data_json["message"]
|
||||||
|
|
||||||
|
self.send(text_data=json.dumps({"message": message}))
|
||||||
0
chat/migrations/__init__.py
Normal file
0
chat/migrations/__init__.py
Normal file
3
chat/models.py
Normal file
3
chat/models.py
Normal file
@@ -0,0 +1,3 @@
|
|||||||
|
from django.db import models
|
||||||
|
|
||||||
|
# Create your models here.
|
||||||
6
chat/url.py
Normal file
6
chat/url.py
Normal file
@@ -0,0 +1,6 @@
|
|||||||
|
from django.urls import path
|
||||||
|
from .consumers import ChatConsumer
|
||||||
|
|
||||||
|
websocket_urlpatterns = [
|
||||||
|
path("ws/chat/", ChatConsumer.as_asgi()),
|
||||||
|
]
|
||||||
Reference in New Issue
Block a user