Add chat App

This commit is contained in:
2025-03-15 12:15:45 +01:00
parent 35617470c4
commit ab6e27f80e
8 changed files with 56 additions and 2 deletions

0
core/chat/__init__.py Normal file
View File

View File

8
core/chat/urls.py Normal file
View File

@@ -0,0 +1,8 @@
# chat/urls.py
from django.urls import path
from . import views
urlpatterns = [
path("", views.index, name="index"),
]

5
core/chat/views.py Normal file
View File

@@ -0,0 +1,5 @@
# chat/views.py
from django.shortcuts import render
def index(request):
return render(request, "chat/index.html")

View File

@@ -10,7 +10,15 @@ https://docs.djangoproject.com/en/5.1/howto/deployment/asgi/
import os
from django.core.asgi import get_asgi_application
from channels.routing import ProtocolTypeRouter
os.environ.setdefault('DJANGO_SETTINGS_MODULE', 'core.settings')
application = get_asgi_application()
#application = get_asgi_application()
application = ProtocolTypeRouter(
{
"http": get_asgi_application(),
# Just HTTP for now. (We can add other protocols later.)
}
)

View File

@@ -31,6 +31,8 @@ ALLOWED_HOSTS = ['127.0.0.1', 'localhost', '*']
# Application definition
INSTALLED_APPS = [
'daphne',
'chat',
'library.apps.LibraryConfig',
'homepage.apps.HomepageConfig',
'components.apps.ComponentsConfig',
@@ -128,4 +130,7 @@ STATICFILES_DIRS = [BASE_DIR / 'static']
DEFAULT_AUTO_FIELD = 'django.db.models.BigAutoField'
LOGIN_URL = '/account/login/'
LOGIN_URL = '/account/login/'
# Daphne
ASGI_APPLICATION = "core.asgi.application"

View File

@@ -23,4 +23,5 @@ urlpatterns = [
path('', include('homepage.urls')),
path('play/', include('play.urls')),
path('library/', include('library.urls')),
path('chat/', include('chat.urls')),
]

View File

@@ -0,0 +1,27 @@
<!-- chat/templates/chat/index.html -->
<!DOCTYPE html>
<html>
<head>
<meta charset="utf-8"/>
<title>Chat Rooms</title>
</head>
<body>
What chat room would you like to enter?<br>
<input id="room-name-input" type="text" size="100"><br>
<input id="room-name-submit" type="button" value="Enter">
<script>
document.querySelector('#room-name-input').focus();
document.querySelector('#room-name-input').onkeyup = function(e) {
if (e.key === 'Enter') { // enter, return
document.querySelector('#room-name-submit').click();
}
};
document.querySelector('#room-name-submit').onclick = function(e) {
var roomName = document.querySelector('#room-name-input').value;
window.location.pathname = '/chat/' + roomName + '/';
};
</script>
</body>
</html>