Compare commits
7 Commits
7e34f883aa
...
9947ea37e2
Author | SHA1 | Date | |
---|---|---|---|
9947ea37e2 | |||
|
b12c03074a | ||
|
8bce7d33ca | ||
|
ffbfe2ddd0 | ||
|
9c59401cf2 | ||
|
2a468bcb82 | ||
|
7b6a8ba57b |
0
chat/__init__.py
Normal file
0
chat/__init__.py
Normal file
3
chat/admin.py
Normal file
3
chat/admin.py
Normal file
@ -0,0 +1,3 @@
|
||||
from django.contrib import admin
|
||||
|
||||
# Register your models here.
|
6
chat/apps.py
Normal file
6
chat/apps.py
Normal file
@ -0,0 +1,6 @@
|
||||
from django.apps import AppConfig
|
||||
|
||||
|
||||
class ChatConfig(AppConfig):
|
||||
default_auto_field = 'django.db.models.BigAutoField'
|
||||
name = 'chat'
|
@ -31,6 +31,6 @@ class ChatConsumer(WebsocketConsumer):
|
||||
|
||||
self.send(text_data=json.dumps({
|
||||
'type':'chat',
|
||||
'username':self.scope["user"].username,
|
||||
'message':message
|
||||
}))
|
||||
|
3
chat/models.py
Normal file
3
chat/models.py
Normal file
@ -0,0 +1,3 @@
|
||||
from django.db import models
|
||||
|
||||
# Create your models here.
|
3
chat/tests.py
Normal file
3
chat/tests.py
Normal file
@ -0,0 +1,3 @@
|
||||
from django.test import TestCase
|
||||
|
||||
# Create your tests here.
|
3
chat/views.py
Normal file
3
chat/views.py
Normal file
@ -0,0 +1,3 @@
|
||||
from django.shortcuts import render
|
||||
|
||||
# Create your views here.
|
@ -12,6 +12,8 @@ import { Client } from "./api/client.js";
|
||||
|
||||
let client = new Client(location.protocol + "//" + location.host)
|
||||
|
||||
let lastView = undefined
|
||||
|
||||
const pathToRegex = path => new RegExp("^" + path.replace(/\//g, "\\/").replace(/:\w+/g, "(.+)") + "$");
|
||||
|
||||
const getParams = match => {
|
||||
@ -57,7 +59,12 @@ const router = async (uri = "") => {
|
||||
result: [uri]
|
||||
};
|
||||
}
|
||||
|
||||
if (lastView !== undefined)
|
||||
await lastView.leavePage();
|
||||
|
||||
const view = new match.route.view(getParams(match));
|
||||
lastView = view;
|
||||
|
||||
let content = await view.getHtml();
|
||||
if (content == null)
|
||||
|
@ -6,6 +6,9 @@ export default class {
|
||||
async postInit() {
|
||||
}
|
||||
|
||||
async leavePage() {
|
||||
}
|
||||
|
||||
setTitle(title) {
|
||||
document.title = title;
|
||||
}
|
||||
|
@ -14,8 +14,9 @@ export default class extends AbstractView {
|
||||
if (data.type === 'chat') {
|
||||
let messages = document.getElementById('messages')
|
||||
|
||||
let username = data.username === null || data.username.length <= 0 ? "NoName" : data.username;
|
||||
messages.insertAdjacentHTML('beforeend', `
|
||||
<div><p>${data.message}</p></div>
|
||||
<div><p>${username}: ${data.message}</p></div>
|
||||
`)
|
||||
}
|
||||
|
||||
@ -32,6 +33,11 @@ export default class extends AbstractView {
|
||||
}))
|
||||
form.reset()
|
||||
})
|
||||
|
||||
}
|
||||
|
||||
async leavePage() {
|
||||
this.chatSocket.close();
|
||||
}
|
||||
|
||||
async getHtml() {
|
||||
|
@ -6,4 +6,5 @@ djangorestframework==3.14.0
|
||||
install==1.3.5
|
||||
pytz==2023.3.post1
|
||||
sqlparse==0.4.4
|
||||
channels==3.0.5
|
||||
channels==4.0.0
|
||||
daphne
|
||||
|
@ -8,9 +8,20 @@ https://docs.djangoproject.com/en/4.2/howto/deployment/asgi/
|
||||
"""
|
||||
|
||||
import os
|
||||
from channels.routing import ProtocolTypeRouter, URLRouter
|
||||
from channels.auth import AuthMiddlewareStack
|
||||
import chat.routing
|
||||
|
||||
from django.core.asgi import get_asgi_application
|
||||
|
||||
os.environ.setdefault('DJANGO_SETTINGS_MODULE', 'trancendence.settings')
|
||||
|
||||
application = get_asgi_application()
|
||||
application = ProtocolTypeRouter({
|
||||
'http':get_asgi_application(),
|
||||
'websocket':AuthMiddlewareStack(
|
||||
URLRouter(
|
||||
chat.routing.websocket_urlpatterns
|
||||
)
|
||||
)
|
||||
})
|
||||
|
||||
|
@ -38,6 +38,9 @@ CORS_ORIGIN_WHITELIST = (
|
||||
# Application definition
|
||||
|
||||
INSTALLED_APPS = [
|
||||
'channels',
|
||||
'daphne',
|
||||
|
||||
'accounts.apps.AccountsConfig',
|
||||
'profiles.apps.ProfilesConfig',
|
||||
'frontend.apps.FrontendConfig',
|
||||
@ -52,6 +55,14 @@ INSTALLED_APPS = [
|
||||
'django.contrib.staticfiles',
|
||||
]
|
||||
|
||||
ASGI_APPLICATION = 'trancendence.asgi.application'
|
||||
|
||||
CHANNEL_LAYERS = {
|
||||
'default' :{
|
||||
'BACKEND':'channels.layers.InMemoryChannelLayer'
|
||||
}
|
||||
}
|
||||
|
||||
MIDDLEWARE = [
|
||||
'corsheaders.middleware.CorsMiddleware',
|
||||
'django.middleware.common.CommonMiddleware',
|
||||
|
Loading…
Reference in New Issue
Block a user