Skip to content

Commit

Permalink
add chat consumers.py, routing.py, add ws to asgi.py, update readme
Browse files Browse the repository at this point in the history
  • Loading branch information
yennanliu committed Jul 27, 2024
1 parent 69f2f58 commit b41451a
Show file tree
Hide file tree
Showing 6 changed files with 85 additions and 1 deletion.
11 changes: 11 additions & 0 deletions chat-room/README.md
Original file line number Diff line number Diff line change
Expand Up @@ -4,13 +4,24 @@
```bash
cd chat-room/mysite/

# init migrate
python manage.py migrate

# run app
# (make sure at same level with manage.py)
python3 manage.py runserver
```

```bash
# check DB
sqlite3 db.sqlite3
```

## Endpoints

- http://127.0.0.1:8000/chat/
- http://127.0.0.1:8000/chat/lobby/

- http://127.0.0.1:8000/admin/

## Ref
Expand Down
2 changes: 2 additions & 0 deletions chat-room/doc/progress.md
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)
26 changes: 26 additions & 0 deletions chat-room/mysite/chat/consumers.py
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}))
7 changes: 7 additions & 0 deletions chat-room/mysite/chat/routing.py
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 modified chat-room/mysite/db.sqlite3
Binary file not shown.
40 changes: 39 additions & 1 deletion chat-room/mysite/mysite/asgi.py
Original file line number Diff line number Diff line change
Expand Up @@ -7,10 +7,48 @@
https://docs.djangoproject.com/en/4.2/howto/deployment/asgi/
"""

# https://channels.readthedocs.io/en/latest/tutorial/part_2.html

import os

from django.core.asgi import get_asgi_application

from channels.auth import AuthMiddlewareStack
from channels.routing import ProtocolTypeRouter, URLRouter
from channels.security.websocket import AllowedHostsOriginValidator

os.environ.setdefault('DJANGO_SETTINGS_MODULE', 'mysite.settings')

application = get_asgi_application()
#application = get_asgi_application()

# 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()


from ..chat.routing import websocket_urlpatterns

"""
This root routing configuration specifies that when a connection is made to the Channels development server,
the ProtocolTypeRouter will first inspect the type of connection. If it is a WebSocket connection (ws:// or wss://),
the connection will be given to the AuthMiddlewareStack.
The AuthMiddlewareStack will populate the connection’s scope
with a reference to the currently authenticated user,
similar to how Django’s AuthenticationMiddleware populates
the request object of a view function with the currently authenticated user.
(Scopes will be discussed later in this tutorial.)
Then the connection will be given to the URLRouter.
The URLRouter will examine the HTTP path of the connection to route it to a particular consumer,
based on the provided url patterns.
"""
application = ProtocolTypeRouter(
{
"http": django_asgi_app,
"websocket": AllowedHostsOriginValidator(
AuthMiddlewareStack(URLRouter(websocket_urlpatterns))
),
}
)

0 comments on commit b41451a

Please sign in to comment.