WIP: Draft: Resolve "Chat App Websocket Tutorial" #174
0
core/chat/__init__.py
Normal file
0
core/chat/__init__.py
Normal file
0
core/chat/migrations/__init__.py
Normal file
0
core/chat/migrations/__init__.py
Normal file
8
core/chat/urls.py
Normal file
8
core/chat/urls.py
Normal 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
5
core/chat/views.py
Normal file
@@ -0,0 +1,5 @@
|
|||||||
|
# chat/views.py
|
||||||
|
from django.shortcuts import render
|
||||||
|
|
||||||
|
def index(request):
|
||||||
|
return render(request, "chat/index.html")
|
||||||
@@ -10,7 +10,15 @@ https://docs.djangoproject.com/en/5.1/howto/deployment/asgi/
|
|||||||
import os
|
import os
|
||||||
|
|
||||||
from django.core.asgi import get_asgi_application
|
from django.core.asgi import get_asgi_application
|
||||||
|
from channels.routing import ProtocolTypeRouter
|
||||||
|
|
||||||
os.environ.setdefault('DJANGO_SETTINGS_MODULE', 'core.settings')
|
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.)
|
||||||
|
}
|
||||||
|
)
|
||||||
@@ -31,6 +31,8 @@ ALLOWED_HOSTS = ['127.0.0.1', 'localhost', '*']
|
|||||||
# Application definition
|
# Application definition
|
||||||
|
|
||||||
INSTALLED_APPS = [
|
INSTALLED_APPS = [
|
||||||
|
'daphne',
|
||||||
|
'chat',
|
||||||
'library.apps.LibraryConfig',
|
'library.apps.LibraryConfig',
|
||||||
'homepage.apps.HomepageConfig',
|
'homepage.apps.HomepageConfig',
|
||||||
'components.apps.ComponentsConfig',
|
'components.apps.ComponentsConfig',
|
||||||
@@ -129,3 +131,6 @@ STATICFILES_DIRS = [BASE_DIR / 'static']
|
|||||||
DEFAULT_AUTO_FIELD = 'django.db.models.BigAutoField'
|
DEFAULT_AUTO_FIELD = 'django.db.models.BigAutoField'
|
||||||
|
|
||||||
LOGIN_URL = '/account/login/'
|
LOGIN_URL = '/account/login/'
|
||||||
|
|
||||||
|
# Daphne
|
||||||
|
ASGI_APPLICATION = "core.asgi.application"
|
||||||
@@ -23,4 +23,5 @@ urlpatterns = [
|
|||||||
path('', include('homepage.urls')),
|
path('', include('homepage.urls')),
|
||||||
path('play/', include('play.urls')),
|
path('play/', include('play.urls')),
|
||||||
path('library/', include('library.urls')),
|
path('library/', include('library.urls')),
|
||||||
|
path('chat/', include('chat.urls')),
|
||||||
]
|
]
|
||||||
|
|||||||
27
core/templates/chat/index.html
Normal file
27
core/templates/chat/index.html
Normal 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>
|
||||||
Reference in New Issue
Block a user