From 6e624dad34a331a0be4faa39da57a1eae8e516e7 Mon Sep 17 00:00:00 2001 From: Xamora Date: Wed, 22 Nov 2023 15:50:42 +0100 Subject: [PATCH] Hopital en PLS --- frontend/consumers.py | 15 +++++++++++ frontend/rounting.py | 6 +++++ frontend/static/css/index.css | 32 ++--------------------- frontend/static/js/.index.js.swp | Bin 0 -> 12288 bytes frontend/static/js/index.js | 6 +++-- frontend/static/js/views/AbstractView.js | 2 +- frontend/static/js/views/Dashboard.js | 2 +- frontend/static/js/views/GeneralChat.js | 22 ++++++++++++++++ frontend/templates/index.html | 1 + trancendence/asgi.py | 12 ++++++++- trancendence/settings.py | 5 ++++ 11 files changed, 68 insertions(+), 35 deletions(-) create mode 100644 frontend/consumers.py create mode 100644 frontend/rounting.py create mode 100644 frontend/static/js/.index.js.swp create mode 100644 frontend/static/js/views/GeneralChat.js diff --git a/frontend/consumers.py b/frontend/consumers.py new file mode 100644 index 0000000..307fdb9 --- /dev/null +++ b/frontend/consumers.py @@ -0,0 +1,15 @@ +import json +from channels.generic.websocket import WebsocketConsumer + +class ChatConsumer(WebsocketConsumer): + def connect(self): + self.accept() + + self.send(text_data=json.dumps({ + 'type':'connection_established', + 'message':'Your are now connected', + })) + + def receive(): + + def disconnect(): diff --git a/frontend/rounting.py b/frontend/rounting.py new file mode 100644 index 0000000..5df8609 --- /dev/null +++ b/frontend/rounting.py @@ -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()) +] diff --git a/frontend/static/css/index.css b/frontend/static/css/index.css index 9416823..db15e14 100644 --- a/frontend/static/css/index.css +++ b/frontend/static/css/index.css @@ -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; -} \ No newline at end of file +} diff --git a/frontend/static/js/.index.js.swp b/frontend/static/js/.index.js.swp new file mode 100644 index 0000000000000000000000000000000000000000..5a97216833d8ec56b4853e810b8d54f11e1cd682 GIT binary patch literal 12288 zcmeHNL2ukd6rLairBEm>Cr%TWgRN}sB<+O_L5h;7ElSjigi2(yL9^c3T_;}K%#5>H zRRJV8AdUzwh&vaA03pGp{{T3I0|x|xBL_~L5q#tEdb1?7S3qOwlh<#)dGp>k?~N2? zTC=xrtkY-xS%T+0AzKga@C$Fhd-lv9PZBQp@N$bQJ#?>U{@NA)>O4!jX&&=F_dDWr z_G=@Dff8(=6|f5YpA=Bp6nPy9 zJy0gKRKBPF_AWo|%PL?MunJfOtO8a6tAJI&Dqt0`3RnfK0#4aH-JsR0j7bk9w+2W-~%87o&qicKb<4wF7O5L8Sp9a9uNaNKm*tU z76BI^!1s?4@)2+VECbI03&45c$43eI8u%RO11(?+co`tTpJxgA4fp}L3%n1!1H1*i z3A_N@03HOU00MmX2q8znhd>0>fv17rk^8T}FTl^h5uo+>2C$D+z$#!BunJfOtOEab z1rEIhLh*<+S6sOGDn3VMJ#<)D^e_D+Y(MHU#Wz!z`-+J!SAI|MPU**xDCK@8_&!h6 z3h%HyR@(DuEb7ql+&)ubk4x8Sw3%X;<0#o{I-W=G6!dl)=6!haTWNdfvvzw``&^48 zZaN}dCyb+T&P7oW$Jihh#0a&vvTxFD(4Qr6~eOmaOliwtm4G98@aK1(xwo05ZU z1k(B(noO#-Ud_1}Zt$3gDn&?_Se7}SA0-Iz`sVr?U8HQlB1QKjKJX#eb=8k6p~Ezh zW(d554``u>U##Zq87^krVuJOhTMT*C7FBwd0gdDpTte_VZNzDao1kgpXH4}H*5}P? zislw;)XhsaW}CHYv{(r7m_5+jHEq_0r69#znLt6xQtX9_SiEj_((gb`chahLp0xNT zN;qznk|_O zmrG^5TKC{jaOLU5inGFG zuaz><9`THnMmGr9Ajb>>1}29|IPSx8!wY_K7HMsJX!cYL{VbQg4c&~cu9sR-H8q+G zT{_0?_-KM_43ynndOg&gqaEs}-0;R8A3_Wk=Ug_TX1O)x;LX-9S{iq=)Bb9rM1(^M zbhH2ZSB*xa14O$tF=z%>UlM{1i>an-%ZAq5ZK`!Cj@_W{ZVzU>pzC?2sEWqbQJdyy z%r(o9jc*etychLec91V|1afz-QlUC8M#NGE58g;g}0~~CXwT|EmyX((*j)z&!GY-NaQ9nzCny6nbB6a*g*PRTGRW@#? lI}4*3RsBxVN9GA`RlAeik@*By(vw6v!5UZZB(t&`_Fvg#id_Hz literal 0 HcmV?d00001 diff --git a/frontend/static/js/index.js b/frontend/static/js/index.js index f63c27c..f93a21e 100644 --- a/frontend/static/js/index.js +++ b/frontend/static/js/index.js @@ -2,6 +2,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 GeneralChat from "./views/GeneralChat.js"; const pathToRegex = path => new RegExp("^" + path.replace(/\//g, "\\/").replace(/:\w+/g, "(.+)") + "$"); @@ -24,7 +25,8 @@ const router = async () => { { path: "/", view: Dashboard }, { path: "/posts", view: Posts }, { path: "/posts/:id", view: PostView }, - { path: "/settings", view: Settings } + { path: "/settings", view: Settings }, + { path: "/generalchat", view: GeneralChat }, ]; // Test each route for potential match @@ -60,4 +62,4 @@ document.addEventListener("DOMContentLoaded", () => { }); router(); -}); \ No newline at end of file +}); diff --git a/frontend/static/js/views/AbstractView.js b/frontend/static/js/views/AbstractView.js index 18bf83f..4b0f939 100644 --- a/frontend/static/js/views/AbstractView.js +++ b/frontend/static/js/views/AbstractView.js @@ -10,4 +10,4 @@ export default class { async getHtml() { return ""; } -} \ No newline at end of file +} diff --git a/frontend/static/js/views/Dashboard.js b/frontend/static/js/views/Dashboard.js index e2f4409..eb3c2f3 100644 --- a/frontend/static/js/views/Dashboard.js +++ b/frontend/static/js/views/Dashboard.js @@ -17,4 +17,4 @@ export default class extends AbstractView {

