Compare commits
4 Commits
60-refacto
...
30-chat-ap
| Author | SHA1 | Date | |
|---|---|---|---|
| 9b13749a29 | |||
|
|
b6831616cc | ||
| 889134784a | |||
| ab6e27f80e |
3
.gitignore
vendored
3
.gitignore
vendored
@@ -2,4 +2,5 @@ __pycache__
|
|||||||
.venv
|
.venv
|
||||||
db.sqlite3
|
db.sqlite3
|
||||||
tailwindcss.exe
|
tailwindcss.exe
|
||||||
t-style.css
|
t-style.css
|
||||||
|
dump.rdb
|
||||||
|
|||||||
0
core/chat/__init__.py
Normal file
0
core/chat/__init__.py
Normal file
40
core/chat/consumers.py
Normal file
40
core/chat/consumers.py
Normal file
@@ -0,0 +1,40 @@
|
|||||||
|
import json
|
||||||
|
|
||||||
|
from asgiref.sync import async_to_sync
|
||||||
|
from channels.generic.websocket import WebsocketConsumer
|
||||||
|
|
||||||
|
|
||||||
|
class ChatConsumer(WebsocketConsumer):
|
||||||
|
def connect(self):
|
||||||
|
self.room_name = self.scope["url_route"]["kwargs"]["room_name"]
|
||||||
|
self.room_group_name = f"chat_{self.room_name}"
|
||||||
|
|
||||||
|
# Join room group
|
||||||
|
async_to_sync(self.channel_layer.group_add)(
|
||||||
|
self.room_group_name, self.channel_name
|
||||||
|
)
|
||||||
|
|
||||||
|
self.accept()
|
||||||
|
|
||||||
|
def disconnect(self, close_code):
|
||||||
|
# Leave room group
|
||||||
|
async_to_sync(self.channel_layer.group_discard)(
|
||||||
|
self.room_group_name, self.channel_name
|
||||||
|
)
|
||||||
|
|
||||||
|
# Receive message from WebSocket
|
||||||
|
def receive(self, text_data):
|
||||||
|
text_data_json = json.loads(text_data)
|
||||||
|
message = text_data_json["message"]
|
||||||
|
|
||||||
|
# Send message to room group
|
||||||
|
async_to_sync(self.channel_layer.group_send)(
|
||||||
|
self.room_group_name, {"type": "chat.message", "message": message}
|
||||||
|
)
|
||||||
|
|
||||||
|
# Receive message from room group
|
||||||
|
def chat_message(self, event):
|
||||||
|
message = event["message"]
|
||||||
|
|
||||||
|
# Send message to WebSocket
|
||||||
|
self.send(text_data=json.dumps({"message": message}))
|
||||||
0
core/chat/migrations/__init__.py
Normal file
0
core/chat/migrations/__init__.py
Normal file
8
core/chat/routing.py
Normal file
8
core/chat/routing.py
Normal file
@@ -0,0 +1,8 @@
|
|||||||
|
# chat/routing.py
|
||||||
|
from django.urls import re_path
|
||||||
|
|
||||||
|
from . import consumers
|
||||||
|
|
||||||
|
websocket_urlpatterns = [
|
||||||
|
re_path(r"ws/chat/(?P<room_name>\w+)/$", consumers.ChatConsumer.as_asgi()),
|
||||||
|
]
|
||||||
9
core/chat/urls.py
Normal file
9
core/chat/urls.py
Normal file
@@ -0,0 +1,9 @@
|
|||||||
|
# chat/urls.py
|
||||||
|
from django.urls import path
|
||||||
|
|
||||||
|
from . import views
|
||||||
|
|
||||||
|
urlpatterns = [
|
||||||
|
path("", views.index, name="index"),
|
||||||
|
path("<str:room_name>/", views.room, name="room"),
|
||||||
|
]
|
||||||
8
core/chat/views.py
Normal file
8
core/chat/views.py
Normal file
@@ -0,0 +1,8 @@
|
|||||||
|
# chat/views.py
|
||||||
|
from django.shortcuts import render
|
||||||
|
|
||||||
|
def index(request):
|
||||||
|
return render(request, "chat/index.html")
|
||||||
|
|
||||||
|
def room(request, room_name):
|
||||||
|
return render(request, "chat/room.html", {"room_name": room_name})
|
||||||
@@ -9,8 +9,26 @@ https://docs.djangoproject.com/en/5.1/howto/deployment/asgi/
|
|||||||
|
|
||||||
import os
|
import os
|
||||||
|
|
||||||
|
from channels.auth import AuthMiddlewareStack
|
||||||
|
from channels.routing import ProtocolTypeRouter, URLRouter
|
||||||
|
from channels.security.websocket import AllowedHostsOriginValidator
|
||||||
from django.core.asgi import get_asgi_application
|
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()
|
||||||
|
|
||||||
|
django_asgi_app = get_asgi_application()
|
||||||
|
|
||||||
|
from chat.routing import websocket_urlpatterns
|
||||||
|
|
||||||
|
application = ProtocolTypeRouter(
|
||||||
|
{
|
||||||
|
"http": django_asgi_app,
|
||||||
|
"websocket": AllowedHostsOriginValidator(
|
||||||
|
AuthMiddlewareStack(URLRouter(websocket_urlpatterns))
|
||||||
|
),
|
||||||
|
}
|
||||||
|
)
|
||||||
@@ -25,12 +25,14 @@ SECRET_KEY = 'django-insecure-#+s307$rxkts=8@+_^i)8)n1b4*y4za1xz!mvv(h@!+n9!mp9)
|
|||||||
# SECURITY WARNING: don't run with debug turned on in production!
|
# SECURITY WARNING: don't run with debug turned on in production!
|
||||||
DEBUG = True
|
DEBUG = True
|
||||||
|
|
||||||
ALLOWED_HOSTS = ['127.0.0.1', 'localhost', '*']
|
ALLOWED_HOSTS = ['127.0.0.1', 'localhost', '*', '172.22.10.111']
|
||||||
|
|
||||||
|
|
||||||
# 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',
|
||||||
@@ -128,4 +130,18 @@ 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"
|
||||||
|
|
||||||
|
# Channels
|
||||||
|
ASGI_APPLICATION = "core.asgi.application"
|
||||||
|
CHANNEL_LAYERS = {
|
||||||
|
"default": {
|
||||||
|
"BACKEND": "channels_redis.core.RedisChannelLayer",
|
||||||
|
"CONFIG": {
|
||||||
|
"hosts": [("127.0.0.1", 8000)],
|
||||||
|
},
|
||||||
|
},
|
||||||
|
}
|
||||||
@@ -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>
|
||||||
50
core/templates/chat/room.html
Normal file
50
core/templates/chat/room.html
Normal file
@@ -0,0 +1,50 @@
|
|||||||
|
<!-- chat/templates/chat/room.html -->
|
||||||
|
<!DOCTYPE html>
|
||||||
|
<html>
|
||||||
|
<head>
|
||||||
|
<meta charset="utf-8"/>
|
||||||
|
<title>Chat Room</title>
|
||||||
|
</head>
|
||||||
|
<body>
|
||||||
|
<textarea id="chat-log" cols="100" rows="20"></textarea><br>
|
||||||
|
<input id="chat-message-input" type="text" size="100"><br>
|
||||||
|
<input id="chat-message-submit" type="button" value="Send">
|
||||||
|
{{ room_name|json_script:"room-name" }}
|
||||||
|
<script>
|
||||||
|
const roomName = JSON.parse(document.getElementById('room-name').textContent);
|
||||||
|
|
||||||
|
const chatSocket = new WebSocket(
|
||||||
|
'ws://'
|
||||||
|
+ window.location.host
|
||||||
|
+ '/ws/chat/'
|
||||||
|
+ roomName
|
||||||
|
+ '/'
|
||||||
|
);
|
||||||
|
|
||||||
|
chatSocket.onmessage = function(e) {
|
||||||
|
const data = JSON.parse(e.data);
|
||||||
|
document.querySelector('#chat-log').value += (data.message + '\n');
|
||||||
|
};
|
||||||
|
|
||||||
|
chatSocket.onclose = function(e) {
|
||||||
|
console.error('Chat socket closed unexpectedly');
|
||||||
|
};
|
||||||
|
|
||||||
|
document.querySelector('#chat-message-input').focus();
|
||||||
|
document.querySelector('#chat-message-input').onkeyup = function(e) {
|
||||||
|
if (e.key === 'Enter') { // enter, return
|
||||||
|
document.querySelector('#chat-message-submit').click();
|
||||||
|
}
|
||||||
|
};
|
||||||
|
|
||||||
|
document.querySelector('#chat-message-submit').onclick = function(e) {
|
||||||
|
const messageInputDom = document.querySelector('#chat-message-input');
|
||||||
|
const message = messageInputDom.value;
|
||||||
|
chatSocket.send(JSON.stringify({
|
||||||
|
'message': message
|
||||||
|
}));
|
||||||
|
messageInputDom.value = '';
|
||||||
|
};
|
||||||
|
</script>
|
||||||
|
</body>
|
||||||
|
</html>
|
||||||
@@ -1,3 +1,4 @@
|
|||||||
django~=5.1.4
|
django~=5.1.4
|
||||||
channels~=4.2.0
|
channels~=4.2.0
|
||||||
daphne~=4.1.2
|
daphne~=4.1.2
|
||||||
|
channels_redis
|
||||||
Reference in New Issue
Block a user