Skip to content

Commit d18d0c0

Browse files
committed
충헌님 room 기능
1 parent 161fdda commit d18d0c0

27 files changed

+130
-17
lines changed
32 Bytes
Binary file not shown.

chongzaban/templates/partials/_header.html

+4-7
Original file line numberDiff line numberDiff line change
@@ -124,9 +124,8 @@ <h6 class="dropdown-title">Messages</h6>
124124
></div>
125125
</div>
126126
<div class="content-wrapper">
127-
<small class="name">Clifford Gordon</small>
128127
<small class="content-text">
129-
Lorem ipsum dolor sit amet.
128+
<a href=http://127.0.0.1:8000/room/soccer>축구방</a>
130129
</small>
131130
</div>
132131
</div>
@@ -142,9 +141,8 @@ <h6 class="dropdown-title">Messages</h6>
142141
></div>
143142
</div>
144143
<div class="content-wrapper">
145-
<small class="name">Rachel Doyle</small>
146144
<small class="content-text">
147-
Lorem ipsum dolor sit amet.
145+
<a href=http://127.0.0.1:8000/room/baseball>야구방</a>
148146
</small>
149147
</div>
150148
</div>
@@ -160,9 +158,8 @@ <h6 class="dropdown-title">Messages</h6>
160158
></div>
161159
</div>
162160
<div class="content-wrapper">
163-
<small class="name">Lewis Guzman</small>
164161
<small class="content-text">
165-
Lorem ipsum dolor sit amet.
162+
<a href=http://127.0.0.1:8000/room/basketball>농구방</a>
166163
</small>
167164
</div>
168165
</div>
@@ -218,4 +215,4 @@ <h6 class="dropdown-title">Apps</h6>
218215
</ul>
219216
</div>
220217
</div>
221-
</nav>
218+
</nav>

chongzaban/urls.py

+2-1
Original file line numberDiff line numberDiff line change
@@ -26,5 +26,6 @@
2626
path('accounts/', include('allauth.urls')), # allauth
2727
path('buckets/', include('buckets.urls')), # bucketlist
2828
path('user/', include('user.urls')), # user
29-
path('board/', include('board.urls')),#게시판
29+
path('board/', include('board.urls')),# 게시판
30+
path('room/', include('room.urls')),# 채팅방
3031
] + static(settings.MEDIA_URL, document_root=settings.MEDIA_ROOT)
135 Bytes
Binary file not shown.
8 Bytes
Binary file not shown.

room/__pycache__/admin.cpython-38.pyc

176 Bytes
Binary file not shown.

room/__pycache__/admin.cpython-39.pyc

8 Bytes
Binary file not shown.

room/__pycache__/apps.cpython-39.pyc

-54 Bytes
Binary file not shown.
1.39 KB
Binary file not shown.
1.4 KB
Binary file not shown.
-177 Bytes
Binary file not shown.
301 Bytes
Binary file not shown.
308 Bytes
Binary file not shown.

room/__pycache__/urls.cpython-38.pyc

311 Bytes
Binary file not shown.

room/__pycache__/urls.cpython-39.pyc

282 Bytes
Binary file not shown.

room/__pycache__/views.cpython-38.pyc

461 Bytes
Binary file not shown.

room/__pycache__/views.cpython-39.pyc

339 Bytes
Binary file not shown.

room/apps.py

+1-2
Original file line numberDiff line numberDiff line change
@@ -1,6 +1,5 @@
11
from django.apps import AppConfig
22

33

4-
class RoomConfig(AppConfig):
5-
default_auto_field = 'django.db.models.BigAutoField'
4+
class ChatappConfig(AppConfig):
65
name = 'room'

room/consumers.py

