바뀜

1,288 바이트 추가됨 ,  2022년 10월 15일 (토) 12:22
편집 요약 없음
9번째 줄: 9번째 줄:  
|-
 
|-
 
|3
 
|3
|2.2 이상
+
|2.2~
 
|
 
|
 
|-
 
|-
20번째 줄: 20번째 줄:     
ASGI는 비동기 요청인 웹 소켓을 처리하는 이벤트로 connect, send, receive, disconnect가 있다.
 
ASGI는 비동기 요청인 웹 소켓을 처리하는 이벤트로 connect, send, receive, disconnect가 있다.
      
===패키지 설치===
 
===패키지 설치===
 +
버전에 따라 방법이 달라질 수 있으니 공식 링크를 참고하자.
 
{| class="wikitable"
 
{| class="wikitable"
 
!과정
 
!과정
30번째 줄: 30번째 줄:  
|패키지 설치
 
|패키지 설치
 
|Channels 패키지를 설치한다.
 
|Channels 패키지를 설치한다.
|pip install -U channels
+
stable 버전이 설치되며, 필요한 패키지들이 함께 설치된다.
 +
|pip install channels
 
|-
 
|-
 
|앱 등록
 
|앱 등록
48번째 줄: 49번째 줄:  
|프로젝트의 asgi.py를 다음과 같이 편집한다.
 
|프로젝트의 asgi.py를 다음과 같이 편집한다.
 
(장고 2.2라면 asgi.py가 없어 새로 작성해주어야 한다.)
 
(장고 2.2라면 asgi.py가 없어 새로 작성해주어야 한다.)
|장고 3.X대라면 아래처럼 덮어주고,<syntaxhighlight lang="python">
+
 
 +
 
 +
mysite.settings는 자신의 설정에 따라 다를테니, 여기에 맞게 해주면 된다.
 +
| 장고 3.X대라면 아래처럼 덮어주고,<syntaxhighlight lang="python">
 
import os
 
import os
   63번째 줄: 67번째 줄:  
     # Just HTTP for now. (We can add other protocols later.)
 
     # Just HTTP for now. (We can add other protocols later.)
 
})
 
})
</syntaxhighlight>장고 2.2대라면 아래처럼 settings.py와 같은 경로에 새로 작성해준다.<syntaxhighlight lang="python">
+
</syntaxhighlight>장고 2.2대라면 settings.py와 같은 경로에 새로 작성해준다.<syntaxhighlight lang="python">
 
import os
 
import os
   77번째 줄: 81번째 줄:  
   # Just HTTP for now. (We can add other protocols later.)
 
   # Just HTTP for now. (We can add other protocols later.)
 
})
 
})
</syntaxhighlight>|-
+
</syntaxhighlight> |<syntaxhighlight lang="python">
|라우팅 설정 작성
+
import os
|가장 상위의 디렉터리에 routing.py를 다음과 같이 작성한다.
+
 
(취향에 따라 달리 작성해도 된다.)
+
from channels.routing import ProtocolTypeRouter
|<syntaxhighlight lang="python">
+
from django.core.asgi import get_asgi_application
 +
 
 +
os.environ.setdefault('DJANGO_SETTINGS_MODULE', 'mysite.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.)
 +
})
 +
</syntaxhighlight>asgi.py가 없는 낮은버전의 경우 아래와 같이 만들어주자.<syntaxhighlight lang="python">
 +
import os
 +
 
 +
import django
 +
from channels.http import AsgiHandler
 
from channels.routing import ProtocolTypeRouter
 
from channels.routing import ProtocolTypeRouter
 +
 +
os.environ.setdefault('DJANGO_SETTINGS_MODULE', 'mysite.settings')
 +
django.setup()
    
application = ProtocolTypeRouter({
 
application = ProtocolTypeRouter({
    # (http->django views is added by default)
+
  "http": AsgiHandler(),
 +
  # Just HTTP for now. (We can add other protocols later.)
 
})
 
})
 
</syntaxhighlight>
 
</syntaxhighlight>
 
|-
 
|-
 
|설정 추가
 
|설정 추가
|settings.py 안에 우측의 내용을 추가한다.
+
|settings.py 안에 우측의 내용을 추가한다. # 프로젝트의 세팅에 맞춰 변형.
 
라우팅파일의 위치만 잘 잡아주면 된다.
 
라우팅파일의 위치만 잘 잡아주면 된다.
 
|<syntaxhighlight lang="python">
 
|<syntaxhighlight lang="python">
ASGI_APPLICATION = 'routing.application'  # routing.py 파일의 application을 불러온다.
+
ASGI_APPLICATION = "myproject.asgi.application"
 
</syntaxhighlight>(라우팅 파일 안의 application을 가져온다.)
 
</syntaxhighlight>(라우팅 파일 안의 application을 가져온다.)
 +
|-
 +
|확인
 +
|서버를 실행해 확인해 보자.
 +
|python manage.py runserver
 
|}
 
|}
 +
 +
= 관련 에러 =
 +
 +
=== ModuleNotFoundError: No module named 'channels' ===
 +
채널을 찾지 못해 생기는 에러인데, 구글링해보면 설치하라는 이야기가 대다수이다.. 문제는 설치를 하고 위 과정을 진행했는데도 나오는 경우..
 +
 +
어이없게도 IDE를 다시 켜거나 재부팅하면 해결되었다.
 
[[분류:장고 웹소켓]]
 
[[분류:장고 웹소켓]]