-
Notifications
You must be signed in to change notification settings - Fork 1
Commit
This commit does not belong to any branch on this repository, and may belong to a fork outside of the repository.
add chat consumers.py, routing.py, add ws to asgi.py, update readme
- Loading branch information
Showing
6 changed files
with
85 additions
and
1 deletion.
There are no files selected for viewing
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -0,0 +1,2 @@ | ||
## 20240727 | ||
- https://channels.readthedocs.io/en/latest/tutorial/part_2.html (to complete) |
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -0,0 +1,26 @@ | ||
import json | ||
|
||
from channels.generic.websocket import WebsocketConsumer | ||
|
||
|
||
# https://channels.readthedocs.io/en/latest/tutorial/part_2.html | ||
""" | ||
This is a synchronous WebSocket consumer | ||
that accepts all connections, receives messages | ||
from its client, and echos those messages back to the same client. | ||
For now it does not broadcast messages to other clients in the same room. | ||
""" | ||
|
||
|
||
class ChatConsumer(WebsocketConsumer): | ||
def connect(self): | ||
self.accept() | ||
|
||
def disconnect(self, close_code): | ||
pass | ||
|
||
def receive(self, text_data): | ||
text_data_json = json.loads(text_data) | ||
message = text_data_json["message"] | ||
|
||
self.send(text_data=json.dumps({"message": message})) |
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -0,0 +1,7 @@ | ||
from django.urls import re_path | ||
|
||
from . import consumers | ||
|
||
websocket_urlpatterns = [ | ||
re_path(r"ws/chat/(?P<room_name>\w+)/$", consumers.ChatConsumer.as_asgi()), | ||
] |
Binary file not shown.
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters