Merge branch 'Chatte' into feat/accounts

This commit is contained in:
2023-11-29 18:36:08 +01:00
14 changed files with 62 additions and 5 deletions

View File

@ -1,36 +0,0 @@
import json
from channels.generic.websocket import WebsocketConsumer
from asgiref.sync import async_to_sync
class ChatConsumer(WebsocketConsumer):
def connect(self):
self.room_group_name = 'test'
async_to_sync(self.channel_layer.group_add)(
self.room_group_name,
self.channel_name
)
self.accept()
def receive(self, text_data):
text_data_json = json.loads(text_data)
message = text_data_json['message']
async_to_sync(self.channel_layer.group_send)(
self.room_group_name,
{
'type':'chat_message',
'message':message
}
)
def chat_message(self, event):
message = event['message']
self.send(text_data=json.dumps({
'type':'chat',
'message':message
}))

View File

@ -1,6 +0,0 @@
from django.urls import re_path
from . import consumers
websocket_urlpatterns = [
re_path(r'ws/socket-server/', consumers.ChatConsumer.as_asgi())
]

View File

@ -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)
@ -81,4 +88,4 @@ document.addEventListener("DOMContentLoaded", () => {
router(location.pathname);
});
export { client, navigateTo }
export { client, navigateTo }

View File

@ -6,6 +6,9 @@ export default class {
async postInit() {
}
async leavePage() {
}
setTitle(title) {
document.title = title;
}

View File

@ -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() {