Merge branch 'Chatte' into feat/accounts
This commit is contained in:
commit
d490208ffa
36
frontend/consumers.py
Normal file
36
frontend/consumers.py
Normal file
@ -0,0 +1,36 @@
|
||||
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
|
||||
}))
|
||||
|
6
frontend/routing.py
Normal file
6
frontend/routing.py
Normal file
@ -0,0 +1,6 @@
|
||||
from django.urls import re_path
|
||||
from . import consumers
|
||||
|
||||
websocket_urlpatterns = [
|
||||
re_path(r'ws/socket-server/', consumers.ChatConsumer.as_asgi())
|
||||
]
|
@ -1,37 +1,9 @@
|
||||
body {
|
||||
--nav-width: 200px;
|
||||
margin: 0 0 0 var(--nav-width);
|
||||
margin: 10;
|
||||
font-family: 'Quicksand', sans-serif;
|
||||
font-size: 18px;
|
||||
}
|
||||
|
||||
.nav {
|
||||
position: fixed;
|
||||
top: 0;
|
||||
left: 0;
|
||||
width: var(--nav-width);
|
||||
height: 100vh;
|
||||
background: #222222;
|
||||
}
|
||||
|
||||
.nav__link {
|
||||
display: block;
|
||||
padding: 12px 18px;
|
||||
text-decoration: none;
|
||||
color: #eeeeee;
|
||||
font-weight: 500;
|
||||
}
|
||||
|
||||
.nav__link:hover {
|
||||
background: rgba(255, 255, 255, 0.05);
|
||||
}
|
||||
|
||||
#app {
|
||||
margin: 2em;
|
||||
line-height: 1.5;
|
||||
font-weight: 500;
|
||||
}
|
||||
|
||||
a {
|
||||
color: #009579;
|
||||
}
|
@ -3,6 +3,7 @@ import Dashboard from "./views/Dashboard.js";
|
||||
import Posts from "./views/Posts.js";
|
||||
import PostView from "./views/PostView.js";
|
||||
import Settings from "./views/Settings.js";
|
||||
import Chat from "./views/Chat.js";
|
||||
|
||||
import { Client } from "./api/client.js";
|
||||
import RegisterView from "./views/accounts/RegisterView.js";
|
||||
@ -33,6 +34,7 @@ const router = async () => {
|
||||
{ path: "/settings", view: Settings },
|
||||
{ path: "/login", view: LoginView },
|
||||
{ path: "/register", view: RegisterView },
|
||||
{ path: "/chat", view: Chat },
|
||||
];
|
||||
|
||||
// Test each route for potential match
|
||||
@ -55,6 +57,8 @@ const router = async () => {
|
||||
const view = new match.route.view(getParams(match));
|
||||
|
||||
document.querySelector("#app").innerHTML = await view.getHtml();
|
||||
|
||||
await view.postInit();
|
||||
};
|
||||
|
||||
window.addEventListener("popstate", router);
|
||||
|
@ -3,6 +3,9 @@ export default class {
|
||||
this.params = params;
|
||||
}
|
||||
|
||||
async postInit() {
|
||||
}
|
||||
|
||||
setTitle(title) {
|
||||
document.title = title;
|
||||
}
|
||||
|
51
frontend/static/js/views/Chat.js
Normal file
51
frontend/static/js/views/Chat.js
Normal file
@ -0,0 +1,51 @@
|
||||
import AbstractView from "./AbstractView.js";
|
||||
|
||||
export default class extends AbstractView {
|
||||
constructor(params) {
|
||||
super(params);
|
||||
this.setTitle("Chat");
|
||||
|
||||
let url = `ws://${window.location.host}/ws/socket-server/`
|
||||
|
||||
this.chatSocket = new WebSocket(url)
|
||||
this.chatSocket.onmessage = function(e){
|
||||
let data = JSON.parse(e.data)
|
||||
console.log('Data:', data)
|
||||
if (data.type === 'chat') {
|
||||
let messages = document.getElementById('messages')
|
||||
|
||||
messages.insertAdjacentHTML('beforeend', `
|
||||
<div><p>${data.message}</p></div>
|
||||
`)
|
||||
}
|
||||
|
||||
}
|
||||
}
|
||||
|
||||
async postInit() {
|
||||
let form = document.getElementById('form')
|
||||
form.addEventListener('submit', (e)=> {
|
||||
e.preventDefault()
|
||||
let message = e.target.message.value
|
||||
this.chatSocket.send(JSON.stringify({
|
||||
'message':message
|
||||
}))
|
||||
form.reset()
|
||||
})
|
||||
}
|
||||
|
||||
async getHtml() {
|
||||
return `
|
||||
<h1>Chat</h1>
|
||||
|
||||
<form id="form">
|
||||
<input type="text" name="message" />
|
||||
</form>
|
||||
|
||||
<div id="messages">
|
||||
|
||||
</div>
|
||||
|
||||
`;
|
||||
}
|
||||
}
|
@ -13,6 +13,7 @@
|
||||
<a href="/posts" class="nav__link" data-link>Posts</a>
|
||||
<a href="/settings" class="nav__link" data-link>Settings</a>
|
||||
<a href="/login" class="nav__link" data-link>Login</a>
|
||||
<a href="/chat" class="nav__link" data-link>Chat</a>
|
||||
</nav>
|
||||
<div id="app"></div>
|
||||
<script type="module" src="{% static 'js/index.js' %}"></script>
|
||||
|
@ -2,5 +2,6 @@ from django.shortcuts import render
|
||||
|
||||
# Create your views here.
|
||||
|
||||
|
||||
def index_view(req):
|
||||
return render(req, 'index.html');
|
||||
|
@ -6,3 +6,4 @@ djangorestframework==3.14.0
|
||||
install==1.3.5
|
||||
pytz==2023.3.post1
|
||||
sqlparse==0.4.4
|
||||
channels==3.0.5
|
||||
|
Loading…
Reference in New Issue
Block a user