장고 실시간 채팅(Channels와 redis server 이용)
둘러보기로 가기
검색하러 가기
1 개요
https://victorydntmd.tistory.com/261를 참고하였다.
장고는 기본적으로 동기식으로, 채팅을 위해선 비동기식 처리가 필요하다.
이를 위해 Django Channels가 마련되어 있다.
ASGI(Async Server Gateway Interface)프로토콜은 WSGI를 계승하여 이와 잘 호환되도록 설계되어 있다.
ASGI는 비동기 요청인 웹 소켓을 처리하는 이벤트로 connect, send, receive, disconnect가 있다.
2 기초준비
2.1 채팅어플 만들기
패키지설치부터 진행하기엔 중간점검이 어려워 틀 만들기를 먼저 수행한다.
과정 | 설명 | 방법 |
---|---|---|
어플리케이션생성 | 채팅을 위한 앱을 생성한다.
앱을 생성하고 __init__.py와 views.py를 제외한 모든 것들을 지운다. |
django-admin startapp chat |
앱 등록 | settings.py에 추가. | INSTALLED_APPS = [
'chat',
|
URL매핑 | 기본이 되는 urls.py 안에서 해당 앱으로 매핑을 시켜준다. | path('chat/', include('chat.urls')), |
URL매핑2 | 그리고 앱 안의 urls.py 작성. | from dfango.urls import path
from . import views
urlpatterns = [
path('/', views.index, name='index'),
]
|
view 작성 | from django.shortcuts import render
def index(request):
return render(request, 'chat/index.html', {})
| |
탬플릿 준비 | 앱 하위에 templates>chat 디렉터리까지 만든다.
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>
|
여기까지 하고 runserver 후 제대로 작동하는지 확인하자.
2.2 패키지 설치
과정 | 설명 | 방법 |
---|---|---|
패키지 설치 | Channels 패키지를 설치한다. | pip install -U channels |
앱 등록 | settings.py에 추가.
channels는 runserver 명령을 제어하여 기존 서버를 대체한다. |
INSTALLED_APPS 하위, 가장 처음에 'channels' 넣는다.INSTALLED_APPS = [
'channels', # 다른 서드파티 앱과 충돌할 수 있어 가장 처음에 둔다.
... # 공식 튜토리얼에선 'chat'을 상위로 올리지만, 아래에 있어도 상관 없다.
...
'chat',
|
라우팅 설정 작성 | 가장 상위의 디렉터리에 routing.py를 다음과 같이 작성한다.
(취향에 따라 달리 작성해도 된다.) |
from channels.routing import ProtocolTypeRouter
application = ProtocolTypeRouter({
# (http->django views is added by default)
})
|
설정 추가 | settings.py 안에 우측의 내용을 추가한다.
라우팅파일의 위치만 잘 잡아주면 된다. |
ASGI_APPLICATION = 'routing.application' # routing.py 파일의 application을 불러온다.
|