+48
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,48 @@
1+
# chat/consumers.py
2+
import json
3+
from asgiref.sync import async_to_sync
4+
from channels.generic.websocket import WebsocketConsumer
5+
6+
7+
class ChatConsumer(WebsocketConsumer):
8+
def connect(self):
9+
self.room_name = self.scope['url_route']['kwargs']['room_name']
10+
self.room_group_name = 'chat_%s' % self.room_name
11+
12+
# Join room group
13+
async_to_sync(self.channel_layer.group_add)(
14+
self.room_group_name,
15+
self.channel_name
16+
)
17+
18+
self.accept()
19+
20+
def disconnect(self, close_code):
21+
# Leave room group
22+
async_to_sync(self.channel_layer.group_discard)(
23+
self.room_group_name,
24+
self.channel_name
25+
)
26+
27+
# Receive message from WebSocket
28+
def receive(self, text_data):
29+
text_data_json = json.loads(text_data)
30+
message = text_data_json['message']
31+
32+
# Send message to room group
33+
async_to_sync(self.channel_layer.group_send)(
34+
self.room_group_name,
35+
{
36+
'type': 'chat_message',
37+
'message': message
38+
}
39+
)
40+
41+
# Receive message from room group
42+
def chat_message(self, event):
43+
message = event['message']
44+
45+
# Send message to WebSocket
46+
self.send(text_data=json.dumps({
47+
'message': message
48+
}))
146 Bytes
Binary file not shown.
Binary file not shown.

room/models.py

-3
This file was deleted.

room/routing.py

+9
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,9 @@
1+
# chat/routing.py
2+
from django.urls import re_path
3+
4+
from . import consumers
5+
6+
websocket_urlpatterns = [
7+
re_path(r'ws/room/(?P<room_name>\w+)/$', consumers.ChatConsumer),
8+
]
9+

room/templates/room/room.html

+54
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,54 @@
1+
{% extends 'shared/base.html' %}
2+
{% block content %}
3+
<body>
4+
5+
{{ room_name|json_script:"room-name" }}
6+
{% if room_name == 'soccer' %}
7+
<h1>축구방</h1>
8+
{% elif room_name == 'baseball' %}
9+
<h1>야구방</h1>
10+
{% else %}
11+
<h1>농구방</h1>
12+
{% endif %}
13+
<textarea id="chat-log" cols="70" rows="20"></textarea><br>
14+
<input id="chat-message-input" type="text" size="67"><br>
15+
<input id="chat-message-submit" type="button" value="Send">
16+
{{ room_name|json_script:"room-name" }}
17+
<script>
18+
const roomName = JSON.parse(document.getElementById('room-name').textContent);
19+
20+
const chatSocket = new WebSocket(
21+
'ws://' +
22+
window.location.host +
23+
'/ws/room/' +
24+
roomName +
25+
'/'
26+
);
27+
28+
chatSocket.onmessage = function (e) {
29+
const data = JSON.parse(e.data);
30+
document.querySelector('#chat-log').value += (data.message + '\n');
31+
};
32+
33+
chatSocket.onclose = function (e) {
34+
console.error('Chat socket closed unexpectedly');
35+
};
36+
const name = "{{user.username}}";
37+
document.querySelector('#chat-message-input').focus();
38+
document.querySelector('#chat-message-input').onkeyup = function (e) {
39+
if (e.keyCode === 13) { // enter, return
40+
document.querySelector('#chat-message-submit').click();
41+
}
42+
};
43+
44+
document.querySelector('#chat-message-submit').onclick = function (e) {
45+
const messageInputDom = document.querySelector('#chat-message-input');
46+
const message = messageInputDom.value;
47+
chatSocket.send(JSON.stringify({
48+
'message': name + ": "+ message
49+
}));
50+
messageInputDom.value = '';
51+
};
52+
</script>
53+
</body>
54+
{% endblock %}

room/tests.py

-3
This file was deleted.

room/urls.py

+8
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,8 @@
1+
# chat/urls.py
2+
from django.urls import path
3+
4+
from . import views
5+
6+
urlpatterns = [
7+
path('<str:room_name>/', views.room, name='room'),
8+
]

room/views.py

+4-1
Original file line numberDiff line numberDiff line change
@@ -1,3 +1,6 @@
11
from django.shortcuts import render
22

3-
# Create your views here.
3+
def room(request, room_name):
4+
return render(request, 'room/room.html', {
5+
'room_name': room_name
6+
})

0 commit comments

Comments
 (0)