WIP: Draft: Resolve "Chat App Websocket Tutorial" #174
18
core/chat/consumers.py
Normal file
18
core/chat/consumers.py
Normal file
@@ -0,0 +1,18 @@
|
|||||||
|
# chat/consumers.py
|
||||||
|
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}))
|
||||||
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()),
|
||||||
|
]
|
||||||
@@ -5,4 +5,5 @@ from . import views
|
|||||||
|
|
||||||
urlpatterns = [
|
urlpatterns = [
|
||||||
path("", views.index, name="index"),
|
path("", views.index, name="index"),
|
||||||
|
path("<str:room_name>/", views.room, name="room"),
|
||||||
]
|
]
|
||||||
@@ -2,4 +2,7 @@
|
|||||||
from django.shortcuts import render
|
from django.shortcuts import render
|
||||||
|
|
||||||
def index(request):
|
def index(request):
|
||||||
return render(request, "chat/index.html")
|
return render(request, "chat/index.html")
|
||||||
|
|
||||||
|
def room(request, room_name):
|
||||||
|
return render(request, "chat/room.html", {"room_name": room_name})
|
||||||
|
|||||||
@@ -9,6 +9,10 @@ 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
|
from channels.routing import ProtocolTypeRouter
|
||||||
|
|
||||||
@@ -16,9 +20,15 @@ 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(
|
application = ProtocolTypeRouter(
|
||||||
{
|
{
|
||||||
"http": get_asgi_application(),
|
"http": django_asgi_app,
|
||||||
# Just HTTP for now. (We can add other protocols later.)
|
"websocket": AllowedHostsOriginValidator(
|
||||||
|
AuthMiddlewareStack(URLRouter(websocket_urlpatterns))
|
||||||
|
),
|
||||||
}
|
}
|
||||||
)
|
)
|
||||||
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>
|
||||||
Reference in New Issue
Block a user