salut j’ai suivi le didacticiel en question mais cela ne fonctionne toujours pas. si vous pouviez m’aider
mon asgi.py
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
os.environ.setdefault('DJANGO_SETTINGS_MODULE', 'messenger.settings')
# Initialize Django ASGI application early to ensure the AppRegistry
# is populated before importing code that may import ORM models.
django_asgi_app = get_asgi_application()
from messenger_app.routing import websocket_urlpatterns
application = ProtocolTypeRouter({
"http": django_asgi_app,
"websocket": AllowedHostsOriginValidator(
AuthMiddlewareStack(URLRouter(websocket_urlpatterns))
),
# Just HTTP for now. (We can add other protocols later.)
})
mon settings.py
INSTALLED_APPS = [
"daphne",
'allauth_ui',
'django.contrib.admin',
'django.contrib.auth',
'django.contrib.contenttypes',
'django.contrib.sessions',
'django.contrib.messages',
'django.contrib.staticfiles',
'messenger_app',
#django allauth
'allauth',
'allauth.account',
'allauth.socialaccount',
'allauth.socialaccount.providers.google', # Par exemple, pour Google OAuth2
"widget_tweaks",
#extension
"channels",
]
# Configuration du backend de Channels
ASGI_APPLICATION = 'messenger.asgi.application'
AUTH_USER_MODEL = 'messenger_app.CustomUser'
l’urls.py:
from django.contrib import admin
from django.urls import path, include
from django.conf import settings
from django.conf.urls.static import static
urlpatterns = [
path('admin/', admin.site.urls),
path('messenger_app/', include('messenger_app.urls')),
#commande pour allauth
path('accounts/', include('allauth.urls')),
]
if settings.DEBUG:
urlpatterns +=static(settings.MEDIA_URL,
document_root=settings.MEDIA_ROOT)
# /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}))
# /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()),
]
ma views.py
def index(request):
return render(request, "chat/index.html")
def chat_group(request):
return render(request, "chat/chat_group.html")
def chat_direct(request, room_name):
return render(request, "chat/chat_direct.html", {'room_name':room_name})
mon urls.py de l’application
from django.urls import path
from .import views
urlpatterns = [
path("", views.index, name = "index"),
path("chat_group", views.chat_group, name="chat_group"),
path('chat_direct/<str:room_name>/', views.chat_direct, name='chat_direct'),
path("chat_empty", views.chat_empty, name="chat_empty"),
path("lockscreen", views.lockscreen, name="lockscreen"),
path("password_reset", views.password_reset, name="password_reset"),
path("signup", views.signup, name="signup"),
path("signin", views.signin, name="signin"),
path('conversation/<int:pk>/', views.conversation_detail, name='conversation_detail'),
#path send_mail
path("envoyer_email", views.envoyer_email, name="envoyer_email"),
]
mon chat_direct.html
<form class="chat-form rounded-pill bg-dark" data-emoji-form="">
<div class="row align-items-center gx-0">
<div class="col-auto">
<a href="#" class="btn btn-icon btn-link text-body rounded-circle" id="dz-btn">
<svg xmlns="http://www.w3.org/2000/svg" width="24" height="24" viewBox="0 0 24 24" fill="none" stroke="currentColor" stroke-width="2" stroke-linecap="round" stroke-linejoin="round" class="feather feather-paperclip">
<path d="M21.44 11.05l-9.19 9.19a6 6 0 0 1-8.49-8.49l9.19-9.19a4 4 0 0 1 5.66 5.66l-9.2 9.19a2 2 0 0 1-2.83-2.83l8.49-8.48"></path>
</svg>
</a>
</div>
<div class="col">
<div class="input-group">
<textarea id="chat-message-input" class="form-control px-0" placeholder="Type your message..." rows="1" data-emoji-input="" data-autosize="true"></textarea>
<a href="#" class="input-group-text text-body pe-0" data-emoji-btn="">
<span class="icon icon-lg">
<svg xmlns="http://www.w3.org/2000/svg" width="24" height="24" viewBox="0 0 24 24" fill="none" stroke="currentColor" stroke-width="2" stroke-linecap="round" stroke-linejoin="round" class="feather feather-smile">
<circle cx="12" cy="12" r="10"></circle>
<path d="M8 14s1.5 2 4 2 4-2 4-2"></path>
<line x1="9" y1="9" x2="9.01" y2="9"></line>
<line x1="15" y1="9" x2="15.01" y2="9"></line>
</svg>
</span>
</a>
</div>
</div>
<div class="col-auto">
<button id="chat-message-submit" class="btn btn-icon btn-primary rounded-circle ms-5">
{{ room_name|json_script:"room-name" }}
<svg xmlns="http://www.w3.org/2000/svg" width="24" height="24" viewBox="0 0 24 24" fill="none" stroke="currentColor" stroke-width="2" stroke-linecap="round" stroke-linejoin="round" class="feather feather-send">
<line x1="22" y1="2" x2="11" y2="13"></line>
<polygon points="22 2 15 22 11 13 2 9 22 2"></polygon>
</svg>
</button>
</div>
</div>
</form>
<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>```