Commit 3853fe01 authored by Aymeric Obled's avatar Aymeric Obled

add websocket

parent 55df225e
......@@ -10,6 +10,8 @@
<option value="E501" />
<option value="W29" />
<option value="E501" />
<option value="W29" />
<option value="E501" />
</list>
</option>
</inspection_tool>
......
This diff is collapsed.
"""
ASGI entrypoint file for default channel layer.
Points to the channel layer configured as "default" so you can point
ASGI applications at "multichat.asgi:channel_layer" as their channel layer.
"""
import os
from channels.asgi import get_channel_layer
os.environ.setdefault("DJANGO_SETTINGS_MODULE", "pyprevention.settings")
channel_layer = get_channel_layer()
from channels.generic.websocket import WebsocketConsumer
class ChatConsumer(WebsocketConsumer):
def connect(self):
print(self)
self.accept()
self.send(text_data="### Welcome ! You're now connected to the best ws ever ! ###")
def receive(self, text_data):
print(text_data)
self.send(text_data="Voici les données reçues : " + text_data)
def disconnect(self, message):
self.send(text_data="Bye, Bye !")
pass
from channels.routing import ProtocolTypeRouter, URLRouter
from channels.auth import AuthMiddlewareStack
from django.conf.urls import url
from . import consumers
websocket_urlpatterns = [
url(r'^ws/chat$', consumers.ChatConsumer),
]
application = ProtocolTypeRouter({
'websocket': AuthMiddlewareStack(
URLRouter(
websocket_urlpatterns
)
),
})
......@@ -12,6 +12,7 @@ https://docs.djangoproject.com/en/2.1/ref/settings/
import os
# Build paths inside the project like this: os.path.join(BASE_DIR, ...)
BASE_DIR = os.path.dirname(os.path.dirname(os.path.abspath(__file__)))
......@@ -39,6 +40,7 @@ INSTALLED_APPS = [
'django.contrib.staticfiles',
'django_extensions',
'dashboard',
'channels',
]
MIDDLEWARE = [
......@@ -70,6 +72,7 @@ TEMPLATES = [
]
WSGI_APPLICATION = 'pyprevention.wsgi.application'
ASGI_APPLICATION = 'pyprevention.routing.application'
# Database
......@@ -120,3 +123,14 @@ USE_TZ = True
# https://docs.djangoproject.com/en/2.1/howto/static-files/
STATIC_URL = '/static/'
ASGI_APPLICATION = "pyprevention.routing.application"
CHANNEL_LAYERS = {
"default": {
"BACKEND": "channels_redis.core.RedisChannelLayer",
"CONFIG": {
"hosts": ['redis://localhost:6379/4']
}
},
}
Markdown is supported
0% or
You are about to add 0 people to the discussion. Proceed with caution.
Finish editing this message first!
Please register or to comment