drf + channels 를 이용한 실시간 채팅 서버
- Python 3.11
- 언어
- Django 5.0.1
- 백엔드 서버
- Channels 4.0.0
- HTTP 뿐만 아니라 Websocket, chatbot 등 다양한 프로토콜 지원하는 비동기 처리 패키지
- Daphne 4.0.0
- ASGI(비동기 서버) application server 패키지
- python 3.11 환경에서 실행 가정
pip install -r requirements.txt
python manage.py makemigrations chatting
python manage.py migrate chatting
python manage.py runserver
path : /ws/chatting/
path : /ws/chatting/ (동일)
형식 : json
설명 : 최초 접속 후 token 정보 전송하여 인증
{
"token" : "<JWT token>"
}
응답
형식 : json
설명 : 온라인 상태의 유저 닉네임 제공
{
"type": "system_message",
"message": "You have successfully logged in",
"online_friends": [
["<user_id1>",
"<user_id2>"]
],
"to_id" : "<수신자 id>",
"time": "<ISO 8601 형식의 시각>"
}
path : /ws/chatting/ (동일)
형식 : json
{
"target_id" : "<대상 id>",
"message" : "<메시지 내용>"
}
{
"type": "chat_message",
"message": "<메시지 내용>",
"from_id" : "<발신자 id>",
"to_id" : "<수신자 id>",
"time": "<ISO 8601 형식의 시각>",
}
{
"type": "system_message",
"error": "No User or Offline",
"from_id": "<target_id>",
"to_id" : "<수신자 id>",
"time": "<ISO 8601 형식의 시각>"
}
path : /api/chatting/blockedusers/
method : post
authentication : bearer token
{
"target_id" : "<차단하고자 하는 유저 id : int, 필수>"
}
status : 201 created
{
"target_id": "<차단된 유저 id>"
}
status : 401 UNAUTHORIZED
{
"target_id": [
"해당 id가 존재하지 않습니다."
]
}
path : /api/chatting/blockedusers/
method : delete
authentication : bearer token
{
"target_id" : "<차단하고자 하는 유저 id : int, 필수>"
}
status : 200 OK
status : 401 UNAUTHORIZED
path: /api/chatting/blockedusers/
method : GET
응답: json list
[
{
"id" : "<차단된 id>"
},
//...
]
path: /blockedusers/?target_id=<차단 조회할 유저 id>
method : GET
응답: json
status : 200 OK
{
"is_blocked": <차단인지 아닌지 : boolean>
}
status : 404 NOT FOUND
해당 유저 닉네임이 없을 때
{
"detail": "Not found."
}
path : /s2sapi/system-message/
method : POST
body : json
description : 외부에서 요청이 아닌 시스템 내부에서 요청
{
"target_id": "<전송하고자 하는 유저 id>",
"message": "<메시지 내용>"
}
토너먼트 메시지 전송 시
{
"type" : "invite_game",
"target_id": "<전송하고자 하는 유저 id>",
"message" : "<메시지 내용>",
"room_id" : "<uuid room id>"
}
응답 : json
status : 200 OK
{
"type": "system_message",
"from": "admin",
"message": "<메시지 내용>",
"to_id" : "<수신자 id>",
"time": "<ISO 8601 형식의 시각>"
}
status : 400 Bad Request
description : 전송 형식 오류
status : 404 Not Found description : 해당하는 유저가 없거나 오프라인일 때
응답 : json
설명 : 새로 접속한 친구는 online 보냄, 접속 종료한 친구는 offline
{
"type": "send_status",
"from_id": "<상태 업데이트 된 유저 id>",
"to_id" : "<수신자 id>",
"status": "<online or offline>",
"time": "<ISO 8601 형식의 시각>"
}
-
path : api/chatting/is-online/?id=<조회할 id>
-
method : GET
-
필요 데이터 :
- id: query parameter
- access_token : Authorization header
-
응답 : json
{
"id": "<user id : int>",
"is_online": "<true or false>"
}
요청 : json
설명 : 초대시 invite, 초대 취소 시 cancel
{
"target_id": "<초대하고자 하는 대상 id>",
"type" : "invite_game",
"status" : "<'invite' or 'cancel'>"
}
응답 : json
설명 : 수신자, 발신자 둘 다 보냄.
{
"type" : "invite_game",
"from_id": "<수신자 id>",
"to_id": "<발신자 id>",
"time": "<ISO 8601 형식의 시각>",
"room_id": "<uuid 형식>"
}