`; } -} \ No newline at end of file +} diff --git a/frontend/static/js/views/GeneralChat.js b/frontend/static/js/views/GeneralChat.js new file mode 100644 index 0000000..fc0af98 --- /dev/null +++ b/frontend/static/js/views/GeneralChat.js @@ -0,0 +1,22 @@ +import AbstractView from "./AbstractView.js"; + +export default class extends AbstractView { + constructor(params) { + super(params); + this.setTitle("General Chat"); + let url = `ws://${window.location.host}/ws/socket-server/` + + const chatSocket = new WebSocket(url) + chatSocket.onmessage = function(e) { + let data = JSON.parse(e.data) + console.log('Data:', data) + } + } + + async getHtml() { + return ` +

General Chat

+ + `; + } +} diff --git a/frontend/templates/index.html b/frontend/templates/index.html index 6fcaec3..d511259 100644 --- a/frontend/templates/index.html +++ b/frontend/templates/index.html @@ -11,6 +11,7 @@
diff --git a/trancendence/asgi.py b/trancendence/asgi.py index eaaad8a..8856b08 100644 --- a/trancendence/asgi.py +++ b/trancendence/asgi.py @@ -10,7 +10,17 @@ https://docs.djangoproject.com/en/4.2/howto/deployment/asgi/ import os from django.core.asgi import get_asgi_application +from channels.routing import ProtocolTypeRouter, URLRouter +from channels.auth import AuthMiddlewareStack +import chat.routing 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 + ) + ), +}) diff --git a/trancendence/settings.py b/trancendence/settings.py index a8d3760..21d7952 100644 --- a/trancendence/settings.py +++ b/trancendence/settings.py @@ -38,6 +38,9 @@ CORS_ORIGIN_WHITELIST = ( # Application definition INSTALLED_APPS = [ + + 'channels', + 'accounts.apps.AccountsConfig', 'profiles.apps.ProfilesConfig', 'frontend.apps.FrontendConfig', @@ -52,6 +55,8 @@ INSTALLED_APPS = [ 'django.contrib.staticfiles', ] +ASGI_APPLICATION = 'trancendence.asgi.application' + MIDDLEWARE = [ 'corsheaders.middleware.CorsMiddleware', 'django.middleware.common.CommonMiddleware',