에라모르겠다(‘◇’)?
[python] django channels - web socket 연동 (1) 본문
일단 가장 중요한
https://channels.readthedocs.io/en/stable/tutorial/part_1.html
Tutorial Part 1: Basic Setup — Channels 4.0.0 documentation
So far we’ve just created a regular Django app; we haven’t used the Channels library at all. Now it’s time to integrate Channels. Let’s start by creating a routing configuration for Channels. A Channels routing configuration is an ASGI application
channels.readthedocs.io
channels 자습서를 참고해서 구현 할 예정
가상환경, 프로젝트 생성하는 부분은 생략
- 프로젝트 생성 (config)
$ django-admin startproject config .
- 채팅 앱 생성
$ python3 manage.py startapp chat
자습서에서 지우라는 .py 파일들 지우고
__init__.py와 views.py만 남김
- config/setting.py
생성한 chat 앱 추가해줌 , daphne는 밑에서 추가하는 작업 할 예정
- templates/chat/index.html 추가
chat 앱에 templates/chat 폴더 아래 index.html 파일을 추가해줌
- index.html
<!-- chat/templates/chat/index.html -->
<!DOCTYPE html>
<html>
<head>
<meta charset="utf-8"/>
<title>Chat Rooms</title>
</head>
<body>
What chat room would you like to enter?<br>
<input id="room-name-input" type="text" size="100"><br>
<input id="room-name-submit" type="button" value="Enter">
<script>
document.querySelector('#room-name-input').focus();
document.querySelector('#room-name-input').onkeyup = function(e) {
if (e.keyCode === 13) { // enter, return
document.querySelector('#room-name-submit').click();
}
};
document.querySelector('#room-name-submit').onclick = function(e) {
var roomName = document.querySelector('#room-name-input').value;
window.location.pathname = '/chat/' + roomName + '/';
};
</script>
</body>
</html>
자습서에 있는 index.html 코드 그대로 복붙해줌
- chat / view.py 에 해당 코드 입력
from django.shortcuts import render
# Create your views here.
def index(request):
return render(request, "chat/index.html")
- chat / urls.py 파일 생성 후 해당 코드 입력
# chat/urls.py
from django.urls import path
from . import views
urlpatterns = [
path("", views.index, name="index"),
]
- config / urls.py 설정
- daphne 설치
$ pip install daphne
- config/setting.py
INSTALLED_APPS의 맨 위에 daphne 넣어줌 꼭 !
- config / asgi.py
$ pip install channels
channels 설치 해주고 해당 코드 넣어줌
import os
from channels.routing import ProtocolTypeRouter
from django.core.asgi import get_asgi_application
os.environ.setdefault('DJANGO_SETTINGS_MODULE', 'config.settings') #프로젝트명.settings
# Initialize Django ASGI application early to ensure the AppRegistry
# is populated before importing code that may import ORM models.
django_asgi_app = get_asgi_application()
application = ProtocolTypeRouter({
"http": django_asgi_app,
# Just HTTP for now. (We can add other protocols later.)
})
- config / setting.py
ASGI_APPLICATION = "config.asgi.application" #프로젝트명.asgi.application
제일 아래에 추가해줌
추가 한 후 runserver 실행하면
Starting ASGI/Daphne 어쩌구 하면서 실행됨
- 127.0.0.1:8000/chat/ 주소를 입력하면 아까 만든 index.html 파일이 나타남
'Language > Python' 카테고리의 다른 글
[Python] APScheduler (0) | 2023.06.07 |
---|---|
[python] django channels - web socket 연동 (2) (0) | 2023.05.30 |
[python] 인터프리터 설정 (0) | 2023.05.30 |
[Django] 스케줄러 등록 (0) | 2023.05.04 |
[Django] pandas모듈을 이용한 json -> csv 파일 생성 (0) | 2023.05.03 |